Skip to content

Commit

Permalink
[Bug #555] Create term as root when parent is from different vocabulary.
Browse files Browse the repository at this point in the history
  • Loading branch information
ledsoft committed Oct 31, 2024
1 parent 229acdd commit a863fe0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/action/AsyncTermActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*
* This file contains asynchronous actions related to term management in the frontend.
*/
import VocabularyUtils, { IRI } from "../util/VocabularyUtils";
import VocabularyUtils, { IRI, IRIImpl } from "../util/VocabularyUtils";
import ActionType from "./ActionType";
import { GetStoreState, ThunkDispatch } from "../util/Types";
import * as SyncActions from "./SyncActions";
Expand Down Expand Up @@ -84,8 +84,14 @@ function resolveTermCreationUrl(term: Term, targetVocabularyIri: IRI) {
let url = `${ENDPOINT}${targetVocabularyIri.fragment}/terms`;
const parents = Utils.sanitizeArray(term.parentTerms);
if (parents.length > 0) {
// Use one of the parents, it does not matter which one
url += `/${VocabularyUtils.create(parents[0].iri!).fragment}/subterms`;
const parentInThisVocabulary = parents.find(
(p) => p.vocabulary!.iri === IRIImpl.toString(targetVocabularyIri)
);
if (parentInThisVocabulary) {
url += `/${
VocabularyUtils.create(parentInThisVocabulary.iri!).fragment
}/subterms`;
}
}
return url;
}
Expand Down
60 changes: 60 additions & 0 deletions src/action/__tests__/AsyncTermActions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,66 @@ describe("AsyncTermActions", () => {
expect(data.vocabulary.iri).toEqual(vocabularyIri.toString());
});
});

it("adds term as root when parent is from different vocabulary", () => {
const term = new Term({
label: langString("Test term 1"),
iri: vocabularyIri.toString() + "term/test-term-1",
parentTerms: [
{
iri: Generator.generateUri(),
label: langString("Parent term from different vocabulary"),
vocabulary: {
iri: Generator.generateUri(),
},
},
],
});
const mock = jest.fn().mockImplementation(() => Promise.resolve());
Ajax.post = mock;
return Promise.resolve(
(store.dispatch as ThunkDispatch)(createTerm(term, vocabularyIri))
).then(() => {
expect(Ajax.post).toHaveBeenCalled();
expect(mock.mock.calls[0][0]).toEqual(
`${Constants.API_PREFIX}/vocabularies/${vocabularyIri.fragment}/terms`
);
});
});

it("uses parent term from current vocabulary when multiple parents are set", () => {
const parentIri = vocabularyIri.toString() + "/terms/parent-one";
const term = new Term({
label: langString("Test term 1"),
iri: vocabularyIri.toString() + "term/test-term-1",
parentTerms: [
{
iri: parentIri,
label: langString("Parent term from this vocabulary"),
vocabulary: {
iri: vocabularyIri.toString(),
},
},
{
iri: Generator.generateUri(),
label: langString("Parent term from different vocabulary"),
vocabulary: {
iri: Generator.generateUri(),
},
},
],
});
const mock = jest.fn().mockImplementation(() => Promise.resolve());
Ajax.post = mock;
return Promise.resolve(
(store.dispatch as ThunkDispatch)(createTerm(term, vocabularyIri))
).then(() => {
expect(Ajax.post).toHaveBeenCalled();
expect(mock.mock.calls[0][0]).toEqual(
`${Constants.API_PREFIX}/vocabularies/${vocabularyIri.fragment}/terms/parent-one/subterms`
);
});
});
});

describe("setTermDefinitionSource", () => {
Expand Down

0 comments on commit a863fe0

Please sign in to comment.