|
| 1 | +import { describe, expect, it } from "vitest"; |
1 | 2 | import recordsCardUtils from "@/utils/recordsCardUtils"; |
2 | 3 |
|
3 | 4 | const utils = recordsCardUtils; |
@@ -35,6 +36,10 @@ describe("recordsCardsUtils.js", function () { |
35 | 36 | type: "objectTypes", |
36 | 37 | }; |
37 | 38 | expect(utils.methods.getChipColor(chip)).toEqual("object_type_color"); |
| 39 | + chip = { |
| 40 | + type: "xyz", |
| 41 | + }; |
| 42 | + expect(utils.methods.getChipColor(chip)).toEqual("object_type_color"); |
38 | 43 | }); |
39 | 44 |
|
40 | 45 | it("capitalizes text", () => { |
@@ -127,6 +132,102 @@ describe("recordsCardsUtils.js", function () { |
127 | 132 | expect(utils.methods.chips).toStrictEqual(result); |
128 | 133 | }); |
129 | 134 |
|
| 135 | + describe("setChips", () => { |
| 136 | + let context = { |
| 137 | + chips: [], |
| 138 | + remainTagCount: 0, |
| 139 | + getMaxItemShown: 2, |
| 140 | + }; |
| 141 | + it("aggregates chips from multiple categories in the correct order", () => { |
| 142 | + context.getMaxItemShown = 5; |
| 143 | + const record = { |
| 144 | + subjects: [{ name: "S1" }], |
| 145 | + domains: [{ name: "D1" }], |
| 146 | + }; |
| 147 | + |
| 148 | + context.setChips(record); |
| 149 | + |
| 150 | + expect(context.chips).toHaveLength(2); |
| 151 | + expect(context.chips[0].type).toBe("subjects"); |
| 152 | + expect(context.chips[1].type).toBe("domains"); |
| 153 | + }); |
| 154 | + |
| 155 | + it("calculates the global remainTagCount correctly", () => { |
| 156 | + // Setup: Limit is 1 per category |
| 157 | + context.getMaxItemShown = 1; |
| 158 | + |
| 159 | + const record = { |
| 160 | + objectTypes: [{ name: "O1" }, { name: "O2" }], |
| 161 | + subjects: [{ name: "S1" }, { name: "S2" }, { name: "S3" }], |
| 162 | + }; |
| 163 | + |
| 164 | + utils.methods.setChips(record); |
| 165 | + expect(context.remainTagCount).toBe(3); |
| 166 | + expect(context.chips).toHaveLength(2); |
| 167 | + }); |
| 168 | + |
| 169 | + it("resets chips and counts before processing", () => { |
| 170 | + // Setup: Dirty state |
| 171 | + context.chips = ["old_garbage"]; |
| 172 | + context.remainTagCount = 999; |
| 173 | + |
| 174 | + const record = { objectTypes: [{ name: "New" }] }; |
| 175 | + context.getMaxItemShown = 5; |
| 176 | + |
| 177 | + context.setChips(record); |
| 178 | + |
| 179 | + expect(context.chips).toHaveLength(1); |
| 180 | + expect(context.chips[0].name).toBe("New"); |
| 181 | + expect(context.remainTagCount).toBe(0); |
| 182 | + }); |
| 183 | + }); |
| 184 | + |
| 185 | + describe("organizeChips", () => { |
| 186 | + let context = { |
| 187 | + // Data properties |
| 188 | + chips: [], |
| 189 | + remainTagCount: 0, |
| 190 | + getMaxItemShown: 2, // Arbitrary limit for testing |
| 191 | + }; |
| 192 | + it("pushes items to chips array if they are within the limit", () => { |
| 193 | + const record = { |
| 194 | + subjects: [{ name: "Math" }, { name: "Science" }], |
| 195 | + }; |
| 196 | + const result = utils.methods.organizeChips(record, "subjects", 2); |
| 197 | + |
| 198 | + expect(result).toBe(true); |
| 199 | + expect(context.chips).toHaveLength(2); |
| 200 | + expect(context.chips[0]).toMatchObject({ |
| 201 | + name: "Math", |
| 202 | + type: "subjects", |
| 203 | + }); |
| 204 | + expect(context.chips[1]).toMatchObject({ |
| 205 | + name: "Science", |
| 206 | + type: "subjects", |
| 207 | + }); |
| 208 | + }); |
| 209 | + |
| 210 | + it("increments remainTagCount on the record if items exceed limit", () => { |
| 211 | + const record = { |
| 212 | + subjects: [{ name: "1" }, { name: "2" }, { name: "3" }, { name: "4" }], |
| 213 | + }; |
| 214 | + record.subjects.remainTagCount = 0; |
| 215 | + |
| 216 | + utils.methods.organizeChips(record, "subjects", 2); |
| 217 | + |
| 218 | + expect(context.chips).toHaveLength(2); |
| 219 | + expect(record.subjects.remainTagCount).toBe(2); |
| 220 | + }); |
| 221 | + |
| 222 | + it("returns false if the node does not exist in record", () => { |
| 223 | + const record = {}; |
| 224 | + const result = utils.methods.organizeChips(record, "missing_node", 5); |
| 225 | + |
| 226 | + expect(result).toBe(false); |
| 227 | + expect(context.chips).toHaveLength(0); |
| 228 | + }); |
| 229 | + }); |
| 230 | + |
130 | 231 | it("truncates strings", function () { |
131 | 232 | expect(utils.methods.truncateString(null, 1)).toBe(null); |
132 | 233 | expect(utils.methods.truncateString("", 1)).toEqual(""); |
|
0 commit comments