|
| 1 | +import { observable } from "mobx"; |
| 2 | +import { SetupHost } from "@mendix/widget-plugin-mobx-kit/main"; |
| 3 | +import { EditableChartStore, EditableChartStoreProps } from "../EditableChart.store"; |
| 4 | + |
| 5 | +class TestHost extends SetupHost {} |
| 6 | + |
| 7 | +function setupStore(initial: EditableChartStoreProps = { layout: {}, config: {}, data: [] }): { |
| 8 | + store: EditableChartStore; |
| 9 | + props: ReturnType<typeof observable.box<EditableChartStoreProps>>; |
| 10 | + dispose: () => void; |
| 11 | +} { |
| 12 | + const props = observable.box<EditableChartStoreProps>(initial, { deep: false }); |
| 13 | + const host = new TestHost(); |
| 14 | + const store = new EditableChartStore(host, { get: () => props.get() }); |
| 15 | + const dispose = host.setup(); |
| 16 | + return { store, props, dispose }; |
| 17 | +} |
| 18 | + |
| 19 | +describe("EditableChartStore", () => { |
| 20 | + it("loads layout, config and data from props on setup", () => { |
| 21 | + const { store, dispose } = setupStore({ |
| 22 | + layout: { a: 1 }, |
| 23 | + config: { b: 2 }, |
| 24 | + data: [{ x: [1] }] |
| 25 | + }); |
| 26 | + |
| 27 | + expect(store.layout).toEqual({ a: 1 }); |
| 28 | + expect(store.config).toEqual({ b: 2 }); |
| 29 | + expect(store.data).toEqual([{ x: [1] }]); |
| 30 | + |
| 31 | + dispose(); |
| 32 | + }); |
| 33 | + |
| 34 | + it("setDataAt replaces the trace at a valid index with parsed JSON", () => { |
| 35 | + const { store, dispose } = setupStore({ |
| 36 | + layout: {}, |
| 37 | + config: {}, |
| 38 | + data: [{ x: [1] }, { x: [2] }] |
| 39 | + }); |
| 40 | + const before = store.data; |
| 41 | + |
| 42 | + store.setDataAt(1, '{"x":[9]}'); |
| 43 | + |
| 44 | + expect(store.data[0]).toEqual({ x: [1] }); |
| 45 | + expect(store.data[1]).toEqual({ x: [9] }); |
| 46 | + expect(store.data).not.toBe(before); |
| 47 | + |
| 48 | + dispose(); |
| 49 | + }); |
| 50 | + |
| 51 | + it("setDataAt ignores an out-of-range index without throwing", () => { |
| 52 | + const { store, dispose } = setupStore({ layout: {}, config: {}, data: [{ x: [1] }, { x: [2] }] }); |
| 53 | + const before = store.data; |
| 54 | + |
| 55 | + store.setDataAt(5, '{"x":[9]}'); |
| 56 | + store.setDataAt(-1, '{"x":[9]}'); |
| 57 | + |
| 58 | + expect(store.data).toBe(before); |
| 59 | + |
| 60 | + dispose(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("setDataAt swallows invalid JSON, keeps data, and warns", () => { |
| 64 | + const { store, dispose } = setupStore({ layout: {}, config: {}, data: [{ x: [1] }] }); |
| 65 | + const before = store.data; |
| 66 | + const warn = jest.spyOn(console, "warn").mockImplementation(() => undefined); |
| 67 | + |
| 68 | + store.setDataAt(0, "{ not json "); |
| 69 | + |
| 70 | + expect(store.data).toBe(before); |
| 71 | + expect(warn).toHaveBeenCalledTimes(1); |
| 72 | + |
| 73 | + warn.mockRestore(); |
| 74 | + dispose(); |
| 75 | + }); |
| 76 | + |
| 77 | + it("setDataAt rejects non-object JSON (array or primitive)", () => { |
| 78 | + const { store, dispose } = setupStore({ layout: {}, config: {}, data: [{ x: [1] }] }); |
| 79 | + const before = store.data; |
| 80 | + |
| 81 | + store.setDataAt(0, "[1,2,3]"); |
| 82 | + store.setDataAt(0, "42"); |
| 83 | + |
| 84 | + expect(store.data).toBe(before); |
| 85 | + |
| 86 | + dispose(); |
| 87 | + }); |
| 88 | + |
| 89 | + it("setLayout ignores null and replaces on a real object", () => { |
| 90 | + const { store, dispose } = setupStore({ layout: { a: 1 }, config: {}, data: [] }); |
| 91 | + |
| 92 | + store.setLayout(null as unknown as Record<string, unknown>); |
| 93 | + expect(store.layout).toEqual({ a: 1 }); |
| 94 | + |
| 95 | + const before = store.layout; |
| 96 | + store.setLayout({ c: 3 }); |
| 97 | + expect(store.layout).toEqual({ c: 3 }); |
| 98 | + expect(store.layout).not.toBe(before); |
| 99 | + |
| 100 | + dispose(); |
| 101 | + }); |
| 102 | + |
| 103 | + it("setConfig ignores null and replaces on a real object", () => { |
| 104 | + const { store, dispose } = setupStore({ layout: {}, config: { b: 2 }, data: [] }); |
| 105 | + |
| 106 | + store.setConfig(null as unknown as Record<string, unknown>); |
| 107 | + expect(store.config).toEqual({ b: 2 }); |
| 108 | + |
| 109 | + store.setConfig({ d: 4 }); |
| 110 | + expect(store.config).toEqual({ d: 4 }); |
| 111 | + |
| 112 | + dispose(); |
| 113 | + }); |
| 114 | + |
| 115 | + it("exposes layout, config and data as JSON strings", () => { |
| 116 | + const { store, dispose } = setupStore({ |
| 117 | + layout: { a: 1 }, |
| 118 | + config: { b: 2 }, |
| 119 | + data: [{ x: [1] }] |
| 120 | + }); |
| 121 | + |
| 122 | + expect(store.layoutJson).toBe('{"a":1}'); |
| 123 | + expect(store.configJson).toBe('{"b":2}'); |
| 124 | + expect(store.dataJson).toEqual(['{"x":[1]}']); |
| 125 | + |
| 126 | + dispose(); |
| 127 | + }); |
| 128 | +}); |
0 commit comments