From 908f2063c2d70c6aea95325f749e2564a7005392 Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 28 Oct 2025 11:54:19 +0530 Subject: [PATCH 1/2] feat: Add locales method to Term class and corresponding tests --- src/lib/term.ts | 17 +++++++++++++++++ test/api/term.spec.ts | 8 ++++++++ test/unit/term.spec.ts | 9 ++++++++- test/utils/mocks.ts | 17 +++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/lib/term.ts b/src/lib/term.ts index 615e95f..917d55c 100644 --- a/src/lib/term.ts +++ b/src/lib/term.ts @@ -12,6 +12,23 @@ export class Term { this._termUid = termUid; this._urlPath = `/taxonomy-manager/${this._taxonomyUid}/terms/${this._termUid}`; // TODO: change to /taxonomies } + + /** + * @method locales + * @memberof Term + * @description Fetches locales 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').locales().fetch(); + */ + locales(): this { + this._urlPath = `${this._urlPath}/locales`; + return this; + } + async fetch(): Promise { const response = await getData(this._client, this._urlPath); diff --git a/test/api/term.spec.ts b/test/api/term.spec.ts index 72c1c6e..c599655 100644 --- a/test/api/term.spec.ts +++ b/test/api/term.spec.ts @@ -13,7 +13,15 @@ describe("Terms API test cases", () => { expect(result.created_by).toBeDefined(); expect(result.updated_by).toBeDefined(); }); + + it("should get locales for a term", async () => { + // const result = await makeTerms("term1").locales().fetch(); + // API under building phase, so it should throw error + expect(async () => await makeTerms("term1").locales().fetch()).rejects.toThrow(); + // TODO: add assertions + }); }); + function makeTerms(termUid = ""): Term { const terms = stack.taxonomy("taxonomy_testing").term(termUid); return terms; diff --git a/test/unit/term.spec.ts b/test/unit/term.spec.ts index 65d4140..9f60002 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 } from '../utils/mocks'; +import { termQueryFindResponseDataMock, termLocalesFindResponseDataMock } from '../utils/mocks'; import { MOCK_CLIENT_OPTIONS } from '../utils/constant'; import { Term } from '../../src/lib/term'; import { Taxonomy } from '../../src/lib/taxonomy'; @@ -25,4 +25,11 @@ describe('Term class', () => { const response = await term.fetch(); expect(response).toEqual(termQueryFindResponseDataMock.terms[0]); }); + + it('should fetch locales for a term when locales().fetch() is called', async () => { + mockClient.onGet('/taxonomy-manager/taxonomy_testing/terms/term1/locales').reply(200, termLocalesFindResponseDataMock); + + const response = await term.locales().fetch(); + expect(response).toEqual(termLocalesFindResponseDataMock.locales); + }); }); diff --git a/test/utils/mocks.ts b/test/utils/mocks.ts index 82c4bec..51d2b32 100644 --- a/test/utils/mocks.ts +++ b/test/utils/mocks.ts @@ -1698,6 +1698,22 @@ const taxonomyFindResponseDataMock = { } ] } + +const termLocalesFindResponseDataMock = { + "locales": [ + { + "code": "en-us", + "name": "English (United States)", + "fallback_code": null + }, + { + "code": "es-es", + "name": "Spanish (Spain)", + "fallback_code": "en-us" + } + ] +} + const termQueryFindResponseDataMock = { "terms": [ { @@ -1743,4 +1759,5 @@ export { gfieldQueryFindResponseDataMock, taxonomyFindResponseDataMock, termQueryFindResponseDataMock, + termLocalesFindResponseDataMock, }; From 92d633d1dc5e302ddb35b984e2f7291e004829df Mon Sep 17 00:00:00 2001 From: "harshitha.d" Date: Tue, 28 Oct 2025 12:07:24 +0530 Subject: [PATCH 2/2] refactor: Update locales method in Term class to return data directly and adjust related tests --- src/lib/term.ts | 10 ++++++---- test/api/term.spec.ts | 2 +- test/unit/term.spec.ts | 10 +++++----- test/utils/mocks.ts | 17 +++-------------- 4 files changed, 15 insertions(+), 24 deletions(-) diff --git a/src/lib/term.ts b/src/lib/term.ts index 917d55c..e1596b2 100644 --- a/src/lib/term.ts +++ b/src/lib/term.ts @@ -22,11 +22,13 @@ export class Term { * 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').locales().fetch(); + * const result = await stack.taxonomy('taxonomy_uid').term('term_uid').locales(); */ - locales(): this { - this._urlPath = `${this._urlPath}/locales`; - return this; + async locales(): Promise { + const urlPath = `/taxonomy-manager/${this._taxonomyUid}/terms/${this._termUid}/locales`; + const response = await getData(this._client, urlPath); + if (response.locales) return response.locales as T; + return response; } async fetch(): Promise { diff --git a/test/api/term.spec.ts b/test/api/term.spec.ts index c599655..cb3f41b 100644 --- a/test/api/term.spec.ts +++ b/test/api/term.spec.ts @@ -17,7 +17,7 @@ describe("Terms API test cases", () => { it("should get locales for a term", async () => { // const result = await makeTerms("term1").locales().fetch(); // API under building phase, so it should throw error - expect(async () => await makeTerms("term1").locales().fetch()).rejects.toThrow(); + expect(async () => await makeTerms("term1").locales()).rejects.toThrow(); // TODO: add assertions }); }); diff --git a/test/unit/term.spec.ts b/test/unit/term.spec.ts index 9f60002..4c78f7f 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, termLocalesFindResponseDataMock } from '../utils/mocks'; +import { termQueryFindResponseDataMock, termLocalesResponseDataMock } from '../utils/mocks'; import { MOCK_CLIENT_OPTIONS } from '../utils/constant'; import { Term } from '../../src/lib/term'; import { Taxonomy } from '../../src/lib/taxonomy'; @@ -26,10 +26,10 @@ describe('Term class', () => { expect(response).toEqual(termQueryFindResponseDataMock.terms[0]); }); - it('should fetch locales for a term when locales().fetch() is called', async () => { - mockClient.onGet('/taxonomy-manager/taxonomy_testing/terms/term1/locales').reply(200, termLocalesFindResponseDataMock); + it('should fetch locales for a term when locales() is called', async () => { + mockClient.onGet('/taxonomy-manager/taxonomy_testing/terms/term1/locales').reply(200, termLocalesResponseDataMock.terms); //TODO: change to /taxonomies - const response = await term.locales().fetch(); - expect(response).toEqual(termLocalesFindResponseDataMock.locales); + const response = await term.locales(); + expect(response).toEqual(termLocalesResponseDataMock.terms); }); }); diff --git a/test/utils/mocks.ts b/test/utils/mocks.ts index 51d2b32..d1808ae 100644 --- a/test/utils/mocks.ts +++ b/test/utils/mocks.ts @@ -1699,19 +1699,8 @@ const taxonomyFindResponseDataMock = { ] } -const termLocalesFindResponseDataMock = { - "locales": [ - { - "code": "en-us", - "name": "English (United States)", - "fallback_code": null - }, - { - "code": "es-es", - "name": "Spanish (Spain)", - "fallback_code": "en-us" - } - ] +const termLocalesResponseDataMock = { + terms: [] } const termQueryFindResponseDataMock = { @@ -1759,5 +1748,5 @@ export { gfieldQueryFindResponseDataMock, taxonomyFindResponseDataMock, termQueryFindResponseDataMock, - termLocalesFindResponseDataMock, + termLocalesResponseDataMock, };