diff --git a/src/lib/term.ts b/src/lib/term.ts index e1596b29..f33ec0e0 100644 --- a/src/lib/term.ts +++ b/src/lib/term.ts @@ -25,12 +25,28 @@ export class Term { * const result = await stack.taxonomy('taxonomy_uid').term('term_uid').locales(); */ async locales(): Promise { - 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} + * @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(): Promise { + const response = await getData(this._client, `${this._urlPath}/ancestors`); + if (response.ancestors) return response.ancestors as T; + return response; + } + async fetch(): Promise { const response = await getData(this._client, this._urlPath); diff --git a/test/api/taxonomy.spec.ts b/test/api/taxonomy.spec.ts index 09a8f291..10e975c7 100644 --- a/test/api/taxonomy.spec.ts +++ b/test/api/taxonomy.spec.ts @@ -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(); + const result = await makeTaxonomy('taxonomy_testing_3').fetch(); expect(result).toBeDefined(); }); }); diff --git a/test/api/term-query.spec.ts b/test/api/term-query.spec.ts index 58f59574..8ea808db 100644 --- a/test/api/term-query.spec.ts +++ b/test/api/term-query.spec.ts @@ -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(); + const result = await makeTerms("taxonomy_testing_3").find(); if (result.terms) { expect(result.terms).toBeDefined(); expect(result.terms[0].taxonomy_uid).toBeDefined(); diff --git a/test/api/term.spec.ts b/test/api/term.spec.ts index cb3f41be..b09094be 100644 --- a/test/api/term.spec.ts +++ b/test/api/term.spec.ts @@ -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(); + const result = await makeTerms("vehicles").fetch(); expect(result).toBeDefined(); expect(result.taxonomy_uid).toBeDefined(); expect(result.uid).toBeDefined(); @@ -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(); + 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; } diff --git a/test/unit/term.spec.ts b/test/unit/term.spec.ts index 4c78f7f2..244b3e88 100644 --- a/test/unit/term.spec.ts +++ b/test/unit/term.spec.ts @@ -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'; @@ -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); + }); }); diff --git a/test/utils/mocks.ts b/test/utils/mocks.ts index d1808aea..5ff3626b 100644 --- a/test/utils/mocks.ts +++ b/test/utils/mocks.ts @@ -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": [ { @@ -1749,4 +1784,5 @@ export { taxonomyFindResponseDataMock, termQueryFindResponseDataMock, termLocalesResponseDataMock, + termAncestorsResponseDataMock, };