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
19 changes: 19 additions & 0 deletions src/lib/term.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<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').locales();
*/
async locales<T>(): Promise<T> {
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<T>(): Promise<T> {
const response = await getData(this._client, this._urlPath);

Expand Down
8 changes: 8 additions & 0 deletions test/api/term.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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 } 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';
Expand All @@ -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);
});
});
6 changes: 6 additions & 0 deletions test/utils/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,11 @@ const taxonomyFindResponseDataMock = {
}
]
}

const termLocalesResponseDataMock = {
terms: []
}

const termQueryFindResponseDataMock = {
"terms": [
{
Expand Down Expand Up @@ -1743,4 +1748,5 @@ export {
gfieldQueryFindResponseDataMock,
taxonomyFindResponseDataMock,
termQueryFindResponseDataMock,
termLocalesResponseDataMock,
};