|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { flatten } from "flat"; |
| 3 | +import createFlatLoader, { |
| 4 | + buildDenormalizedKeysMap, |
| 5 | + denormalizeObjectKeys, |
| 6 | + mapDenormalizedKeys, |
| 7 | + normalizeObjectKeys, |
| 8 | + OBJECT_NUMERIC_KEY_PREFIX, |
| 9 | +} from "./flat"; |
| 10 | + |
| 11 | +describe("flat loader", () => { |
| 12 | + describe("createFlatLoader", () => { |
| 13 | + it("loads numeric object and array and preserves state", async () => { |
| 14 | + const loader = createFlatLoader(); |
| 15 | + loader.setDefaultLocale("en"); |
| 16 | + await loader.pull("en", { |
| 17 | + messages: { "1": "foo", "2": "bar" }, |
| 18 | + years: ["January 13, 2025", "February 14, 2025"], |
| 19 | + }); |
| 20 | + await loader.pull("en", {}); // run again to ensure state is preserved |
| 21 | + const output = await loader.push("en", { |
| 22 | + "messages/1": "foo", |
| 23 | + "messages/2": "bar", |
| 24 | + "years/0": "January 13, 2025", |
| 25 | + "years/1": "February 14, 2025", |
| 26 | + }); |
| 27 | + expect(output).toEqual({ |
| 28 | + messages: { "1": "foo", "2": "bar" }, |
| 29 | + years: ["January 13, 2025", "February 14, 2025"], |
| 30 | + }); |
| 31 | + }); |
| 32 | + }); |
| 33 | + |
| 34 | + describe("helper functions", () => { |
| 35 | + const inputObj = { |
| 36 | + messages: { |
| 37 | + "1": "a", |
| 38 | + "2": "b", |
| 39 | + }, |
| 40 | + }; |
| 41 | + const inputArray = { |
| 42 | + messages: ["a", "b", "c"], |
| 43 | + }; |
| 44 | + |
| 45 | + describe("denormalizeObjectKeys", () => { |
| 46 | + it("should denormalize object keys", () => { |
| 47 | + const output = denormalizeObjectKeys(inputObj); |
| 48 | + expect(output).toEqual({ |
| 49 | + messages: { |
| 50 | + [`${OBJECT_NUMERIC_KEY_PREFIX}1`]: "a", |
| 51 | + [`${OBJECT_NUMERIC_KEY_PREFIX}2`]: "b", |
| 52 | + }, |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it("should preserve array", () => { |
| 57 | + const output = denormalizeObjectKeys(inputArray); |
| 58 | + expect(output).toEqual({ |
| 59 | + messages: ["a", "b", "c"], |
| 60 | + }); |
| 61 | + }); |
| 62 | + }); |
| 63 | + |
| 64 | + describe("buildDenormalizedKeysMap", () => { |
| 65 | + it("should build normalized keys map", () => { |
| 66 | + const denormalized: Record<string, string> = flatten(denormalizeObjectKeys(inputObj), { delimiter: "/" }); |
| 67 | + const output = buildDenormalizedKeysMap(denormalized); |
| 68 | + expect(output).toEqual({ |
| 69 | + "messages/1": `messages/${OBJECT_NUMERIC_KEY_PREFIX}1`, |
| 70 | + "messages/2": `messages/${OBJECT_NUMERIC_KEY_PREFIX}2`, |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("should build keys map array", () => { |
| 75 | + const denormalized: Record<string, string> = flatten(denormalizeObjectKeys(inputArray), { delimiter: "/" }); |
| 76 | + const output = buildDenormalizedKeysMap(denormalized); |
| 77 | + expect(output).toEqual({ |
| 78 | + "messages/0": "messages/0", |
| 79 | + "messages/1": "messages/1", |
| 80 | + "messages/2": "messages/2", |
| 81 | + }); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + describe("normalizeObjectKeys", () => { |
| 86 | + it("should normalize denormalized object keys", () => { |
| 87 | + const output = normalizeObjectKeys(denormalizeObjectKeys(inputObj)); |
| 88 | + expect(output).toEqual(inputObj); |
| 89 | + }); |
| 90 | + |
| 91 | + it("should process array keys", () => { |
| 92 | + const output = normalizeObjectKeys(denormalizeObjectKeys(inputArray)); |
| 93 | + expect(output).toEqual(inputArray); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe("mapDeormalizedKeys", () => { |
| 98 | + it("should map normalized keys", () => { |
| 99 | + const denormalized: Record<string, string> = flatten(denormalizeObjectKeys(inputObj), { delimiter: "/" }); |
| 100 | + const keyMap = buildDenormalizedKeysMap(denormalized); |
| 101 | + const flattened: Record<string, string> = flatten(inputObj, { delimiter: "/" }); |
| 102 | + const mapped = mapDenormalizedKeys(flattened, keyMap); |
| 103 | + expect(mapped).toEqual(denormalized); |
| 104 | + }); |
| 105 | + |
| 106 | + it("should map array", () => { |
| 107 | + const denormalized: Record<string, string> = flatten(denormalizeObjectKeys(inputArray), { delimiter: "/" }); |
| 108 | + const keyMap = buildDenormalizedKeysMap(denormalized); |
| 109 | + const flattened: Record<string, string> = flatten(inputArray, { delimiter: "/" }); |
| 110 | + const mapped = mapDenormalizedKeys(flattened, keyMap); |
| 111 | + expect(mapped).toEqual(denormalized); |
| 112 | + }); |
| 113 | + }); |
| 114 | + }); |
| 115 | +}); |
0 commit comments