diff --git a/src/lib/term.ts b/src/lib/term.ts index 615e95f..e1596b2 100644 --- a/src/lib/term.ts +++ b/src/lib/term.ts @@ -12,6 +12,25 @@ 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(); + */ + 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 { const response = await getData(this._client, this._urlPath); diff --git a/test/api/term.spec.ts b/test/api/term.spec.ts index 72c1c6e..cb3f41b 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()).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..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 } 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'; @@ -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() 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(); + expect(response).toEqual(termLocalesResponseDataMock.terms); + }); }); diff --git a/test/utils/mocks.ts b/test/utils/mocks.ts index 82c4bec..d1808ae 100644 --- a/test/utils/mocks.ts +++ b/test/utils/mocks.ts @@ -1698,6 +1698,11 @@ const taxonomyFindResponseDataMock = { } ] } + +const termLocalesResponseDataMock = { + terms: [] +} + const termQueryFindResponseDataMock = { "terms": [ { @@ -1743,4 +1748,5 @@ export { gfieldQueryFindResponseDataMock, taxonomyFindResponseDataMock, termQueryFindResponseDataMock, + termLocalesResponseDataMock, };