Skip to content

Commit c4695d8

Browse files
feat: Add ancestors method to Term class and update related tests
1 parent 33bca3b commit c4695d8

File tree

6 files changed

+76
-10
lines changed

6 files changed

+76
-10
lines changed

src/lib/term.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,28 @@ export class Term {
2525
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').locales();
2626
*/
2727
async locales<T>(): Promise<T> {
28-
const urlPath = `/taxonomy-manager/${this._taxonomyUid}/terms/${this._termUid}/locales`;
29-
const response = await getData(this._client, urlPath);
28+
const response = await getData(this._client, `${this._urlPath}/locales`);
3029
if (response.locales) return response.locales as T;
3130
return response;
3231
}
3332

33+
/**
34+
* @method ancestors
35+
* @memberof Term
36+
* @description Fetches ancestors for the term
37+
* @returns {Promise<T>}
38+
* @example
39+
* import contentstack from '@contentstack/delivery-sdk'
40+
*
41+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
42+
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').ancestors();
43+
*/
44+
async ancestors<T>(): Promise<T> {
45+
const response = await getData(this._client, `${this._urlPath}/ancestors`);
46+
if (response.ancestors) return response.ancestors as T;
47+
return response;
48+
}
49+
3450
async fetch<T>(): Promise<T> {
3551
const response = await getData(this._client, this._urlPath);
3652

test/api/taxonomy.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ describe('ContentType API test cases', () => {
1616
});
1717

1818
it('should give a single taxonomy when taxonomy method is called with taxonomyUid', async () => {
19-
const result = await makeTaxonomy('taxonomy_testing').fetch<TTaxonomy>();
19+
const result = await makeTaxonomy('taxonomy_testing_3').fetch<TTaxonomy>();
2020
expect(result).toBeDefined();
2121
});
2222
});

test/api/term-query.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const stack = stackInstance();
66

77
describe("Terms API test cases", () => {
88
it("should check for terms is defined", async () => {
9-
const result = await makeTerms("taxonomy_testing").find<TTerm>();
9+
const result = await makeTerms("taxonomy_testing_3").find<TTerm>();
1010
if (result.terms) {
1111
expect(result.terms).toBeDefined();
1212
expect(result.terms[0].taxonomy_uid).toBeDefined();

test/api/term.spec.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Term } from "../../src/lib/term";
22
import { stackInstance } from "../utils/stack-instance";
3-
import { TTerm } from "./types";
3+
import { TTerm, TTerms } from "./types";
44

55
const stack = stackInstance();
66

77
describe("Terms API test cases", () => {
88
it("should get a term by uid", async () => {
9-
const result = await makeTerms("term1").fetch<TTerm>();
9+
const result = await makeTerms("vehicles").fetch<TTerm>();
1010
expect(result).toBeDefined();
1111
expect(result.taxonomy_uid).toBeDefined();
1212
expect(result.uid).toBeDefined();
@@ -15,14 +15,21 @@ describe("Terms API test cases", () => {
1515
});
1616

1717
it("should get locales for a term", async () => {
18-
// const result = await makeTerms("term1").locales().fetch();
18+
// const result = await makeTerms("vehicles").locales().fetch();
1919
// API under building phase, so it should throw error
20-
expect(async () => await makeTerms("term1").locales()).rejects.toThrow();
20+
expect(async () => await makeTerms("vehicles").locales()).rejects.toThrow();
2121
// TODO: add assertions
2222
});
23+
24+
it("should get ancestors for a term", async () => {
25+
const result = await makeTerms("sleeper").ancestors<TTerms>();
26+
expect(result).toBeDefined();
27+
expect(result.terms).toBeDefined();
28+
expect(result.terms[0].name).toBeDefined();
29+
});
2330
});
2431

2532
function makeTerms(termUid = ""): Term {
26-
const terms = stack.taxonomy("taxonomy_testing").term(termUid);
33+
const terms = stack.taxonomy("taxonomy_testing_3").term(termUid);
2734
return terms;
2835
}

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, termLocalesResponseDataMock } from '../utils/mocks';
3+
import { termQueryFindResponseDataMock, termLocalesResponseDataMock, termAncestorsResponseDataMock } 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';
@@ -32,4 +32,11 @@ describe('Term class', () => {
3232
const response = await term.locales();
3333
expect(response).toEqual(termLocalesResponseDataMock.terms);
3434
});
35+
36+
it('should fetch ancestors for a term when ancestors() is called', async () => {
37+
mockClient.onGet('/taxonomy-manager/taxonomy_testing/terms/term1/ancestors').reply(200, termAncestorsResponseDataMock);
38+
39+
const response = await term.ancestors();
40+
expect(response).toEqual(termAncestorsResponseDataMock);
41+
});
3542
});

test/utils/mocks.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,6 +1703,41 @@ const termLocalesResponseDataMock = {
17031703
terms: []
17041704
}
17051705

1706+
const termAncestorsResponseDataMock = {
1707+
"terms": [
1708+
{
1709+
"uid": "vehicles",
1710+
"name": "vehicles",
1711+
"publish_details": {
1712+
"time": "2025-10-28T06:54:12.505Z",
1713+
"user": "user",
1714+
"environment": "environment",
1715+
"locale": "en-us"
1716+
}
1717+
},
1718+
{
1719+
"uid": "buses",
1720+
"name": "buses",
1721+
"publish_details": {
1722+
"time": "2025-10-28T06:54:12.514Z",
1723+
"user": "user",
1724+
"environment": "environment",
1725+
"locale": "en-us"
1726+
}
1727+
},
1728+
{
1729+
"uid": "vrl",
1730+
"name": "vrl",
1731+
"publish_details": {
1732+
"time": "2025-10-28T06:54:12.570Z",
1733+
"user": "user",
1734+
"environment": "environment",
1735+
"locale": "en-us"
1736+
}
1737+
}
1738+
]
1739+
}
1740+
17061741
const termQueryFindResponseDataMock = {
17071742
"terms": [
17081743
{
@@ -1749,4 +1784,5 @@ export {
17491784
taxonomyFindResponseDataMock,
17501785
termQueryFindResponseDataMock,
17511786
termLocalesResponseDataMock,
1787+
termAncestorsResponseDataMock,
17521788
};

0 commit comments

Comments
 (0)