Newer
Older
const { select, selectAll } = require('d3-selection');
const { provide } = require('../../lib/redux');
const { attachToNode, timeSeriesGraph } = require('./index');
const { Actions, configureStore } = require('./store');
Naab, Daniel James
committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const TEST_STATE = {
series: {
timeSeries: {
'00060:current': {
startTime: new Date('2018-01-02T15:00:00.000-06:00'),
endTime: new Date('2018-01-02T15:00:00.000-06:00'),
points: [{
dateTime: new Date('2018-01-02T15:00:00.000-06:00'),
value: 10,
qualifiers: ['P']
}]
},
'00060:compare': {
startTime: new Date('2018-01-02T15:00:00.000-06:00'),
endTime: new Date('2018-01-02T15:00:00.000-06:00'),
points: [{
dateTime: new Date('2018-01-02T15:00:00.000-06:00'),
value: 10,
qualifiers: ['P']
}]
}
},
timeSeriesCollections: {
'coll1': {
variable: 45807197,
timeSeries: ['00060:current']
},
'coll2': {
variable: 45807197,
timeSeries: ['00060:compare']
}
},
requests: {
current: {
timeSeriesCollections: ['coll1']
},
compare: {
timeSeriesCollections: ['coll2']
}
},
variables: {
'45807197': {
variableCode: '00060',
oid: 45807197,
unit: {
unitCode: 'unitCode'
}
}
}
},
currentVariableID: '45807197',
showSeries: {
current: true,
compare: true
},
width: 400
};
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', () => {
Naab, Daniel James
committed
const store = configureStore(TEST_STATE);
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(() => {
Naab, Daniel James
committed
const store = configureStore(TEST_STATE);
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
...TEST_STATE,
series: {
...TEST_STATE.series,
timeSeries: {
...TEST_STATE.series.timeSeries,
'00060:current': {
...TEST_STATE.series.timeSeries['00060:current'],
startTime: new Date('2018-01-02T15:00:00.000-06:00'),
endTime: new Date('2018-01-02T16:00:00.000-06:00'),
points: [{
dateTime: new Date('2018-01-02T15:00:00.000-06:00'),
Naab, Daniel James
committed
value: 10,
Naab, Daniel James
committed
qualifiers: ['P']
Naab, Daniel James
committed
dateTime: new Date('2018-01-02T16:00:00.000-06:00'),
Naab, Daniel James
committed
qualifiers: ['P', 'FLD']
}]
Naab, Daniel James
committed
}
}
Naab, Daniel James
committed
}
});
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 have a defs node', () => {
expect(selectAll('defs').size()).toBe(1);
expect(selectAll('defs mask').size()).toBe(1);
expect(selectAll('defs pattern').size()).toBe(2);
});
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.
Naab, Daniel James
committed
expect(selectAll('svg path.line').size()).toBe(2);
it('should render a rectangle for masked data', () => {
expect(selectAll('g.current-mask-group').size()).toBe(1);
});
Naab, Daniel James
committed
xit('should have a point for the median stat data with a label', () => {
Bucknell, Mary S.
committed
expect(selectAll('svg #median-points circle.median-data-series').size()).toBe(1);
expect(selectAll('svg #median-points text').size()).toBe(0);
Naab, Daniel James
committed
xit('should have a legend with two markers', () => {
expect(selectAll('g.legend-marker').size()).toBe(4);
Bucknell, Mary S.
committed
Naab, Daniel James
committed
xit('show the labels for the median stat data showMedianStatsLabel is true', () => {
Bucknell, Mary S.
committed
store.dispatch(Actions.showMedianStatsLabel(true));
Bucknell, Mary S.
committed
expect(selectAll('svg #median-points text').size()).toBe(1);
Bucknell, Mary S.
committed
});
describe('Adding and removing compare time series', () => {
Bucknell, Mary S.
committed
/* eslint no-use-before-define: "ignore" */
Bucknell, Mary S.
committed
beforeEach(() => {
Naab, Daniel James
committed
store = configureStore(TEST_STATE);
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);
});
Naab, Daniel James
committed
xit('Should have three legend markers', () => {
expect(selectAll('g.legend-marker').size()).toBe(5);
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);
});
Naab, Daniel James
committed
xit('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(3);
//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
}
];