Skip to content

Commit 33bca3b

Browse files
Merge pull request #247 from contentstack/feat/dx-3632-terms-loacales
Feat/dx 3632 terms loacales
2 parents 6ed4fdb + 92d633d commit 33bca3b

File tree

4 files changed

+41
-1
lines changed

4 files changed

+41
-1
lines changed

src/lib/term.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,25 @@ export class Term {
1212
this._termUid = termUid;
1313
this._urlPath = `/taxonomy-manager/${this._taxonomyUid}/terms/${this._termUid}`; // TODO: change to /taxonomies
1414
}
15+
16+
/**
17+
* @method locales
18+
* @memberof Term
19+
* @description Fetches locales for the term
20+
* @returns {Promise<T>}
21+
* @example
22+
* import contentstack from '@contentstack/delivery-sdk'
23+
*
24+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
25+
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').locales();
26+
*/
27+
async locales<T>(): Promise<T> {
28+
const urlPath = `/taxonomy-manager/${this._taxonomyUid}/terms/${this._termUid}/locales`;
29+
const response = await getData(this._client, urlPath);
30+
if (response.locales) return response.locales as T;
31+
return response;
32+
}
33+
1534
async fetch<T>(): Promise<T> {
1635
const response = await getData(this._client, this._urlPath);
1736

test/api/term.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,15 @@ describe("Terms API test cases", () => {
1313
expect(result.created_by).toBeDefined();
1414
expect(result.updated_by).toBeDefined();
1515
});
16+
17+
it("should get locales for a term", async () => {
18+
// const result = await makeTerms("term1").locales().fetch();
19+
// API under building phase, so it should throw error
20+
expect(async () => await makeTerms("term1").locales()).rejects.toThrow();
21+
// TODO: add assertions
22+
});
1623
});
24+
1725
function makeTerms(termUid = ""): Term {
1826
const terms = stack.taxonomy("taxonomy_testing").term(termUid);
1927
return terms;

test/unit/term.spec.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { AxiosInstance, httpClient } from '@contentstack/core';
22
import MockAdapter from 'axios-mock-adapter';
3-
import { termQueryFindResponseDataMock } from '../utils/mocks';
3+
import { termQueryFindResponseDataMock, termLocalesResponseDataMock } from '../utils/mocks';
44
import { MOCK_CLIENT_OPTIONS } from '../utils/constant';
55
import { Term } from '../../src/lib/term';
66
import { Taxonomy } from '../../src/lib/taxonomy';
@@ -25,4 +25,11 @@ describe('Term class', () => {
2525
const response = await term.fetch();
2626
expect(response).toEqual(termQueryFindResponseDataMock.terms[0]);
2727
});
28+
29+
it('should fetch locales for a term when locales() is called', async () => {
30+
mockClient.onGet('/taxonomy-manager/taxonomy_testing/terms/term1/locales').reply(200, termLocalesResponseDataMock.terms); //TODO: change to /taxonomies
31+
32+
const response = await term.locales();
33+
expect(response).toEqual(termLocalesResponseDataMock.terms);
34+
});
2835
});

test/utils/mocks.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1698,6 +1698,11 @@ const taxonomyFindResponseDataMock = {
16981698
}
16991699
]
17001700
}
1701+
1702+
const termLocalesResponseDataMock = {
1703+
terms: []
1704+
}
1705+
17011706
const termQueryFindResponseDataMock = {
17021707
"terms": [
17031708
{
@@ -1743,4 +1748,5 @@ export {
17431748
gfieldQueryFindResponseDataMock,
17441749
taxonomyFindResponseDataMock,
17451750
termQueryFindResponseDataMock,
1751+
termLocalesResponseDataMock,
17461752
};

0 commit comments

Comments
 (0)