Newer
Older
const { select, selectAll } = require('d3-selection');
const { reduxProvide: provide } = require('d3-redux');
const { attachToNode, getNearestTime, timeSeriesGraph } = require('./index');
const { Actions, configureStore } = require('./store');
describe('Hydrograph charting module', () => {
Bucknell, Mary S.
committed
let graphNode;
Bucknell, Mary S.
committed
beforeEach(() => {
select('body')
.append('div')
.attr('id', 'hydrograph');
graphNode = document.getElementById('hydrograph')
});
afterEach(() => {
select('#hydrograph').remove();
});
it('empty graph displays warning', () => {
attachToNode(graphNode, {});
Bucknell, Mary S.
committed
expect(graphNode.innerHTML).toContain('No data is available');
});
it('single data point renders', () => {
const store = configureStore({
tsData: {
current: {
data: [{
time: new Date(),
value: 10,
label: 'Label'
}],
show: true
},
compare: {
data: [],
show: false
}
},
title: '',
desc: ''
Bucknell, Mary S.
committed
});
select(graphNode)
.call(provide(store))
.call(timeSeriesGraph, store);
Bucknell, Mary S.
committed
expect(graphNode.innerHTML).toContain('hydrograph-container');
describe('SVG has been made accessibile', () => {
let svg;
beforeEach(() => {
const store = configureStore({
tsData: {
current: {
data: [{
time: new Date(),
value: 10,
label: 'Label'
}],
show: true
},
compare: {
data: [],
show: false
}
},
title: 'My Title',
desc: 'My Description',
});
select(graphNode)
.call(provide(store))
.call(timeSeriesGraph, store);
svg = select('svg');
});
it('title and desc attributes are present', function() {
expect(svg.attr('title'), 'My Title');
expect(svg.attr('desc'), 'My Description');
let labelledBy = svg.attr('aria-labelledby');
expect(labelledBy).toContain('title');
expect(labelledBy).toContain('desc');
});
it('svg should be focusable', function() {
expect(svg.attr('tabindex')).toBe('0');
});
});
describe('Renders real data from site #05370000', () => {
Bucknell, Mary S.
committed
/* eslint no-use-before-define: "ignore" */
beforeEach(() => {
const store = configureStore({
tsData: {
current: {
data: MOCK_DATA,
show: true
},
compare: {
data: [],
show: false
}
},
title: 'My Title',
desc: 'My Description',
});
select(graphNode)
.call(provide(store))
.call(timeSeriesGraph, store);
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
});
it('should render an svg node', () => {
expect(selectAll('svg').size()).toBe(1);
});
it('should render timeseries data as a line', () => {
// There is not a good way to validate that <path d="..."/>
// has the correct data, but we can validate that tooltips display
// at the correct location.
// First, confirm the chart line exists.
expect(selectAll('svg path.line').size()).toBe(1);
});
});
describe('Hydrograph tooltips', () => {
let data = [12, 13, 14, 15, 16].map(hour => {
return {
time: new Date(`2018-01-03T${hour}:00:00.000Z`),
label: 'label',
value: 0
};
});
it('return correct data points via getNearestTime' , () => {
// Check each date with the given offset against the hourly-spaced
// test data.
function expectOffset(offset, side) {
for (let [index, datum] of data.entries()) {
let expected;
Bucknell, Mary S.
committed
if (side === 'left' || index === data.length - 1) {
expected = {datum, index};
} else {
expected = {datum: data[index + 1], index: index + 1};
}
let time = new Date(datum.time.getTime() + offset);
let returned = getNearestTime(data, time);
expect(returned.datum.time).toBe(expected.datum.time);
expect(returned.datum.index).toBe(expected.datum.index);
}
}
let hour = 3600000; // 1 hour in milliseconds
// Check each date against an offset from itself.
expectOffset(0, 'left');
expectOffset(1, 'left');
expectOffset(hour / 2 - 1, 'left');
expectOffset(hour / 2, 'left');
expectOffset(hour / 2 + 1, 'right');
expectOffset(hour - 1, 'right');
});
});
Bucknell, Mary S.
committed
describe('Adding and removing compare time series', () => {
Bucknell, Mary S.
committed
/* eslint no-use-before-define: "ignore" */
let hydrograph;
Bucknell, Mary S.
committed
beforeEach(() => {
store = configureStore({
tsData: {
current: {
data: MOCK_DATA,
show: true
},
compare: {
data: MOCK_DATA_FOR_PREVIOUS_YEAR,
show: true
}
},
title: 'My Title',
desc: 'My Description',
});
select(graphNode)
.call(provide(store))
.call(timeSeriesGraph, store);
Bucknell, Mary S.
committed
});
it('Should render two lines', () => {
expect(selectAll('svg path.line').size()).toBe(2);
});
it('Should remove one of the lines when removing the compare time series', () => {
store.dispatch(Actions.toggleTimeseries('compare', false));
Bucknell, Mary S.
committed
expect(selectAll('svg path.line').size()).toBe(1);
});
//TODO: Consider adding a test which checks that the y axis is rescaled by
// examining the contents of the text labels.
Bucknell, Mary S.
committed
});
});
Bucknell, Mary S.
committed
const MOCK_DATA = [
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
{
"label": "1/3/2018, 10:00:00 AM -0600\n24.0 ft3/s",
"time": "2018-01-03T16:00:00.000Z",
"value": 24
},
{
"label": "1/3/2018, 10:15:00 AM -0600\n24.6 ft3/s",
"time": "2018-01-03T16:15:00.000Z",
"value": 24
},
{
"label": "1/3/2018, 10:30:00 AM -0600\n24.6 ft3/s",
"time": "2018-01-03T16:30:00.000Z",
"value": 24
},
{
"label": "1/3/2018, 10:45:00 AM -0600\n25.0 ft3/s",
"time": "2018-01-03T16:45:00.000Z",
"value": 25
},
{
"label": "1/3/2018, 11:00:00 AM -0600\n24.6 ft3/s",
"time": "2018-01-03T17:00:00.000Z",
"value": 24
},
{
"label": "1/3/2018, 11:15:00 AM -0600\n24.6 ft3/s",
"time": "2018-01-03T17:15:00.000Z",
"value": 24
},
{
"label": "1/3/2018, 11:30:00 AM -0600\n24.0 ft3/s",
"time": "2018-01-03T17:30:00.000Z",
"value": 24
},
{
"label": "1/3/2018, 11:45:00 AM -0600\n24.0 ft3/s",
"time": "2018-01-03T17:45:00.000Z",
"value": 24
},
{
"label": "1/3/2018, 12:00:00 PM -0600\n24.0 ft3/s",
"time": "2018-01-03T18:00:00.000Z",
"value": 24
},
{
"label": "1/3/2018, 12:15:00 PM -0600\n24.0 ft3/s",
"time": "2018-01-03T18:15:00.000Z",
"value": 24
}
Bucknell, Mary S.
committed
const MOCK_DATA_FOR_PREVIOUS_YEAR = [
{
"label": "1/3/2017, 10:00:00 AM -0600\n24.0 ft3/s",
"time": "2017-01-03T16:00:00.000Z",
Bucknell, Mary S.
committed
},
{
"label": "1/3/2017, 10:15:00 AM -0600\n24.6 ft3/s",
"time": "2017-01-03T16:15:00.000Z",
"value": 24
},
{
"label": "1/3/2017, 10:30:00 AM -0600\n24.6 ft3/s",
"time": "2017-01-03T16:30:00.000Z",
Bucknell, Mary S.
committed
},
{
"label": "1/3/2017, 10:45:00 AM -0600\n25.0 ft3/s",
"time": "2017-01-03T16:45:00.000Z",
Bucknell, Mary S.
committed
},
{
"label": "1/3/2017, 11:00:00 AM -0600\n24.6 ft3/s",
"time": "2017-01-03T17:00:00.000Z",
Bucknell, Mary S.
committed
},
{
"label": "1/3/2017, 11:15:00 AM -0600\n24.6 ft3/s",
"time": "2017-01-03T17:15:00.000Z",
Bucknell, Mary S.
committed
},
{
"label": "1/3/2017, 11:30:00 AM -0600\n24.0 ft3/s",
"time": "2017-01-03T17:30:00.000Z",
Bucknell, Mary S.
committed
},
{
"label": "1/3/2017, 11:45:00 AM -0600\n24.0 ft3/s",
"time": "2017-01-03T17:45:00.000Z",
Bucknell, Mary S.
committed
},
{
"label": "1/3/2018, 12:00:00 PM -0600\n24.0 ft3/s",
"time": "2018-01-03T18:00:00.000Z",
"value": 24
},
{
"label": "1/3/2018, 12:15:00 PM -0600\n24.0 ft3/s",
"time": "2018-01-03T18:15:00.000Z",
"value": 24
}
];