energy-visualization-sankey 1.1.0 → 1.2.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 +62 -28
- package/dist/.tsbuildinfo +1 -1
- package/dist/sankey.esm.js +208 -40
- package/dist/sankey.esm.js.map +1 -1
- package/dist/sankey.umd.js +208 -40
- package/dist/sankey.umd.js.map +1 -1
- package/dist/types/core/Sankey.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/types/index.d.ts +29 -0
- package/dist/types/types/index.d.ts.map +1 -1
- package/dist/types/utils/header.d.ts +54 -0
- package/dist/types/utils/header.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/core/Sankey.ts +10 -41
- package/src/index.ts +2 -0
- package/src/types/index.ts +31 -0
- package/src/utils/header.ts +238 -0
- package/tests/header.test.ts +198 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configurable header
|
|
3
|
+
*
|
|
4
|
+
* Unit tests for the pure helpers in utils/header plus DOM tests against the
|
|
5
|
+
* real class in jsdom, using the same dataset as sankey-events.test.ts.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import * as fs from 'fs';
|
|
9
|
+
import * as path from 'path';
|
|
10
|
+
|
|
11
|
+
import Sankey from '@/core/Sankey';
|
|
12
|
+
import {SankeyError} from '@/types';
|
|
13
|
+
import type {EnergyDataPoint, SankeyHeaderOptions, SankeyOptions} from '@/types';
|
|
14
|
+
import {DEFAULT_HEADER, escapeHtml, isHttpsUrl, resolveHeader} from '@/utils/header';
|
|
15
|
+
|
|
16
|
+
const actualData: EnergyDataPoint[] = JSON.parse(
|
|
17
|
+
fs.readFileSync(path.join(__dirname, '../examples/data/us.json'), 'utf8')
|
|
18
|
+
);
|
|
19
|
+
const firstYear = Math.min(...actualData.map(d => d.year));
|
|
20
|
+
const lastYear = Math.max(...actualData.map(d => d.year));
|
|
21
|
+
|
|
22
|
+
const flush = (): Promise<void> => new Promise(resolve => setTimeout(resolve, 0));
|
|
23
|
+
|
|
24
|
+
const baseOptions = (overrides: Partial<SankeyOptions> = {}): SankeyOptions => ({
|
|
25
|
+
data: actualData,
|
|
26
|
+
country: 'US',
|
|
27
|
+
width: 1400,
|
|
28
|
+
...overrides
|
|
29
|
+
} as SankeyOptions);
|
|
30
|
+
|
|
31
|
+
const context = {country: 'China', firstYear: 1950, lastYear: 2020};
|
|
32
|
+
|
|
33
|
+
describe('header helpers', () => {
|
|
34
|
+
it('escapeHtml escapes markup and quotes', () => {
|
|
35
|
+
expect(escapeHtml(`<a href="x" onclick='y'>&</a>`))
|
|
36
|
+
.toBe('<a href="x" onclick='y'>&</a>');
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it('isHttpsUrl accepts only https URLs', () => {
|
|
40
|
+
expect(isHttpsUrl('https://example.org/logo.png')).toBe(true);
|
|
41
|
+
expect(isHttpsUrl('http://example.org/logo.png')).toBe(false);
|
|
42
|
+
expect(isHttpsUrl('javascript:alert(1)')).toBe(false);
|
|
43
|
+
expect(isHttpsUrl('data:image/png;base64,AAAA')).toBe(false);
|
|
44
|
+
expect(isHttpsUrl('not a url')).toBe(false);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('resolveHeader without options gives the historical defaults', () => {
|
|
48
|
+
expect(resolveHeader(undefined, context)).toEqual({
|
|
49
|
+
title: 'China energy usage',
|
|
50
|
+
subtitle: 'Energy Transitions in China History, 1950-2020',
|
|
51
|
+
attribution: 'Suits, Matteson, and Moyer (2020)',
|
|
52
|
+
affiliation: ['Center for Robust Decision-making on', 'Climate and Energy Policy, UChicago'],
|
|
53
|
+
logo: {...DEFAULT_HEADER.logo}
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('resolveHeader substitutes placeholders in every text field', () => {
|
|
58
|
+
const resolved = resolveHeader({
|
|
59
|
+
title: '{country}',
|
|
60
|
+
subtitle: '{firstYear} to {lastYear}',
|
|
61
|
+
attribution: 'Data for {country}, {lastYear}',
|
|
62
|
+
affiliation: ['{country} team']
|
|
63
|
+
}, context);
|
|
64
|
+
|
|
65
|
+
expect(resolved.title).toBe('China');
|
|
66
|
+
expect(resolved.subtitle).toBe('1950 to 2020');
|
|
67
|
+
expect(resolved.attribution).toBe('Data for China, 2020');
|
|
68
|
+
expect(resolved.affiliation).toEqual(['China team']);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it('resolveHeader hides null and blank values', () => {
|
|
72
|
+
const resolved = resolveHeader({
|
|
73
|
+
title: null,
|
|
74
|
+
subtitle: ' ',
|
|
75
|
+
attribution: null,
|
|
76
|
+
affiliation: ['', ' '],
|
|
77
|
+
logo: null
|
|
78
|
+
}, context);
|
|
79
|
+
|
|
80
|
+
expect(resolved).toEqual({
|
|
81
|
+
title: null, subtitle: null, attribution: null, affiliation: null, logo: null
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('resolveHeader uses a custom logo without falling back to the RDCEP link', () => {
|
|
86
|
+
expect(resolveHeader({logo: {src: 'https://example.org/l.png'}}, context).logo)
|
|
87
|
+
.toEqual({src: 'https://example.org/l.png', href: null, alt: ''});
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
describe('Sankey header rendering', () => {
|
|
92
|
+
let sankey: Sankey | null = null;
|
|
93
|
+
|
|
94
|
+
const text = (selector: string): string[] =>
|
|
95
|
+
Array.from(document.querySelectorAll(selector)).map(el => el.textContent ?? '');
|
|
96
|
+
|
|
97
|
+
const create = async (header?: SankeyHeaderOptions, country = 'US'): Promise<void> => {
|
|
98
|
+
sankey = new Sankey('sankey', baseOptions({header, country}));
|
|
99
|
+
await flush();
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
beforeEach(() => {
|
|
103
|
+
document.body.innerHTML = '<div id="sankey"></div>';
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
afterEach(() => {
|
|
107
|
+
sankey?.destroy();
|
|
108
|
+
sankey = null;
|
|
109
|
+
document.body.innerHTML = '';
|
|
110
|
+
jest.restoreAllMocks();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('renders the historical header when no header option is given', async () => {
|
|
114
|
+
await create();
|
|
115
|
+
|
|
116
|
+
expect(text('.evs-main-title')).toEqual(['US energy usage']);
|
|
117
|
+
expect(text('.evs-subtitle')).toEqual([`Energy Transitions in US History, ${firstYear}-${lastYear}`]);
|
|
118
|
+
expect(text('.evs-attribution')).toEqual(['Suits, Matteson, and Moyer (2020)']);
|
|
119
|
+
expect(text('.evs-affiliation-text')).toEqual([
|
|
120
|
+
'Center for Robust Decision-making on',
|
|
121
|
+
'Climate and Energy Policy, UChicago'
|
|
122
|
+
]);
|
|
123
|
+
|
|
124
|
+
const link = document.querySelector('.evs-header-logo a') as HTMLAnchorElement;
|
|
125
|
+
expect(link.getAttribute('href')).toBe('https://www.rdcep.org/');
|
|
126
|
+
const img = link.querySelector('img') as HTMLImageElement;
|
|
127
|
+
expect(img.getAttribute('src')).toBe(DEFAULT_HEADER.logo.src);
|
|
128
|
+
expect(img.getAttribute('alt')).toBe(DEFAULT_HEADER.logo.alt);
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('renders overridden values', async () => {
|
|
132
|
+
await create({
|
|
133
|
+
title: 'Energy in {country}',
|
|
134
|
+
attribution: 'Author A and Author B (2024)',
|
|
135
|
+
affiliation: ['Line one'],
|
|
136
|
+
logo: {src: 'https://example.org/l.png', href: 'https://example.org/', alt: 'Example'}
|
|
137
|
+
}, 'China');
|
|
138
|
+
|
|
139
|
+
expect(text('.evs-main-title')).toEqual(['Energy in China']);
|
|
140
|
+
expect(text('.evs-attribution')).toEqual(['Author A and Author B (2024)']);
|
|
141
|
+
expect(text('.evs-affiliation-text')).toEqual(['Line one']);
|
|
142
|
+
// Not overridden, so still the default, with the new country.
|
|
143
|
+
expect(text('.evs-subtitle')).toEqual([`Energy Transitions in China History, ${firstYear}-${lastYear}`]);
|
|
144
|
+
expect(document.querySelector('.evs-header-logo a')!.getAttribute('href')).toBe('https://example.org/');
|
|
145
|
+
expect(document.querySelector('.evs-header-logo img')!.getAttribute('alt')).toBe('Example');
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('hides elements set to null but keeps the animated overlays', async () => {
|
|
149
|
+
await create({title: null, subtitle: null, attribution: null, affiliation: null, logo: null});
|
|
150
|
+
|
|
151
|
+
expect(document.querySelector('.evs-main-title')).toBeNull();
|
|
152
|
+
expect(document.querySelector('.evs-header-info')).toBeNull();
|
|
153
|
+
expect(document.querySelector('.evs-header-affiliation')).toBeNull();
|
|
154
|
+
expect(document.querySelector('.evs-header-logo')).toBeNull();
|
|
155
|
+
|
|
156
|
+
expect(document.querySelector('.evs-energy-usage-overlay svg#evs-energy-usage-overlay')).not.toBeNull();
|
|
157
|
+
expect(document.querySelector('.evs-year-overlay svg#evs-year-overlay')).not.toBeNull();
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('keeps the info block when only one of subtitle and attribution is hidden', async () => {
|
|
161
|
+
await create({subtitle: null});
|
|
162
|
+
|
|
163
|
+
expect(document.querySelector('.evs-subtitle')).toBeNull();
|
|
164
|
+
expect(text('.evs-attribution')).toEqual(['Suits, Matteson, and Moyer (2020)']);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('renders markup in header text and country as plain text', async () => {
|
|
168
|
+
const payload = '<img src=x onerror="window.__xss=1"><script>window.__xss=1</script>';
|
|
169
|
+
await create({attribution: payload, affiliation: [payload]}, payload);
|
|
170
|
+
|
|
171
|
+
// Only the logo image exists; nothing was injected.
|
|
172
|
+
expect(document.querySelectorAll('.evs-title-container img')).toHaveLength(1);
|
|
173
|
+
expect(document.querySelectorAll('.evs-title-container script')).toHaveLength(0);
|
|
174
|
+
expect(text('.evs-attribution')).toEqual([payload]);
|
|
175
|
+
expect(text('.evs-affiliation-text')).toEqual([payload]);
|
|
176
|
+
expect(text('.evs-main-title')).toEqual([`${payload} energy usage`]);
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe('validation', () => {
|
|
180
|
+
const construct = (header: unknown) => () =>
|
|
181
|
+
new Sankey('sankey', baseOptions({header: header as SankeyHeaderOptions}));
|
|
182
|
+
|
|
183
|
+
it.each([
|
|
184
|
+
['javascript: logo src', {logo: {src: 'javascript:alert(1)'}}],
|
|
185
|
+
['http logo src', {logo: {src: 'http://example.org/l.png'}}],
|
|
186
|
+
['javascript: logo href', {logo: {src: 'https://example.org/l.png', href: 'javascript:alert(1)'}}],
|
|
187
|
+
['missing logo src', {logo: {}}],
|
|
188
|
+
['unknown header key', {author: 'x'}],
|
|
189
|
+
['unknown logo key', {logo: {src: 'https://example.org/l.png', onload: 'x'}}],
|
|
190
|
+
['non-string title', {title: 42}],
|
|
191
|
+
['non-array affiliation', {affiliation: 'one line'}],
|
|
192
|
+
['non-string affiliation line', {affiliation: ['ok', 3]}],
|
|
193
|
+
['header that is not an object', 'title']
|
|
194
|
+
])('rejects %s', (_label, header) => {
|
|
195
|
+
expect(construct(header)).toThrow(SankeyError);
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
});
|