|
| 1 | +import { RelationshipSchema } from "@/entities/schema/types"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { generateRelationshipSchema } from "../../../../../tests/fake/schema"; |
| 4 | +import { getRelationshipsVisibleInTab } from "./get-relationships-visible-in-tab"; |
| 5 | + |
| 6 | +describe("getRelationshipsVisibleInTab", () => { |
| 7 | + it("should return relationships with kind Generic and cardinality many", () => { |
| 8 | + // GIVEN |
| 9 | + const relationships = [generateRelationshipSchema({ kind: "Generic", cardinality: "many" })]; |
| 10 | + |
| 11 | + // WHEN |
| 12 | + const result = getRelationshipsVisibleInTab(relationships); |
| 13 | + |
| 14 | + // THEN |
| 15 | + expect(result).toEqual(relationships); |
| 16 | + }); |
| 17 | + |
| 18 | + it("should return relationships with kind Component and cardinality many", () => { |
| 19 | + // GIVEN |
| 20 | + const relationships = [generateRelationshipSchema({ kind: "Component", cardinality: "many" })]; |
| 21 | + |
| 22 | + // WHEN |
| 23 | + const result = getRelationshipsVisibleInTab(relationships); |
| 24 | + |
| 25 | + // THEN |
| 26 | + expect(result).toEqual(relationships); |
| 27 | + }); |
| 28 | + |
| 29 | + it("should return relationships with kind Hierarchy and cardinality many", () => { |
| 30 | + // GIVEN |
| 31 | + const relationships = [generateRelationshipSchema({ kind: "Hierarchy", cardinality: "many" })]; |
| 32 | + |
| 33 | + // WHEN |
| 34 | + const result = getRelationshipsVisibleInTab(relationships); |
| 35 | + |
| 36 | + // THEN |
| 37 | + expect(result).toEqual(relationships); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should return relationships with kind Template and cardinality many", () => { |
| 41 | + // GIVEN |
| 42 | + const relationships = [generateRelationshipSchema({ kind: "Template", cardinality: "many" })]; |
| 43 | + |
| 44 | + // WHEN |
| 45 | + const result = getRelationshipsVisibleInTab(relationships); |
| 46 | + |
| 47 | + // THEN |
| 48 | + expect(result).toEqual(relationships); |
| 49 | + }); |
| 50 | + |
| 51 | + it("should not return relationships with cardinality one regardless of kind", () => { |
| 52 | + // GIVEN |
| 53 | + const relationships = [ |
| 54 | + generateRelationshipSchema({ kind: "Attribute", cardinality: "one" }), |
| 55 | + generateRelationshipSchema({ kind: "Generic", cardinality: "one" }), |
| 56 | + generateRelationshipSchema({ kind: "Component", cardinality: "one" }), |
| 57 | + generateRelationshipSchema({ kind: "Group", cardinality: "one" }), |
| 58 | + generateRelationshipSchema({ kind: "Hierarchy", cardinality: "one" }), |
| 59 | + generateRelationshipSchema({ kind: "Parent", cardinality: "one" }), |
| 60 | + generateRelationshipSchema({ kind: "Profile", cardinality: "one" }), |
| 61 | + generateRelationshipSchema({ kind: "Template", cardinality: "one" }), |
| 62 | + ]; |
| 63 | + |
| 64 | + // WHEN |
| 65 | + const result = getRelationshipsVisibleInTab(relationships); |
| 66 | + |
| 67 | + // THEN |
| 68 | + expect(result).toEqual([]); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should not return relationships with kinds not in the visible list", () => { |
| 72 | + // GIVEN |
| 73 | + const relationships = [ |
| 74 | + generateRelationshipSchema({ kind: "Attribute", cardinality: "many" }), |
| 75 | + generateRelationshipSchema({ kind: "Parent", cardinality: "many" }), |
| 76 | + generateRelationshipSchema({ kind: "Group", cardinality: "many" }), |
| 77 | + generateRelationshipSchema({ kind: "Profile", cardinality: "many" }), |
| 78 | + ]; |
| 79 | + |
| 80 | + // WHEN |
| 81 | + const result = getRelationshipsVisibleInTab(relationships); |
| 82 | + |
| 83 | + // THEN |
| 84 | + expect(result).toEqual([]); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should handle mixed relationships correctly", () => { |
| 88 | + // GIVEN |
| 89 | + const visibleRelationships = [ |
| 90 | + generateRelationshipSchema({ kind: "Generic", cardinality: "many" }), |
| 91 | + generateRelationshipSchema({ kind: "Component", cardinality: "many" }), |
| 92 | + ]; |
| 93 | + const invisibleRelationships = [ |
| 94 | + generateRelationshipSchema({ kind: "Generic", cardinality: "one" }), |
| 95 | + generateRelationshipSchema({ kind: "Attribute", cardinality: "many" }), |
| 96 | + ]; |
| 97 | + const relationships = [...visibleRelationships, ...invisibleRelationships]; |
| 98 | + |
| 99 | + // WHEN |
| 100 | + const result = getRelationshipsVisibleInTab(relationships); |
| 101 | + |
| 102 | + // THEN |
| 103 | + expect(result).toEqual(visibleRelationships); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should return empty array for empty input", () => { |
| 107 | + // GIVEN |
| 108 | + const relationships: RelationshipSchema[] = []; |
| 109 | + |
| 110 | + // WHEN |
| 111 | + const result = getRelationshipsVisibleInTab(relationships); |
| 112 | + |
| 113 | + // THEN |
| 114 | + expect(result).toEqual([]); |
| 115 | + }); |
| 116 | + |
| 117 | + it("should sort relationships by order_weight", () => { |
| 118 | + // GIVEN |
| 119 | + const templateRel = generateRelationshipSchema({ |
| 120 | + kind: "Template", |
| 121 | + cardinality: "many", |
| 122 | + order_weight: 0, |
| 123 | + }); |
| 124 | + const componentRel = generateRelationshipSchema({ |
| 125 | + kind: "Component", |
| 126 | + cardinality: "many", |
| 127 | + order_weight: 1, |
| 128 | + }); |
| 129 | + const genericRel = generateRelationshipSchema({ |
| 130 | + kind: "Generic", |
| 131 | + cardinality: "many", |
| 132 | + order_weight: 2, |
| 133 | + }); |
| 134 | + const relationships = [genericRel, componentRel, templateRel]; |
| 135 | + |
| 136 | + // WHEN |
| 137 | + const result = getRelationshipsVisibleInTab(relationships); |
| 138 | + |
| 139 | + // THEN |
| 140 | + expect(result).toEqual([templateRel, componentRel, genericRel]); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should handle undefined order_weight as 0", () => { |
| 144 | + // GIVEN |
| 145 | + const templateRel = generateRelationshipSchema({ |
| 146 | + kind: "Template", |
| 147 | + cardinality: "many", |
| 148 | + order_weight: -1, |
| 149 | + }); |
| 150 | + const componentRel = generateRelationshipSchema({ |
| 151 | + kind: "Component", |
| 152 | + cardinality: "many", |
| 153 | + order_weight: undefined, |
| 154 | + }); |
| 155 | + const genericRel = generateRelationshipSchema({ |
| 156 | + kind: "Generic", |
| 157 | + cardinality: "many", |
| 158 | + order_weight: 1, |
| 159 | + }); |
| 160 | + const relationships = [genericRel, componentRel, templateRel]; |
| 161 | + |
| 162 | + // WHEN |
| 163 | + const result = getRelationshipsVisibleInTab(relationships); |
| 164 | + |
| 165 | + // THEN |
| 166 | + expect(result).toEqual([templateRel, componentRel, genericRel]); |
| 167 | + }); |
| 168 | +}); |
0 commit comments