Newer
Older
const { select, selectAll } = require('d3-selection');
const { provide } = require('../../lib/redux');
const { attachToNode, 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: {
Naab, Daniel James
committed
current: {
'00060': {
values: [{
time: new Date(),
value: 10,
label: 'Label',
qualifiers: ['P'],
approved: false,
estimated: false
}],
}
},
compare: {
'00060': {
values: []
}
},
medianStatistics: {
'00060': {
values: []
}
}
Naab, Daniel James
committed
},
showSeries: {
current: true,
Naab, Daniel James
committed
compare: false,
medianStatistics: true
},
title: '',
Bucknell, Mary S.
committed
});
select(graphNode)
.call(provide(store))
Naab, Daniel James
committed
.call(timeSeriesGraph);
let svgNodes = graphNode.getElementsByTagName('svg');
expect(svgNodes.length).toBe(1);
expect(svgNodes[0].getAttribute('viewBox')).toContain('400 200');
Bucknell, Mary S.
committed
expect(graphNode.innerHTML).toContain('hydrograph-container');
describe('SVG has been made accessibile', () => {
let svg;
beforeEach(() => {
const store = configureStore({
tsData: {
Naab, Daniel James
committed
current: {
'00060': {
values: [{
time: new Date(),
value: 10,
label: 'Label',
qualifiers: ['P'],
approved: false,
estimated: false
}],
},
},
compare: {
'00060': {
values: []
}
},
medianStatistics: {
'00060': {
values: []
}
},
Naab, Daniel James
committed
},
showSeries: {
current: true,
Naab, Daniel James
committed
compare: false,
medianStatistics: true,
title: 'My Title',
desc: 'My Description',
select(graphNode)
.call(provide(store))
Naab, Daniel James
committed
.call(timeSeriesGraph);
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');
});
});
Bucknell, Mary S.
committed
describe('SVG contains the expected elements', () => {
Bucknell, Mary S.
committed
/* eslint no-use-before-define: "ignore" */
Bucknell, Mary S.
committed
let store;
beforeEach(() => {
Bucknell, Mary S.
committed
store = configureStore({
Naab, Daniel James
committed
current: {
'00060': {
values: [{
time: new Date(),
value: 10,
label: 'Label',
qualifiers: ['P'],
approved: false,
estimated: false
}],
},
},
compare: {
'00060': {
values: []
}
},
medianStatistics: {
'00060': {
values: MOCK_MEDIAN_STAT_DATA
}
}
Naab, Daniel James
committed
},
showSeries: {
current: true,
compare: false,
medianStatistics: true
},
title: 'My Title',
Bucknell, Mary S.
committed
showMedianStatsLabel: false,
});
select(graphNode)
.call(provide(store))
Naab, Daniel James
committed
.call(timeSeriesGraph);
});
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);
});
Naab, Daniel James
committed
it('should have a point for the median stat data with a label', () => {
expect(selectAll('svg circle#median-point').size()).toBe(1);
Bucknell, Mary S.
committed
expect(selectAll('svg text#median-text').size()).toBe(0);
it('should have a legend with two markers', () => {
expect(selectAll('g.legend-marker').size()).toBe(2);
});
Bucknell, Mary S.
committed
it('show the labels for the median stat data showMedianStatsLabel is true', () => {
Bucknell, Mary S.
committed
store.dispatch(Actions.showMedianStatsLabel(true));
expect(selectAll('svg text#median-text').size()).toBe(1);
});
describe('Adding and removing compare time series', () => {
Bucknell, Mary S.
committed
/* eslint no-use-before-define: "ignore" */
Bucknell, Mary S.
committed
beforeEach(() => {
store = configureStore({
tsData: {
Naab, Daniel James
committed
207
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
current: {
'00060': {
values: [{
time: new Date(),
value: 10,
label: 'Label',
qualifiers: ['P'],
approved: false,
estimated: false
}],
}
},
compare: {
'00060': {
values: [{
time: new Date(),
value: 10,
label: 'Label',
qualifiers: ['P'],
approved: false,
estimated: false
}],
}
},
medianStatistics: {
'00060': {
values: []
}
}
Naab, Daniel James
committed
},
showSeries: {
current: true,
Naab, Daniel James
committed
compare: true,
medianStatistics: true
},
title: 'My Title',
desc: 'My Description',
});
select(graphNode)
.call(provide(store))
Naab, Daniel James
committed
.call(timeSeriesGraph);
Bucknell, Mary S.
committed
});
it('Should render two lines', () => {
expect(selectAll('svg path.line').size()).toBe(2);
});
it('Should have three legend markers', () => {
expect(selectAll('g.legend-marker').size()).toBe(3);
});
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);
});
it('Should have two legend markers after the compare time series is removed', () => {
store.dispatch(Actions.toggleTimeseries('compare', false));
expect(selectAll('g.legend-marker').size()).toBe(2);
});
//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
});
});
const MOCK_MEDIAN_STAT_DATA = [
{
"label": "18 ft3/s",
"time": "2017-01-03T00:00:00.000Z",
"value": 18
}
];