energy-visualization-sankey 1.0.26 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +91 -0
- package/babel.config.cjs +18 -21
- package/dist/.tsbuildinfo +1 -0
- package/dist/sankey.esm.js +285 -123
- package/dist/sankey.esm.js.map +1 -1
- package/dist/sankey.umd.js +285 -123
- package/dist/sankey.umd.js.map +1 -1
- package/dist/types/core/Sankey.d.ts +60 -46
- package/dist/types/core/Sankey.d.ts.map +1 -1
- package/dist/types/core/types/public-events.d.ts +159 -0
- package/dist/types/core/types/public-events.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/services/AnimationService.d.ts.map +1 -1
- package/dist/types/services/InteractionService.d.ts.map +1 -1
- package/dist/types/services/data/DataValidationService.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +11 -0
- package/dist/types/types/index.d.ts.map +1 -1
- package/jest.config.cjs +22 -7
- package/package.json +3 -2
- package/src/core/Sankey.ts +282 -21
- package/src/core/types/public-events.ts +189 -0
- package/src/index.ts +21 -0
- package/src/services/AnimationService.ts +34 -19
- package/src/services/InteractionService.ts +49 -31
- package/src/services/data/DataValidationService.ts +8 -5
- package/src/types/index.ts +16 -0
- package/tests/event-bus.test.ts +137 -0
- package/tests/sankey-events.test.ts +289 -0
- package/tests/setup.js +13 -48
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public event API of the Sankey visualization
|
|
3
|
+
*
|
|
4
|
+
* Exercises the real class against the real dataset in jsdom, rather than the
|
|
5
|
+
* hand-written mocks used by numerical-validation.test.js.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from 'fs';
|
|
9
|
+
import * as path from 'path';
|
|
10
|
+
|
|
11
|
+
import Sankey from '@/core/Sankey';
|
|
12
|
+
import type {EnergyDataPoint, SankeyOptions} from '@/types';
|
|
13
|
+
|
|
14
|
+
const actualData: EnergyDataPoint[] = JSON.parse(
|
|
15
|
+
fs.readFileSync(path.join(__dirname, '../examples/data/us.json'), 'utf8')
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* initialize() chains several awaits before emitting 'system.ready', so a single
|
|
20
|
+
* `await Promise.resolve()` is not enough to observe it. A macrotask turn drains
|
|
21
|
+
* the whole chain.
|
|
22
|
+
*/
|
|
23
|
+
const flush = (): Promise<void> => new Promise(resolve => setTimeout(resolve, 0));
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* An explicit width keeps assertions deterministic: getBoundingClientRect()
|
|
27
|
+
* returns 0 under jsdom, which would otherwise fall through to a default.
|
|
28
|
+
*/
|
|
29
|
+
const baseOptions = (overrides: Partial<SankeyOptions> = {}): SankeyOptions => ({
|
|
30
|
+
data: actualData,
|
|
31
|
+
country: 'US',
|
|
32
|
+
width: 1400,
|
|
33
|
+
...overrides
|
|
34
|
+
} as SankeyOptions);
|
|
35
|
+
|
|
36
|
+
describe('Sankey public event API', () => {
|
|
37
|
+
let sankey: Sankey | null = null;
|
|
38
|
+
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
document.body.innerHTML = '<div id="sankey"></div>';
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterEach(() => {
|
|
44
|
+
sankey?.destroy();
|
|
45
|
+
sankey = null;
|
|
46
|
+
document.body.innerHTML = '';
|
|
47
|
+
jest.restoreAllMocks();
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('events constructor option', () => {
|
|
51
|
+
it('should deliver system.ready, which a later subscription would miss', async () => {
|
|
52
|
+
const onReady = jest.fn();
|
|
53
|
+
sankey = new Sankey('sankey', baseOptions({events: {'system.ready': onReady}}));
|
|
54
|
+
|
|
55
|
+
await flush();
|
|
56
|
+
|
|
57
|
+
expect(onReady).toHaveBeenCalledTimes(1);
|
|
58
|
+
const event = onReady.mock.calls[0][0];
|
|
59
|
+
expect(event.type).toBe('system.ready');
|
|
60
|
+
expect(event.data.dataPointCount).toBe(actualData.length);
|
|
61
|
+
expect(event.data.yearRange).toEqual([1800, 2021]);
|
|
62
|
+
expect(typeof event.data.totalInitTime).toBe('number');
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('should ignore a non-function entry rather than throwing', async () => {
|
|
66
|
+
sankey = new Sankey('sankey', baseOptions({events: {'system.ready': undefined}}));
|
|
67
|
+
await flush();
|
|
68
|
+
|
|
69
|
+
expect(sankey.isInitialized()).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
describe('whenReady', () => {
|
|
74
|
+
it('should resolve for a caller that awaits it after initialization finished', async () => {
|
|
75
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
76
|
+
|
|
77
|
+
// Deliberately let initialization complete before touching the latch.
|
|
78
|
+
await flush();
|
|
79
|
+
await flush();
|
|
80
|
+
|
|
81
|
+
const event = await sankey.whenReady();
|
|
82
|
+
expect(event.type).toBe('system.ready');
|
|
83
|
+
expect(event.data.dataPointCount).toBe(actualData.length);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('should reject when the instance is destroyed before it becomes ready', async () => {
|
|
87
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
88
|
+
const pending = sankey.whenReady();
|
|
89
|
+
|
|
90
|
+
sankey.destroy();
|
|
91
|
+
|
|
92
|
+
await expect(pending).rejects.toThrow(/destroyed before initialization/);
|
|
93
|
+
sankey = null;
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('on', () => {
|
|
98
|
+
it('should deliver year.changed with isAnimating false for a programmatic change', async () => {
|
|
99
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
100
|
+
await flush();
|
|
101
|
+
|
|
102
|
+
const handler = jest.fn();
|
|
103
|
+
sankey.on('year.changed', handler);
|
|
104
|
+
|
|
105
|
+
const years = sankey.getYears();
|
|
106
|
+
sankey.setYear(years[5]);
|
|
107
|
+
await flush();
|
|
108
|
+
|
|
109
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
110
|
+
const {data} = handler.mock.calls[0][0];
|
|
111
|
+
expect(data.year).toBe(years[5]);
|
|
112
|
+
expect(data.yearIndex).toBe(5);
|
|
113
|
+
expect(data.isAnimating).toBe(false);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('should register the same handler reference twice and call it twice', async () => {
|
|
117
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
118
|
+
await flush();
|
|
119
|
+
|
|
120
|
+
const handler = jest.fn();
|
|
121
|
+
const stopFirst = sankey.on('year.changed', handler);
|
|
122
|
+
sankey.on('year.changed', handler);
|
|
123
|
+
|
|
124
|
+
const years = sankey.getYears();
|
|
125
|
+
sankey.setYear(years[1]);
|
|
126
|
+
await flush();
|
|
127
|
+
expect(handler).toHaveBeenCalledTimes(2);
|
|
128
|
+
|
|
129
|
+
// The bus dedupes by function identity, so this only works because
|
|
130
|
+
// each registration is wrapped in its own closure.
|
|
131
|
+
handler.mockClear();
|
|
132
|
+
stopFirst();
|
|
133
|
+
sankey.setYear(years[2]);
|
|
134
|
+
await flush();
|
|
135
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('should return an unsubscribe function that is safe to call twice', async () => {
|
|
139
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
140
|
+
await flush();
|
|
141
|
+
|
|
142
|
+
const handler = jest.fn();
|
|
143
|
+
const stop = sankey.on('year.changed', handler);
|
|
144
|
+
const years = sankey.getYears();
|
|
145
|
+
|
|
146
|
+
stop();
|
|
147
|
+
expect(() => stop()).not.toThrow();
|
|
148
|
+
|
|
149
|
+
sankey.setYear(years[3]);
|
|
150
|
+
await flush();
|
|
151
|
+
expect(handler).not.toHaveBeenCalled();
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('should not let a throwing handler break the visualization', async () => {
|
|
155
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
156
|
+
await flush();
|
|
157
|
+
|
|
158
|
+
jest.spyOn(console, 'error').mockImplementation(() => {
|
|
159
|
+
});
|
|
160
|
+
const survivor = jest.fn();
|
|
161
|
+
sankey.on('year.changed', () => {
|
|
162
|
+
throw new Error('handler exploded');
|
|
163
|
+
});
|
|
164
|
+
sankey.on('year.changed', survivor);
|
|
165
|
+
|
|
166
|
+
const years = sankey.getYears();
|
|
167
|
+
sankey.setYear(years[4]);
|
|
168
|
+
await flush();
|
|
169
|
+
|
|
170
|
+
expect(survivor).toHaveBeenCalledTimes(1);
|
|
171
|
+
expect(sankey.getCurrentYear()).toBe(years[4]);
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
describe('once', () => {
|
|
176
|
+
it('should fire exactly once across two year changes', async () => {
|
|
177
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
178
|
+
await flush();
|
|
179
|
+
|
|
180
|
+
const handler = jest.fn();
|
|
181
|
+
sankey.once('year.changed', handler);
|
|
182
|
+
|
|
183
|
+
const years = sankey.getYears();
|
|
184
|
+
sankey.setYear(years[1]);
|
|
185
|
+
await flush();
|
|
186
|
+
sankey.setYear(years[2]);
|
|
187
|
+
await flush();
|
|
188
|
+
|
|
189
|
+
expect(handler).toHaveBeenCalledTimes(1);
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
describe('off', () => {
|
|
194
|
+
it('should remove every registration of a handler for that event', async () => {
|
|
195
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
196
|
+
await flush();
|
|
197
|
+
|
|
198
|
+
const handler = jest.fn();
|
|
199
|
+
sankey.on('year.changed', handler);
|
|
200
|
+
sankey.on('year.changed', handler);
|
|
201
|
+
sankey.off('year.changed', handler);
|
|
202
|
+
|
|
203
|
+
const years = sankey.getYears();
|
|
204
|
+
sankey.setYear(years[1]);
|
|
205
|
+
await flush();
|
|
206
|
+
|
|
207
|
+
expect(handler).not.toHaveBeenCalled();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
it('should cancel a pending once registration', async () => {
|
|
211
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
212
|
+
await flush();
|
|
213
|
+
|
|
214
|
+
const handler = jest.fn();
|
|
215
|
+
sankey.once('year.changed', handler);
|
|
216
|
+
sankey.off('year.changed', handler);
|
|
217
|
+
|
|
218
|
+
const years = sankey.getYears();
|
|
219
|
+
sankey.setYear(years[1]);
|
|
220
|
+
await flush();
|
|
221
|
+
|
|
222
|
+
expect(handler).not.toHaveBeenCalled();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
it('should be a no-op for a handler that was never registered', async () => {
|
|
226
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
227
|
+
await flush();
|
|
228
|
+
|
|
229
|
+
expect(() => sankey!.off('year.changed', jest.fn())).not.toThrow();
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
describe('destroy', () => {
|
|
234
|
+
it('should stop delivering events', async () => {
|
|
235
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
236
|
+
await flush();
|
|
237
|
+
|
|
238
|
+
const handler = jest.fn();
|
|
239
|
+
sankey.on('year.changed', handler);
|
|
240
|
+
|
|
241
|
+
sankey.destroy();
|
|
242
|
+
await flush();
|
|
243
|
+
handler.mockClear();
|
|
244
|
+
|
|
245
|
+
sankey.setYear(1900);
|
|
246
|
+
await flush();
|
|
247
|
+
expect(handler).not.toHaveBeenCalled();
|
|
248
|
+
|
|
249
|
+
sankey = null;
|
|
250
|
+
});
|
|
251
|
+
|
|
252
|
+
it('should warn and return a no-op unsubscribe when subscribing after destroy', async () => {
|
|
253
|
+
sankey = new Sankey('sankey', baseOptions());
|
|
254
|
+
await flush();
|
|
255
|
+
|
|
256
|
+
const warn = jest.spyOn(console, 'warn').mockImplementation(() => {
|
|
257
|
+
});
|
|
258
|
+
sankey.destroy();
|
|
259
|
+
|
|
260
|
+
const stop = sankey.on('year.changed', jest.fn());
|
|
261
|
+
expect(warn).toHaveBeenCalledWith(expect.stringContaining('has been destroyed'));
|
|
262
|
+
expect(() => stop()).not.toThrow();
|
|
263
|
+
|
|
264
|
+
sankey = null;
|
|
265
|
+
});
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
describe('unhandled rejections', () => {
|
|
269
|
+
// Guards the deliberate no-op .catch() on readyPromise in the
|
|
270
|
+
// constructor. Without it, every destroyed-before-ready instance would
|
|
271
|
+
// surface a fresh unhandled rejection for callers that never awaited.
|
|
272
|
+
it('should not produce an unhandled rejection when destroyed before ready', async () => {
|
|
273
|
+
const onUnhandled = jest.fn();
|
|
274
|
+
process.on('unhandledRejection', onUnhandled);
|
|
275
|
+
|
|
276
|
+
try {
|
|
277
|
+
const instance = new Sankey('sankey', baseOptions());
|
|
278
|
+
instance.destroy();
|
|
279
|
+
|
|
280
|
+
await flush();
|
|
281
|
+
await flush();
|
|
282
|
+
|
|
283
|
+
expect(onUnhandled).not.toHaveBeenCalled();
|
|
284
|
+
} finally {
|
|
285
|
+
process.off('unhandledRejection', onUnhandled);
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
});
|
|
289
|
+
});
|
package/tests/setup.js
CHANGED
|
@@ -1,53 +1,18 @@
|
|
|
1
1
|
// Jest setup for US Energy Sankey validation tests
|
|
2
|
+
//
|
|
3
|
+
// jest-environment-jsdom already supplies document, window, HTMLElement,
|
|
4
|
+
// performance and requestAnimationFrame. This file used to build a SECOND JSDOM
|
|
5
|
+
// realm and overwrite those globals, which caused two real problems:
|
|
6
|
+
//
|
|
7
|
+
// 1. Cross-realm `instanceof` failures. Sankey.resolveContainer() checks
|
|
8
|
+
// `containerId instanceof HTMLElement`, and an element created by Jest's
|
|
9
|
+
// document is not an instance of the overwritten global.HTMLElement.
|
|
10
|
+
// 2. `resources: 'usable'` made JSDOM issue real network requests for the
|
|
11
|
+
// <img> in Sankey's injected header markup.
|
|
12
|
+
//
|
|
13
|
+
// Only the encoder polyfills are still needed.
|
|
2
14
|
|
|
3
|
-
// Polyfills for JSDOM environment
|
|
4
15
|
const {TextEncoder, TextDecoder} = require('util');
|
|
16
|
+
|
|
5
17
|
global.TextEncoder = TextEncoder;
|
|
6
18
|
global.TextDecoder = TextDecoder;
|
|
7
|
-
|
|
8
|
-
const {JSDOM} = require('jsdom');
|
|
9
|
-
|
|
10
|
-
// Setup DOM environment
|
|
11
|
-
const dom = new JSDOM('<!DOCTYPE html><html><body><div id="test-container"></div></body></html>', {
|
|
12
|
-
pretendToBeVisual: true,
|
|
13
|
-
resources: 'usable',
|
|
14
|
-
url: 'http://localhost'
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
// Make DOM available globally
|
|
18
|
-
global.document = dom.window.document;
|
|
19
|
-
global.window = dom.window;
|
|
20
|
-
global.HTMLElement = dom.window.HTMLElement;
|
|
21
|
-
global.HTMLInputElement = dom.window.HTMLInputElement;
|
|
22
|
-
global.HTMLOutputElement = dom.window.HTMLOutputElement;
|
|
23
|
-
global.SVGSVGElement = dom.window.SVGSVGElement;
|
|
24
|
-
global.SVGElement = dom.window.SVGElement;
|
|
25
|
-
|
|
26
|
-
// Mock performance API for Node.js
|
|
27
|
-
global.performance = {
|
|
28
|
-
now: () => Date.now(),
|
|
29
|
-
timing: {},
|
|
30
|
-
memory: {
|
|
31
|
-
usedJSHeapSize: 1024 * 1024 * 10, // 10MB mock
|
|
32
|
-
totalJSHeapSize: 1024 * 1024 * 50, // 50MB mock
|
|
33
|
-
jsHeapSizeLimit: 1024 * 1024 * 100 // 100MB mock
|
|
34
|
-
}
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
// Mock requestAnimationFrame
|
|
38
|
-
global.requestAnimationFrame = (callback) => {
|
|
39
|
-
return setTimeout(callback, 16); // ~60fps
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
global.cancelAnimationFrame = (id) => {
|
|
43
|
-
clearTimeout(id);
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
// Suppress console logs during tests (optional)
|
|
47
|
-
// global.console = {
|
|
48
|
-
// ...console,
|
|
49
|
-
// log: jest.fn(),
|
|
50
|
-
// debug: jest.fn(),
|
|
51
|
-
// info: jest.fn(),
|
|
52
|
-
// warn: jest.fn()
|
|
53
|
-
// };
|