|
| 1 | +import { describe, it, expect } from "vitest"; |
| 2 | + |
| 3 | +import createJsonSortingLoader from "./json-sorting"; |
| 4 | + |
| 5 | +describe("JSON Sorting Loader", () => { |
| 6 | + const loader = createJsonSortingLoader(); |
| 7 | + loader.setDefaultLocale("en"); |
| 8 | + |
| 9 | + describe("pull", () => { |
| 10 | + it("should return input unchanged", async () => { |
| 11 | + const input = { b: 1, a: 2 }; |
| 12 | + const result = await loader.pull("en", input); |
| 13 | + expect(result).toEqual(input); |
| 14 | + }); |
| 15 | + }); |
| 16 | + |
| 17 | + describe("push", () => { |
| 18 | + it("should sort object keys at root level", async () => { |
| 19 | + const input = { zebra: 1, apple: 2, banana: 3 }; |
| 20 | + const expected = { apple: 2, banana: 3, zebra: 1 }; |
| 21 | + |
| 22 | + const result = await loader.push("en", input); |
| 23 | + expect(result).toEqual(expected); |
| 24 | + }); |
| 25 | + |
| 26 | + it("should sort nested object keys", async () => { |
| 27 | + const input = { |
| 28 | + b: { |
| 29 | + z: 1, |
| 30 | + y: 2, |
| 31 | + x: 3, |
| 32 | + }, |
| 33 | + a: 1, |
| 34 | + }; |
| 35 | + const expected = { |
| 36 | + a: 1, |
| 37 | + b: { |
| 38 | + x: 3, |
| 39 | + y: 2, |
| 40 | + z: 1, |
| 41 | + }, |
| 42 | + }; |
| 43 | + |
| 44 | + const result = await loader.push("en", input); |
| 45 | + expect(result).toEqual(expected); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should handle arrays by sorting their object elements", async () => { |
| 49 | + const input = { |
| 50 | + items: [ |
| 51 | + { b: 1, a: 2 }, |
| 52 | + { d: 3, c: 4 }, |
| 53 | + ], |
| 54 | + }; |
| 55 | + const expected = { |
| 56 | + items: [ |
| 57 | + { a: 2, b: 1 }, |
| 58 | + { c: 4, d: 3 }, |
| 59 | + ], |
| 60 | + }; |
| 61 | + |
| 62 | + const result = await loader.push("en", input); |
| 63 | + expect(result).toEqual(expected); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should handle mixed nested structures", async () => { |
| 67 | + const input = { |
| 68 | + zebra: [ |
| 69 | + { beta: 2, alpha: 1 }, |
| 70 | + { delta: 4, gamma: 3 }, |
| 71 | + ], |
| 72 | + apple: { |
| 73 | + two: 2, |
| 74 | + one: 1, |
| 75 | + }, |
| 76 | + }; |
| 77 | + const expected = { |
| 78 | + apple: { |
| 79 | + one: 1, |
| 80 | + two: 2, |
| 81 | + }, |
| 82 | + zebra: [ |
| 83 | + { alpha: 1, beta: 2 }, |
| 84 | + { delta: 4, gamma: 3 }, |
| 85 | + ], |
| 86 | + }; |
| 87 | + |
| 88 | + const result = await loader.push("en", input); |
| 89 | + expect(result).toEqual(expected); |
| 90 | + }); |
| 91 | + |
| 92 | + it("should handle null and primitive values", async () => { |
| 93 | + const input = { |
| 94 | + c: null, |
| 95 | + b: "string", |
| 96 | + a: 123, |
| 97 | + d: true, |
| 98 | + }; |
| 99 | + const expected = { |
| 100 | + a: 123, |
| 101 | + b: "string", |
| 102 | + c: null, |
| 103 | + d: true, |
| 104 | + }; |
| 105 | + |
| 106 | + const result = await loader.push("en", input); |
| 107 | + expect(result).toEqual(expected); |
| 108 | + }); |
| 109 | + }); |
| 110 | +}); |
0 commit comments