|
1 |
| -import { updateValueAtPath, getValueAtPath } from "../jsonPathUtils"; |
2 |
| -import { JsonValue } from "../../components/DynamicJsonForm"; |
| 1 | +import { |
| 2 | + getDataType, |
| 3 | + tryParseJson, |
| 4 | + updateValueAtPath, |
| 5 | + getValueAtPath, |
| 6 | +} from "../jsonUtils"; |
| 7 | +import type { JsonValue } from "../jsonUtils"; |
| 8 | + |
| 9 | +describe("getDataType", () => { |
| 10 | + test("should return 'string' for string values", () => { |
| 11 | + expect(getDataType("hello")).toBe("string"); |
| 12 | + expect(getDataType("")).toBe("string"); |
| 13 | + }); |
| 14 | + |
| 15 | + test("should return 'number' for number values", () => { |
| 16 | + expect(getDataType(123)).toBe("number"); |
| 17 | + expect(getDataType(0)).toBe("number"); |
| 18 | + expect(getDataType(-10)).toBe("number"); |
| 19 | + expect(getDataType(1.5)).toBe("number"); |
| 20 | + expect(getDataType(NaN)).toBe("number"); |
| 21 | + expect(getDataType(Infinity)).toBe("number"); |
| 22 | + }); |
| 23 | + |
| 24 | + test("should return 'boolean' for boolean values", () => { |
| 25 | + expect(getDataType(true)).toBe("boolean"); |
| 26 | + expect(getDataType(false)).toBe("boolean"); |
| 27 | + }); |
| 28 | + |
| 29 | + test("should return 'undefined' for undefined value", () => { |
| 30 | + expect(getDataType(undefined)).toBe("undefined"); |
| 31 | + }); |
| 32 | + |
| 33 | + test("should return 'object' for object values", () => { |
| 34 | + expect(getDataType({})).toBe("object"); |
| 35 | + expect(getDataType({ key: "value" })).toBe("object"); |
| 36 | + }); |
| 37 | + |
| 38 | + test("should return 'array' for array values", () => { |
| 39 | + expect(getDataType([])).toBe("array"); |
| 40 | + expect(getDataType([1, 2, 3])).toBe("array"); |
| 41 | + expect(getDataType(["a", "b", "c"])).toBe("array"); |
| 42 | + expect(getDataType([{}, { nested: true }])).toBe("array"); |
| 43 | + }); |
| 44 | + |
| 45 | + test("should return 'null' for null value", () => { |
| 46 | + expect(getDataType(null)).toBe("null"); |
| 47 | + }); |
| 48 | +}); |
| 49 | + |
| 50 | +describe("tryParseJson", () => { |
| 51 | + test("should correctly parse valid JSON object", () => { |
| 52 | + const jsonString = '{"name":"test","value":123}'; |
| 53 | + const result = tryParseJson(jsonString); |
| 54 | + |
| 55 | + expect(result.success).toBe(true); |
| 56 | + expect(result.data).toEqual({ name: "test", value: 123 }); |
| 57 | + }); |
| 58 | + |
| 59 | + test("should correctly parse valid JSON array", () => { |
| 60 | + const jsonString = '[1,2,3,"test"]'; |
| 61 | + const result = tryParseJson(jsonString); |
| 62 | + |
| 63 | + expect(result.success).toBe(true); |
| 64 | + expect(result.data).toEqual([1, 2, 3, "test"]); |
| 65 | + }); |
| 66 | + |
| 67 | + test("should correctly parse JSON with whitespace", () => { |
| 68 | + const jsonString = ' { "name" : "test" } '; |
| 69 | + const result = tryParseJson(jsonString); |
| 70 | + |
| 71 | + expect(result.success).toBe(true); |
| 72 | + expect(result.data).toEqual({ name: "test" }); |
| 73 | + }); |
| 74 | + |
| 75 | + test("should correctly parse nested JSON structures", () => { |
| 76 | + const jsonString = |
| 77 | + '{"user":{"name":"test","details":{"age":30}},"items":[1,2,3]}'; |
| 78 | + const result = tryParseJson(jsonString); |
| 79 | + |
| 80 | + expect(result.success).toBe(true); |
| 81 | + expect(result.data).toEqual({ |
| 82 | + user: { |
| 83 | + name: "test", |
| 84 | + details: { |
| 85 | + age: 30, |
| 86 | + }, |
| 87 | + }, |
| 88 | + items: [1, 2, 3], |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + test("should correctly parse empty objects and arrays", () => { |
| 93 | + expect(tryParseJson("{}").success).toBe(true); |
| 94 | + expect(tryParseJson("{}").data).toEqual({}); |
| 95 | + |
| 96 | + expect(tryParseJson("[]").success).toBe(true); |
| 97 | + expect(tryParseJson("[]").data).toEqual([]); |
| 98 | + }); |
| 99 | + |
| 100 | + test("should return failure for non-JSON strings", () => { |
| 101 | + const nonJsonString = "this is not json"; |
| 102 | + const result = tryParseJson(nonJsonString); |
| 103 | + |
| 104 | + expect(result.success).toBe(false); |
| 105 | + expect(result.data).toBe(nonJsonString); |
| 106 | + }); |
| 107 | + |
| 108 | + test("should return failure for malformed JSON", () => { |
| 109 | + const malformedJson = '{"name":"test",}'; |
| 110 | + const result = tryParseJson(malformedJson); |
| 111 | + |
| 112 | + expect(result.success).toBe(false); |
| 113 | + expect(result.data).toBe(malformedJson); |
| 114 | + }); |
| 115 | + |
| 116 | + test("should return failure for strings with correct delimiters but invalid JSON", () => { |
| 117 | + const invalidJson = "{name:test}"; |
| 118 | + const result = tryParseJson(invalidJson); |
| 119 | + |
| 120 | + expect(result.success).toBe(false); |
| 121 | + expect(result.data).toBe(invalidJson); |
| 122 | + }); |
| 123 | + |
| 124 | + test("should handle edge cases", () => { |
| 125 | + expect(tryParseJson("").success).toBe(false); |
| 126 | + expect(tryParseJson("").data).toBe(""); |
| 127 | + |
| 128 | + expect(tryParseJson(" ").success).toBe(false); |
| 129 | + expect(tryParseJson(" ").data).toBe(" "); |
| 130 | + |
| 131 | + expect(tryParseJson("null").success).toBe(false); |
| 132 | + expect(tryParseJson("null").data).toBe("null"); |
| 133 | + |
| 134 | + expect(tryParseJson('"string"').success).toBe(false); |
| 135 | + expect(tryParseJson('"string"').data).toBe('"string"'); |
| 136 | + |
| 137 | + expect(tryParseJson("123").success).toBe(false); |
| 138 | + expect(tryParseJson("123").data).toBe("123"); |
| 139 | + |
| 140 | + expect(tryParseJson("true").success).toBe(false); |
| 141 | + expect(tryParseJson("true").data).toBe("true"); |
| 142 | + }); |
| 143 | +}); |
3 | 144 |
|
4 | 145 | describe("updateValueAtPath", () => {
|
5 | 146 | // Basic functionality tests
|
|
0 commit comments