|
| 1 | +import type { IMutField } from '../interfaces'; |
| 2 | +import { newChart } from '../models/visSpecHistory'; |
| 3 | +import { VizSpecStore } from './visualSpecStore'; |
| 4 | + |
| 5 | +const META: IMutField[] = [ |
| 6 | + { fid: 'category', name: 'Category', semanticType: 'nominal', analyticType: 'dimension' }, |
| 7 | + { fid: 'sales', name: 'Sales', semanticType: 'quantitative', analyticType: 'measure' }, |
| 8 | +]; |
| 9 | + |
| 10 | +const dispatchEvent = jest.fn(); |
| 11 | +const originalDocument = Object.getOwnPropertyDescriptor(globalThis, 'document'); |
| 12 | +const originalCustomEvent = Object.getOwnPropertyDescriptor(globalThis, 'CustomEvent'); |
| 13 | + |
| 14 | +const metaFieldIds = (store: VizSpecStore) => { |
| 15 | + const expectedIds = new Set(META.map((field) => field.fid)); |
| 16 | + return [...store.currentEncodings.dimensions, ...store.currentEncodings.measures] |
| 17 | + .map((field) => field.fid) |
| 18 | + .filter((fid) => expectedIds.has(fid)); |
| 19 | +}; |
| 20 | + |
| 21 | +describe('VizSpecStore metadata updates', () => { |
| 22 | + beforeAll(() => { |
| 23 | + Object.defineProperty(globalThis, 'document', { |
| 24 | + configurable: true, |
| 25 | + value: { dispatchEvent }, |
| 26 | + }); |
| 27 | + Object.defineProperty(globalThis, 'CustomEvent', { |
| 28 | + configurable: true, |
| 29 | + value: class { |
| 30 | + constructor( |
| 31 | + public type: string, |
| 32 | + public init?: CustomEventInit, |
| 33 | + ) {} |
| 34 | + }, |
| 35 | + }); |
| 36 | + }); |
| 37 | + |
| 38 | + beforeEach(() => { |
| 39 | + dispatchEvent.mockClear(); |
| 40 | + }); |
| 41 | + |
| 42 | + afterAll(() => { |
| 43 | + if (originalDocument) { |
| 44 | + Object.defineProperty(globalThis, 'document', originalDocument); |
| 45 | + } else { |
| 46 | + Reflect.deleteProperty(globalThis, 'document'); |
| 47 | + } |
| 48 | + if (originalCustomEvent) { |
| 49 | + Object.defineProperty(globalThis, 'CustomEvent', originalCustomEvent); |
| 50 | + } else { |
| 51 | + Reflect.deleteProperty(globalThis, 'CustomEvent'); |
| 52 | + } |
| 53 | + }); |
| 54 | + |
| 55 | + test('hydrates the initial chart when asynchronous metadata first arrives', () => { |
| 56 | + const store = new VizSpecStore([]); |
| 57 | + const initialVisId = store.currentVis.visId; |
| 58 | + |
| 59 | + // App.tsx creates a fresh [] while async fields are still unavailable. |
| 60 | + store.setMeta([]); |
| 61 | + store.setMeta([]); |
| 62 | + store.setMeta(META); |
| 63 | + expect(dispatchEvent).toHaveBeenCalledTimes(1); |
| 64 | + expect(dispatchEvent.mock.calls[0][0]).toMatchObject({ type: 'edit-graphic-walker' }); |
| 65 | + expect(store.canUndo).toBe(false); |
| 66 | + const chartOneFieldsAfterHydration = metaFieldIds(store); |
| 67 | + |
| 68 | + store.addVisualization(); |
| 69 | + const chartTwoFields = metaFieldIds(store); |
| 70 | + |
| 71 | + store.selectVisualization(0); |
| 72 | + |
| 73 | + expect({ |
| 74 | + chartOneFieldsAfterHydration, |
| 75 | + chartTwoFields, |
| 76 | + chartOneFieldsAfterSwitchingBack: metaFieldIds(store), |
| 77 | + }).toEqual({ |
| 78 | + chartOneFieldsAfterHydration: ['category', 'sales'], |
| 79 | + chartTwoFields: ['category', 'sales'], |
| 80 | + chartOneFieldsAfterSwitchingBack: ['category', 'sales'], |
| 81 | + }); |
| 82 | + expect(store.currentVis.visId).toBe(initialVisId); |
| 83 | + }); |
| 84 | + |
| 85 | + test('preserves an existing user chart when metadata props update', () => { |
| 86 | + const store = new VizSpecStore(META); |
| 87 | + store.moveField('dimensions', 0, 'columns', 0); |
| 88 | + const editedChart = store.currentVis; |
| 89 | + const updatedMeta: IMutField[] = [ |
| 90 | + ...META, |
| 91 | + { fid: 'profit', name: 'Profit', semanticType: 'quantitative', analyticType: 'measure' }, |
| 92 | + ]; |
| 93 | + |
| 94 | + store.setMeta(updatedMeta); |
| 95 | + |
| 96 | + expect(store.meta).toEqual(updatedMeta); |
| 97 | + expect(store.currentVis).toBe(editedChart); |
| 98 | + expect(store.currentEncodings.columns.map((field) => field.fid)).toEqual(['category']); |
| 99 | + }); |
| 100 | + |
| 101 | + test('preserves a chart edited before asynchronous metadata arrives', () => { |
| 102 | + const store = new VizSpecStore([]); |
| 103 | + store.setVisualConfig('geoms', ['line']); |
| 104 | + const editedChart = store.currentVis; |
| 105 | + |
| 106 | + store.setMeta(META); |
| 107 | + |
| 108 | + expect(store.currentVis).toBe(editedChart); |
| 109 | + expect(store.currentVis.config.geoms).toEqual(['line']); |
| 110 | + expect(metaFieldIds(store)).toEqual([]); |
| 111 | + }); |
| 112 | + |
| 113 | + test('preserves a supplied chart when asynchronous metadata arrives', () => { |
| 114 | + const store = new VizSpecStore([]); |
| 115 | + store.replaceNow(newChart(META, 'Supplied chart', 'supplied-vis-id')); |
| 116 | + const suppliedChart = store.currentVis; |
| 117 | + |
| 118 | + store.setMeta(META); |
| 119 | + |
| 120 | + expect(store.currentVis).toBe(suppliedChart); |
| 121 | + expect(store.currentVis.visId).toBe('supplied-vis-id'); |
| 122 | + expect(metaFieldIds(store)).toEqual(['category', 'sales']); |
| 123 | + }); |
| 124 | + |
| 125 | + test('preserves multiple charts created before asynchronous metadata arrives', () => { |
| 126 | + const store = new VizSpecStore([]); |
| 127 | + const firstChart = store.visList[0]; |
| 128 | + store.addVisualization(); |
| 129 | + const secondChart = store.visList[1]; |
| 130 | + |
| 131 | + store.setMeta(META); |
| 132 | + |
| 133 | + expect(store.visList[0]).toBe(firstChart); |
| 134 | + expect(store.visList[1]).toBe(secondChart); |
| 135 | + }); |
| 136 | + |
| 137 | + test('hydrates a reset placeholder when metadata arrives', () => { |
| 138 | + const store = new VizSpecStore([]); |
| 139 | + store.resetVisualization('Reset while loading'); |
| 140 | + const resetVisId = store.currentVis.visId; |
| 141 | + |
| 142 | + store.setMeta(META); |
| 143 | + |
| 144 | + expect(store.currentVis.name).toBe('Reset while loading'); |
| 145 | + expect(store.currentVis.visId).toBe(resetVisId); |
| 146 | + expect(metaFieldIds(store)).toEqual(['category', 'sales']); |
| 147 | + }); |
| 148 | + |
| 149 | + test('hydrates the remaining pristine placeholder after the first chart is removed', () => { |
| 150 | + const store = new VizSpecStore([]); |
| 151 | + store.addVisualization(); |
| 152 | + const remainingVisId = store.visList[1].now.visId; |
| 153 | + store.removeVisualization(0); |
| 154 | + |
| 155 | + store.setMeta(META); |
| 156 | + |
| 157 | + expect(store.currentVis.visId).toBe(remainingVisId); |
| 158 | + expect(metaFieldIds(store)).toEqual(['category', 'sales']); |
| 159 | + }); |
| 160 | + |
| 161 | + test('keeps an explicitly empty store chartless when metadata arrives', () => { |
| 162 | + const store = new VizSpecStore([], { empty: true }); |
| 163 | + |
| 164 | + store.setMeta(META); |
| 165 | + |
| 166 | + expect(store.visLength).toBe(0); |
| 167 | + expect(store.meta).toEqual(META); |
| 168 | + }); |
| 169 | + |
| 170 | + test('uses the latest default config when hydrating the initial chart', () => { |
| 171 | + const store = new VizSpecStore([]); |
| 172 | + store.setDefaultConfig({ layout: { renderer: 'observable-plot' } }); |
| 173 | + |
| 174 | + store.setMeta(META); |
| 175 | + |
| 176 | + expect(store.currentVis.layout.renderer).toBe('observable-plot'); |
| 177 | + }); |
| 178 | +}); |
0 commit comments