Skip to content

Commit 92d633d

Browse files
refactor: Update locales method in Term class to return data directly and adjust related tests
1 parent 908f206 commit 92d633d

4 files changed

Lines changed: 15 additions & 24 deletions

File tree

src/lib/term.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ export class Term {
2222
* import contentstack from '@contentstack/delivery-sdk'
2323
*
2424
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
25-
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').locales().fetch();
25+
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').locales();
2626
*/
27-
locales(): this {
28-
this._urlPath = `${this._urlPath}/locales`;
29-
return this;
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;
3032
}
3133

3234
async fetch<T>(): Promise<T> {

test/api/term.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe("Terms API test cases", () => {
1717
it("should get locales for a term", async () => {
1818
// const result = await makeTerms("term1").locales().fetch();
1919
// API under building phase, so it should throw error
20-
expect(async () => await makeTerms("term1").locales().fetch()).rejects.toThrow();
20+
expect(async () => await makeTerms("term1").locales()).rejects.toThrow();
2121
// TODO: add assertions
2222
});
2323
});

test/unit/term.spec.ts

Lines changed: 5 additions & 5 deletions
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, termLocalesFindResponseDataMock } 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';
@@ -26,10 +26,10 @@ describe('Term class', () => {
2626
expect(response).toEqual(termQueryFindResponseDataMock.terms[0]);
2727
});
2828

29-
it('should fetch locales for a term when locales().fetch() is called', async () => {
30-
mockClient.onGet('/taxonomy-manager/taxonomy_testing/terms/term1/locales').reply(200, termLocalesFindResponseDataMock);
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
3131

32-
const response = await term.locales().fetch();
33-
expect(response).toEqual(termLocalesFindResponseDataMock.locales);
32+
const response = await term.locales();
33+
expect(response).toEqual(termLocalesResponseDataMock.terms);
3434
});
3535
});

test/utils/mocks.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,19 +1699,8 @@ const taxonomyFindResponseDataMock = {
16991699
]
17001700
}
17011701

1702-
const termLocalesFindResponseDataMock = {
1703-
"locales": [
1704-
{
1705-
"code": "en-us",
1706-
"name": "English (United States)",
1707-
"fallback_code": null
1708-
},
1709-
{
1710-
"code": "es-es",
1711-
"name": "Spanish (Spain)",
1712-
"fallback_code": "en-us"
1713-
}
1714-
]
1702+
const termLocalesResponseDataMock = {
1703+
terms: []
17151704
}
17161705

17171706
const termQueryFindResponseDataMock = {
@@ -1759,5 +1748,5 @@ export {
17591748
gfieldQueryFindResponseDataMock,
17601749
taxonomyFindResponseDataMock,
17611750
termQueryFindResponseDataMock,
1762-
termLocalesFindResponseDataMock,
1751+
termLocalesResponseDataMock,
17631752
};

0 commit comments

Comments
 (0)