Skip to content

Commit c63f9ca

Browse files
swisskyascorbic
andauthored
fix(core): include term description for flat taxonomies in getTaxonomyTerms() (#1420)
The non-hierarchical branch dropped the already-fetched data column when mapping rows to TaxonomyTerm; only buildTree (hierarchical) parsed it. Closes #1419 Co-authored-by: Matt Kane <mkane@cloudflare.com>
1 parent 4809661 commit c63f9ca

3 files changed

Lines changed: 79 additions & 0 deletions

File tree

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
"emdash": patch
3+
---
4+
5+
`getTaxonomyTerms()` now returns the term `description` for flat
6+
(non-hierarchical) taxonomies (#1419)
7+
8+
The query already fetched the `data` column, but the non-hierarchical branch
9+
dropped it when mapping rows to `TaxonomyTerm` — only hierarchical taxonomies
10+
(via `buildTree`) parsed the description. Descriptions set in the admin UI are
11+
now returned for both kinds of taxonomies.

packages/core/src/taxonomies/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ export async function getTaxonomyTerms(
148148
name: term.name,
149149
slug: term.slug,
150150
label: term.label,
151+
description: term.data ? JSON.parse(term.data).description : undefined,
151152
children: [],
152153
count: counts.get(term.translation_group ?? term.id) ?? 0,
153154
locale: term.locale,
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { afterEach, beforeEach, expect, it, vi } from "vitest";
2+
3+
import { TaxonomyRepository } from "../../../src/database/repositories/taxonomy.js";
4+
import {
5+
describeEachDialect,
6+
setupForDialectWithCollections,
7+
teardownForDialect,
8+
type DialectTestContext,
9+
} from "../../utils/test-db.js";
10+
11+
// Mock loader.getDb so the runtime taxonomy functions read from our test db.
12+
vi.mock("../../../src/loader.js", () => ({
13+
getDb: vi.fn(),
14+
}));
15+
16+
import { getDb } from "../../../src/loader.js";
17+
import { getTaxonomyTerms, invalidateTermCache } from "../../../src/taxonomies/index.js";
18+
19+
describeEachDialect("getTaxonomyTerms", (dialect) => {
20+
let ctx: DialectTestContext;
21+
let taxRepo: TaxonomyRepository;
22+
23+
beforeEach(async () => {
24+
ctx = await setupForDialectWithCollections(dialect);
25+
taxRepo = new TaxonomyRepository(ctx.db);
26+
vi.mocked(getDb).mockResolvedValue(ctx.db);
27+
invalidateTermCache();
28+
});
29+
30+
afterEach(async () => {
31+
invalidateTermCache();
32+
await teardownForDialect(ctx);
33+
vi.restoreAllMocks();
34+
});
35+
36+
it("includes the description for flat (non-hierarchical) taxonomies", async () => {
37+
// `tag` is seeded as non-hierarchical.
38+
await taxRepo.create({
39+
name: "tag",
40+
slug: "longevity",
41+
label: "Longevity",
42+
data: { description: "Healthy aging. wikidata:Q380274" },
43+
});
44+
await taxRepo.create({ name: "tag", slug: "wellness", label: "Wellness" });
45+
46+
const terms = await getTaxonomyTerms("tag");
47+
48+
expect(terms.map((t) => t.slug)).toEqual(["longevity", "wellness"]);
49+
expect(terms[0].description).toBe("Healthy aging. wikidata:Q380274");
50+
expect(terms[1].description).toBeUndefined();
51+
});
52+
53+
it("includes the description for hierarchical taxonomies (control)", async () => {
54+
// `category` is seeded as hierarchical.
55+
await taxRepo.create({
56+
name: "category",
57+
slug: "tech",
58+
label: "Technology",
59+
data: { description: "All things tech" },
60+
});
61+
62+
const terms = await getTaxonomyTerms("category");
63+
64+
expect(terms).toHaveLength(1);
65+
expect(terms[0].description).toBe("All things tech");
66+
});
67+
});

0 commit comments

Comments
 (0)