|
| 1 | +import { NodeCore } from "@/entities/nodes/types"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { generateNodeSchema } from "../../../../../tests/fake/schema"; |
| 4 | +import { getNodeLabel } from "./get-node-label"; |
| 5 | + |
| 6 | +describe("getNodeLabel", () => { |
| 7 | + const baseSchema = generateNodeSchema(); |
| 8 | + const baseNode: NodeCore = { |
| 9 | + id: "test-id", |
| 10 | + hfid: null, |
| 11 | + display_label: null, |
| 12 | + __typename: "TestKind", |
| 13 | + }; |
| 14 | + |
| 15 | + it("should join multiple hfids with comma when node has hfid array", () => { |
| 16 | + // GIVEN |
| 17 | + const node = { ...baseNode, hfid: ["hfid-1", "hfid-2"] }; |
| 18 | + const schema = baseSchema; |
| 19 | + |
| 20 | + // WHEN |
| 21 | + const result = getNodeLabel({ node, schema }); |
| 22 | + |
| 23 | + // THEN |
| 24 | + expect(result).toBe("hfid-1, hfid-2"); |
| 25 | + }); |
| 26 | + |
| 27 | + it("should use display_label when hfid is not present", () => { |
| 28 | + // GIVEN |
| 29 | + const node = { ...baseNode, display_label: "Display Label" }; |
| 30 | + const schema = baseSchema; |
| 31 | + |
| 32 | + // WHEN |
| 33 | + const result = getNodeLabel({ node, schema }); |
| 34 | + |
| 35 | + // THEN |
| 36 | + expect(result).toBe("Display Label"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("should fallback to node.id when neither hfid nor display_label are present", () => { |
| 40 | + // WHEN |
| 41 | + const result = getNodeLabel({ node: baseNode, schema: baseSchema }); |
| 42 | + |
| 43 | + // THEN |
| 44 | + expect(result).toBe("test-id"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("should prefer hfid over display_label when both are available", () => { |
| 48 | + // GIVEN |
| 49 | + const node = { |
| 50 | + ...baseNode, |
| 51 | + hfid: ["hfid-1"], |
| 52 | + display_label: "Display Label", |
| 53 | + }; |
| 54 | + const schema = baseSchema; |
| 55 | + |
| 56 | + // WHEN |
| 57 | + const result = getNodeLabel({ node, schema }); |
| 58 | + |
| 59 | + // THEN |
| 60 | + expect(result).toBe("hfid-1"); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should fallback to node.id when schema allows special labels but node values are null", () => { |
| 64 | + // GIVEN |
| 65 | + const schema = baseSchema; |
| 66 | + |
| 67 | + // WHEN |
| 68 | + const result = getNodeLabel({ node: baseNode, schema }); |
| 69 | + |
| 70 | + // THEN |
| 71 | + expect(result).toBe("test-id"); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should use node.id when schema disables special labels even if node has them", () => { |
| 75 | + // GIVEN |
| 76 | + const node = { |
| 77 | + ...baseNode, |
| 78 | + hfid: ["hfid-1"], |
| 79 | + display_label: "Display Label", |
| 80 | + }; |
| 81 | + const schema = { |
| 82 | + ...baseSchema, |
| 83 | + human_friendly_id: null, |
| 84 | + display_labels: null, |
| 85 | + }; |
| 86 | + |
| 87 | + // WHEN |
| 88 | + const result = getNodeLabel({ node, schema: schema }); |
| 89 | + |
| 90 | + // THEN |
| 91 | + expect(result).toBe("test-id"); |
| 92 | + }); |
| 93 | +}); |
0 commit comments