Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions src/lib/term.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,28 @@ export class Term {
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').locales();
*/
async locales<T>(): Promise<T> {
const urlPath = `/taxonomy-manager/${this._taxonomyUid}/terms/${this._termUid}/locales`;
const response = await getData(this._client, urlPath);
const response = await getData(this._client, `${this._urlPath}/locales`);
if (response.locales) return response.locales as T;
return response;
}

/**
* @method ancestors
* @memberof Term
* @description Fetches ancestors for the term
* @returns {Promise<T>}
* @example
* import contentstack from '@contentstack/delivery-sdk'
*
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').ancestors();
*/
async ancestors<T>(): Promise<T> {
const response = await getData(this._client, `${this._urlPath}/ancestors`);
if (response.ancestors) return response.ancestors as T;
return response;
}

async fetch<T>(): Promise<T> {
const response = await getData(this._client, this._urlPath);

Expand Down
2 changes: 1 addition & 1 deletion test/api/taxonomy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('ContentType API test cases', () => {
});

it('should give a single taxonomy when taxonomy method is called with taxonomyUid', async () => {
const result = await makeTaxonomy('taxonomy_testing').fetch<TTaxonomy>();
const result = await makeTaxonomy('taxonomy_testing_3').fetch<TTaxonomy>();
expect(result).toBeDefined();
});
});
Expand Down
2 changes: 1 addition & 1 deletion test/api/term-query.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const stack = stackInstance();

describe("Terms API test cases", () => {
it("should check for terms is defined", async () => {
const result = await makeTerms("taxonomy_testing").find<TTerm>();
const result = await makeTerms("taxonomy_testing_3").find<TTerm>();
if (result.terms) {
expect(result.terms).toBeDefined();
expect(result.terms[0].taxonomy_uid).toBeDefined();
Expand Down
17 changes: 12 additions & 5 deletions test/api/term.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { Term } from "../../src/lib/term";
import { stackInstance } from "../utils/stack-instance";
import { TTerm } from "./types";
import { TTerm, TTerms } from "./types";

const stack = stackInstance();

describe("Terms API test cases", () => {
it("should get a term by uid", async () => {
const result = await makeTerms("term1").fetch<TTerm>();
const result = await makeTerms("vehicles").fetch<TTerm>();
expect(result).toBeDefined();
expect(result.taxonomy_uid).toBeDefined();
expect(result.uid).toBeDefined();
Expand All @@ -15,14 +15,21 @@ describe("Terms API test cases", () => {
});

it("should get locales for a term", async () => {
// const result = await makeTerms("term1").locales().fetch();
// const result = await makeTerms("vehicles").locales().fetch();
// API under building phase, so it should throw error
expect(async () => await makeTerms("term1").locales()).rejects.toThrow();
expect(async () => await makeTerms("vehicles").locales()).rejects.toThrow();
// TODO: add assertions
});

it("should get ancestors for a term", async () => {
const result = await makeTerms("sleeper").ancestors<TTerms>();
expect(result).toBeDefined();
expect(result.terms).toBeDefined();
expect(result.terms[0].name).toBeDefined();
});
});

function makeTerms(termUid = ""): Term {
const terms = stack.taxonomy("taxonomy_testing").term(termUid);
const terms = stack.taxonomy("taxonomy_testing_3").term(termUid);
return terms;
}
9 changes: 8 additions & 1 deletion test/unit/term.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AxiosInstance, httpClient } from '@contentstack/core';
import MockAdapter from 'axios-mock-adapter';
import { termQueryFindResponseDataMock, termLocalesResponseDataMock } from '../utils/mocks';
import { termQueryFindResponseDataMock, termLocalesResponseDataMock, termAncestorsResponseDataMock } from '../utils/mocks';
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';
import { Term } from '../../src/lib/term';
import { Taxonomy } from '../../src/lib/taxonomy';
Expand Down Expand Up @@ -32,4 +32,11 @@ describe('Term class', () => {
const response = await term.locales();
expect(response).toEqual(termLocalesResponseDataMock.terms);
});

it('should fetch ancestors for a term when ancestors() is called', async () => {
mockClient.onGet('/taxonomy-manager/taxonomy_testing/terms/term1/ancestors').reply(200, termAncestorsResponseDataMock);

const response = await term.ancestors();
expect(response).toEqual(termAncestorsResponseDataMock);
});
});
36 changes: 36 additions & 0 deletions test/utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1703,6 +1703,41 @@ const termLocalesResponseDataMock = {
terms: []
}

const termAncestorsResponseDataMock = {
"terms": [
{
"uid": "vehicles",
"name": "vehicles",
"publish_details": {
"time": "2025-10-28T06:54:12.505Z",
"user": "user",
"environment": "environment",
"locale": "en-us"
}
},
{
"uid": "buses",
"name": "buses",
"publish_details": {
"time": "2025-10-28T06:54:12.514Z",
"user": "user",
"environment": "environment",
"locale": "en-us"
}
},
{
"uid": "vrl",
"name": "vrl",
"publish_details": {
"time": "2025-10-28T06:54:12.570Z",
"user": "user",
"environment": "environment",
"locale": "en-us"
}
}
]
}

const termQueryFindResponseDataMock = {
"terms": [
{
Expand Down Expand Up @@ -1749,4 +1784,5 @@ export {
taxonomyFindResponseDataMock,
termQueryFindResponseDataMock,
termLocalesResponseDataMock,
termAncestorsResponseDataMock,
};