|
| 1 | +import { autorun } from "mobx"; |
| 2 | +import { ChartDataStore } from "../ChartDataStore"; |
| 3 | + |
| 4 | +describe("initial state and JSON outputs", () => { |
| 5 | + it("should contain initial state and JSON outputs", () => { |
| 6 | + const store = new ChartDataStore(); |
| 7 | + expect(store.layout).toEqual({}); |
| 8 | + expect(store.config).toEqual({}); |
| 9 | + expect(store.data).toEqual([]); |
| 10 | + expect(store.layoutJson).toBe("{}"); |
| 11 | + expect(store.configJson).toBe("{}"); |
| 12 | + expect(store.dataJson).toBe("[]"); |
| 13 | + }); |
| 14 | +}); |
| 15 | + |
| 16 | +describe("update layout/config/data and keep JSON in sync", () => { |
| 17 | + it("should update layout/config/data and keep JSON in sync", () => { |
| 18 | + const store = new ChartDataStore(); |
| 19 | + store.setLayout({ title: "Test" }); |
| 20 | + store.setConfig({ type: "bar" }); |
| 21 | + store.setData([ |
| 22 | + { label: "A", value: 10 }, |
| 23 | + { label: "B", value: 20 } |
| 24 | + ]); |
| 25 | + |
| 26 | + expect(store.layoutJson).toBe('{"title":"Test"}'); |
| 27 | + expect(store.configJson).toBe('{"type":"bar"}'); |
| 28 | + expect(store.dataJson).toBe('[{"label":"A","value":10},{"label":"B","value":20}]'); |
| 29 | + }); |
| 30 | + |
| 31 | + it("should parse JSON string and set data at index", () => { |
| 32 | + const store = new ChartDataStore(); |
| 33 | + store.setData([{ id: 1, value: 10 }]); |
| 34 | + store.setDataAt(0, '{"id":2,"value":40}'); |
| 35 | + expect(store.dataJson).toBe('[{"id":2,"value":40}]'); |
| 36 | + }); |
| 37 | + |
| 38 | + it("should handle setDataAt with invalid JSON gracefully", () => { |
| 39 | + const store = new ChartDataStore(); |
| 40 | + store.setData([{ id: 1, value: 10 }]); |
| 41 | + store.setDataAt(1, "invalid json"); |
| 42 | + expect(store.dataJson).toBe('[{"id":1,"value":10}]'); |
| 43 | + }); |
| 44 | + |
| 45 | + it("should ignore setDataAt if parsed result is an array", () => { |
| 46 | + const store = new ChartDataStore(); |
| 47 | + store.setData([{ id: 1, value: 10 }]); |
| 48 | + store.setDataAt(1, '["not", "an", "object"]'); |
| 49 | + expect(store.dataJson).toBe('[{"id":1,"value":10}]'); |
| 50 | + }); |
| 51 | + |
| 52 | + it("should throw error for non-array data in setData", () => { |
| 53 | + const store = new ChartDataStore(); |
| 54 | + expect(() => store.setData({ invalid: "data" } as any)).toThrow(); |
| 55 | + }); |
| 56 | +}); |
| 57 | + |
| 58 | +describe("JSON getters", () => { |
| 59 | + it("should return correct JSON strings for layout", () => { |
| 60 | + const store = new ChartDataStore(); |
| 61 | + store.setLayout({ title: "Demo" }); |
| 62 | + expect(store.layoutJson).toBe('{"title":"Demo"}'); |
| 63 | + }); |
| 64 | + |
| 65 | + it("should return correct JSON strings for config", () => { |
| 66 | + const store = new ChartDataStore(); |
| 67 | + store.setConfig({ type: "pie" }); |
| 68 | + expect(store.configJson).toBe('{"type":"pie"}'); |
| 69 | + }); |
| 70 | + |
| 71 | + it("should return correct JSON strings for data", () => { |
| 72 | + const store = new ChartDataStore(); |
| 73 | + store.setData([ |
| 74 | + { label: "A", value: 10 }, |
| 75 | + { label: "B", value: 20 } |
| 76 | + ]); |
| 77 | + expect(store.dataJson).toBe('[{"label":"A","value":10},{"label":"B","value":20}]'); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe("null/undefined handling", () => { |
| 82 | + it("should not set layout for null or undefined values", () => { |
| 83 | + const store = new ChartDataStore(); |
| 84 | + store.setLayout({ initial: "true" }); |
| 85 | + store.setLayout(null as any); |
| 86 | + expect(store.layoutJson).toBe('{"initial":"true"}'); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should not set config for null or undefined values", () => { |
| 90 | + const store = new ChartDataStore(); |
| 91 | + store.setConfig({ mode: "dark" }); |
| 92 | + store.setConfig(undefined as any); |
| 93 | + expect(store.configJson).toBe('{"mode":"dark"}'); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("reset method", () => { |
| 98 | + it("should reset all fields using reset method", () => { |
| 99 | + const store = new ChartDataStore(); |
| 100 | + store.setLayout({ old: "value" }); |
| 101 | + store.setConfig({ old: "value" }); |
| 102 | + store.setData([{ old: "value" }]); |
| 103 | + |
| 104 | + store.reset({ new: "value" }, { new: "value" }, [{ new: "value" }]); |
| 105 | + |
| 106 | + expect(store.layoutJson).toBe('{"new":"value"}'); |
| 107 | + expect(store.configJson).toBe('{"new":"value"}'); |
| 108 | + expect(store.dataJson).toBe('[{"new":"value"}]'); |
| 109 | + }); |
| 110 | +}); |
| 111 | + |
| 112 | +describe("reactive updates with MobX", () => { |
| 113 | + it("reactively updates layoutJson when layout changes", () => { |
| 114 | + const store = new ChartDataStore(); |
| 115 | + const loggedValues: string[] = []; |
| 116 | + autorun(() => loggedValues.push(store.layoutJson)); |
| 117 | + expect(loggedValues).toEqual(["{}"]); |
| 118 | + store.setLayout({ title: "First" }); |
| 119 | + expect(loggedValues).toEqual(["{}", '{"title":"First"}']); |
| 120 | + store.setLayout({ title: "Updated" }); |
| 121 | + expect(loggedValues).toEqual(["{}", '{"title":"First"}', '{"title":"Updated"}']); |
| 122 | + }); |
| 123 | + |
| 124 | + it("reactively updates dataJson when data changes", () => { |
| 125 | + const store = new ChartDataStore(); |
| 126 | + const loggedValues: string[] = []; |
| 127 | + autorun(() => loggedValues.push(store.dataJson)); |
| 128 | + expect(loggedValues).toEqual(["[]"]); |
| 129 | + store.setData([{ id: 1, value: 5 }]); |
| 130 | + expect(loggedValues.length).toBeGreaterThan(1); |
| 131 | + store.setData([ |
| 132 | + { id: 1, value: 5 }, |
| 133 | + { id: 2, value: 10 } |
| 134 | + ]); |
| 135 | + expect(loggedValues.length).toBeGreaterThan(2); |
| 136 | + }); |
| 137 | + |
| 138 | + it("reactively updates configJson when config changes", () => { |
| 139 | + const store = new ChartDataStore(); |
| 140 | + const loggedValues: string[] = []; |
| 141 | + autorun(() => loggedValues.push(store.configJson)); |
| 142 | + expect(loggedValues).toEqual(["{}"]); |
| 143 | + store.setConfig({ mode: "dark" }); |
| 144 | + expect(loggedValues).toEqual(["{}", '{"mode":"dark"}']); |
| 145 | + store.setConfig({ mode: "light" }); |
| 146 | + expect(loggedValues).toEqual(["{}", '{"mode":"dark"}', '{"mode":"light"}']); |
| 147 | + }); |
| 148 | + |
| 149 | + it("reactively updates store after reset", () => { |
| 150 | + const store = new ChartDataStore(); |
| 151 | + const logged: Array<Record<string, string>> = []; |
| 152 | + autorun(() => { |
| 153 | + logged.push({ |
| 154 | + layout: store.layoutJson, |
| 155 | + config: store.configJson, |
| 156 | + data: store.dataJson |
| 157 | + }); |
| 158 | + }); |
| 159 | + expect(logged).toEqual([ |
| 160 | + { |
| 161 | + layout: "{}", |
| 162 | + config: "{}", |
| 163 | + data: "[]" |
| 164 | + } |
| 165 | + ]); |
| 166 | + store.setLayout({ initial: "true" }); |
| 167 | + store.setConfig({ setting: "on" }); |
| 168 | + store.setData([{ item: "one" }]); |
| 169 | + expect(logged.length).toBeGreaterThan(1); |
| 170 | + store.reset({ reset: "layout" }, { reset: "config" }, [{ reset: "data" }]); |
| 171 | + expect(logged.length).toBeGreaterThan(2); |
| 172 | + }); |
| 173 | +}); |
0 commit comments