Skip to content

Commit 4e97212

Browse files
feat: Add descendants method to Term class and update related tests
1 parent 7a8b8a4 commit 4e97212

File tree

6 files changed

+123
-6
lines changed

6 files changed

+123
-6
lines changed

src/lib/term.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,26 @@ export class Term {
4747
return response;
4848
}
4949

50+
/**
51+
* @method descendants
52+
* @memberof Term
53+
* @description Fetches descendants for the term
54+
* @returns {Promise<T>}
55+
* @example
56+
* import contentstack from '@contentstack/delivery-sdk'
57+
*
58+
* const stack = contentstack.stack({ apiKey: "apiKey", deliveryToken: "deliveryToken", environment: "environment" });
59+
* const result = await stack.taxonomy('taxonomy_uid').term('term_uid').descendants();
60+
*/
61+
async descendants<T>(): Promise<T> {
62+
const response = await getData(this._client, `${this._urlPath}/descendants`);
63+
if (response.descendants) return response.descendants as T;
64+
return response;
65+
}
66+
5067
async fetch<T>(): Promise<T> {
5168
const response = await getData(this._client, this._urlPath);
52-
5369
if (response.term) return response.term as T;
54-
5570
return response;
5671
}
5772
}

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_3').fetch<TTaxonomy>();
19+
const result = await makeTaxonomy('taxonomy_testing').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_3").find<TTerm>();
9+
const result = await makeTerms("taxonomy_testing").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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,16 @@ describe("Terms API test cases", () => {
2727
expect(result.terms).toBeDefined();
2828
expect(result.terms[0].name).toBeDefined();
2929
});
30+
31+
it("should get descendants for a term", async () => {
32+
const result = await makeTerms("vrl").descendants<TTerms>();
33+
expect(result).toBeDefined();
34+
expect(result.terms).toBeDefined();
35+
expect(result.terms[0].name).toBeDefined();
36+
});
3037
});
3138

3239
function makeTerms(termUid = ""): Term {
33-
const terms = stack.taxonomy("taxonomy_testing_3").term(termUid);
40+
const terms = stack.taxonomy("taxonomy_testing").term(termUid);
3441
return terms;
3542
}

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, termAncestorsResponseDataMock } from '../utils/mocks';
3+
import { termQueryFindResponseDataMock, termLocalesResponseDataMock, termAncestorsResponseDataMock, termDescendantsResponseDataMock } 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';
@@ -39,4 +39,11 @@ describe('Term class', () => {
3939
const response = await term.ancestors();
4040
expect(response).toEqual(termAncestorsResponseDataMock);
4141
});
42+
43+
it('should fetch descendants for a term when descendants() is called', async () => {
44+
mockClient.onGet('/taxonomy-manager/taxonomy_testing/terms/term1/descendants').reply(200, termDescendantsResponseDataMock);
45+
46+
const response = await term.descendants();
47+
expect(response).toEqual(termDescendantsResponseDataMock);
48+
});
4249
});

test/utils/mocks.ts

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1738,6 +1738,93 @@ const termAncestorsResponseDataMock = {
17381738
]
17391739
}
17401740

1741+
const termDescendantsResponseDataMock = {
1742+
"terms": [
1743+
{
1744+
"taxonomy_uid": "taxonomy_testing",
1745+
"uid": "sleeper",
1746+
"ancestors": [
1747+
{
1748+
"uid": "taxonomy_testing",
1749+
"name": "taxonomy_testing",
1750+
"type": "TAXONOMY"
1751+
},
1752+
{
1753+
"uid": "vehicles",
1754+
"name": "vehicles",
1755+
"type": ""
1756+
},
1757+
{
1758+
"uid": "buses",
1759+
"name": "buses",
1760+
"type": ""
1761+
},
1762+
{
1763+
"uid": "vrl",
1764+
"name": "vrl",
1765+
"type": ""
1766+
}
1767+
],
1768+
"name": "sleeper",
1769+
"parent_uid": "vrl",
1770+
"created_by": "created_by",
1771+
"created_at": "2025-10-28T07:58:46.870Z",
1772+
"updated_by": "updated_by",
1773+
"updated_at": "2025-10-28T07:58:46.870Z",
1774+
"children_count": 0,
1775+
"depth": 4,
1776+
"ACL": {},
1777+
"publish_details": {
1778+
"time": "2025-10-28T07:59:12.557Z",
1779+
"user": "user",
1780+
"environment": "environment",
1781+
"locale": "en-us"
1782+
}
1783+
},
1784+
{
1785+
"taxonomy_uid": "taxonomy_testing",
1786+
"uid": "intercity",
1787+
"ancestors": [
1788+
{
1789+
"uid": "taxonomy_testing",
1790+
"name": "taxonomy_testing",
1791+
"type": "TAXONOMY"
1792+
},
1793+
{
1794+
"uid": "vehicles",
1795+
"name": "vehicles",
1796+
"type": ""
1797+
},
1798+
{
1799+
"uid": "buses",
1800+
"name": "buses",
1801+
"type": ""
1802+
},
1803+
{
1804+
"uid": "vrl",
1805+
"name": "vrl",
1806+
"type": ""
1807+
}
1808+
],
1809+
"name": "intercity",
1810+
"parent_uid": "vrl",
1811+
"created_by": "created_by",
1812+
"created_at": "2025-10-28T07:58:46.870Z",
1813+
"updated_by": "updated_by",
1814+
"updated_at": "2025-10-28T07:58:46.870Z",
1815+
"children_count": 0,
1816+
"depth": 4,
1817+
"ACL": {},
1818+
"publish_details": {
1819+
"time": "2025-10-28T07:59:12.565Z",
1820+
"user": "user",
1821+
"environment": "environment",
1822+
"locale": "en-us"
1823+
}
1824+
}
1825+
]
1826+
}
1827+
17411828
const termQueryFindResponseDataMock = {
17421829
"terms": [
17431830
{
@@ -1785,4 +1872,5 @@ export {
17851872
termQueryFindResponseDataMock,
17861873
termLocalesResponseDataMock,
17871874
termAncestorsResponseDataMock,
1875+
termDescendantsResponseDataMock,
17881876
};

0 commit comments

Comments
 (0)