Newer
Older
const { select, selectAll } = require('d3-selection');
const { provide } = require('../../lib/redux');
const { attachToNode, timeSeriesGraph, timeSeriesLegend } = require('./index');
Bucknell, Mary S.
committed
const { Actions, configureStore } = require('../../store');
Naab, Daniel James
committed
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']
}],
method: 'method1',
Naab, Daniel James
committed
tsKey: 'current',
variable: '45807197'
Naab, Daniel James
committed
},
'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']
}],
method: 'method1',
Naab, Daniel James
committed
tsKey: 'compare',
variable: '45807197'
},
'00060:median': {
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
}],
metadata: {
beginYear: '2010',
endYear: '2015'
},
method: 'method1',
Naab, Daniel James
committed
tsKey: 'median',
variable: '45807197'
Naab, Daniel James
committed
}
},
timeSeriesCollections: {
'coll1': {
variable: '45807197',
Naab, Daniel James
committed
timeSeries: ['00060:current']
},
'coll2': {
variable: '45807197',
Naab, Daniel James
committed
timeSeries: ['00060:compare']
},
'coll3': {
variable: '45807197',
timeSeries: ['00060:median']
Naab, Daniel James
committed
}
},
requests: {
current: {
timeSeriesCollections: ['coll1']
},
compare: {
timeSeriesCollections: ['coll2']
},
median: {
timeSeriesCollections: ['coll3']
Naab, Daniel James
committed
}
},
variables: {
'45807197': {
variableCode: '00060',
oid: '45807197',
Naab, Daniel James
committed
unit: {
unitCode: 'unitCode'
}
}
},
methods: {
'method1': {
methodDescription: 'method description'
}
Naab, Daniel James
committed
}
},
currentVariableID: '45807197',
showSeries: {
current: true,
compare: true,
median: true
Naab, Daniel James
committed
},
width: 400
};
describe('Hydrograph charting module', () => {
Bucknell, Mary S.
committed
let graphNode;
Bucknell, Mary S.
committed
beforeEach(() => {
select('body')
.append('div')
.attr('id', 'hydrograph');
Bucknell, Mary S.
committed
graphNode = document.getElementById('hydrograph');
Bucknell, Mary S.
committed
});
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);
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');
Bucknell, Mary S.
committed
expect(svg.attr('aria-labelledby')).toContain('title');
expect(svg.attr('aria-describedby')).toContain('desc');
});
it('svg should be focusable', function() {
expect(svg.attr('tabindex')).toBe('0');
it('should have an accessibility table for each time series', function() {
expect(selectAll('table.usa-sr-only').size()).toBe(3);
it('should have a div for each type of time series', function() {
expect(selectAll('div#sr-only-median').size()).toBe(1);
expect(selectAll('div#sr-only-compare').size()).toBe(1);
expect(selectAll('div#sr-only-current').size()).toBe(1);
Bucknell, Mary S.
committed
});
Bucknell, Mary S.
committed
describe('SVG contains the expected elements', () => {
Bucknell, Mary S.
committed
/* eslint no-use-before-define: 0 */
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']
Bucknell, Mary S.
committed
}]
}
Naab, Daniel James
committed
}
Naab, Daniel James
committed
},
showSeries: {
current: true,
compare: true,
median: true
},
title: 'My Title',
Bucknell, Mary S.
committed
showMedianStatsLabel: false,
width: 400,
currentVariableID: '45807197'
});
select(graphNode)
.call(provide(store))
Naab, Daniel James
committed
.call(timeSeriesGraph);
});
it('should render an svg node', () => {
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', () => {
Naab, Daniel James
committed
// There should be one segment per time-series. Each is a single
// point, so should be a circle.
expect(selectAll('svg .line-segment').size()).toBe(2);
it('should render a rectangle for masked data', () => {
expect(selectAll('g.current-mask-group').size()).toBe(1);
});
Naab, Daniel James
committed
it('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);
it('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);
});
it('should have tooltips for the select series table', () => {
expect(selectAll('table .tooltip-table').size()).toBe(1);
});
Bucknell, Mary S.
committed
it('should not have tooltips for the select series table when the screen is large', () => {
store.dispatch(Actions.resizeUI(800, 800));
expect(selectAll('table .tooltip-table').size()).toBe(0);
Bucknell, Mary S.
committed
});
describe('Adding and removing compare time series', () => {
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', () => {
Naab, Daniel James
committed
expect(selectAll('svg .line-segment').size()).toBe(2);
Bucknell, Mary S.
committed
});
it('Should remove one of the lines when removing the compare time series', () => {
store.dispatch(Actions.toggleTimeseries('compare', false));
Naab, Daniel James
committed
expect(selectAll('svg .line-segment').size()).toBe(1);
Bucknell, Mary S.
committed
});
//TODO: Consider adding a test which checks that the y axis is rescaled by
// examining the contents of the text labels.
});
describe('legends should render', () => {
let store;
beforeEach(() => {
store = configureStore(TEST_STATE);
select(graphNode)
.call(provide(store))
.call(timeSeriesLegend);
});
it('Should have three legend markers', () => {
expect(selectAll('g.legend-marker').size()).toBe(3);
});
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);
it('Should have one legend marker after the compare and median time series are removed', () => {
store.dispatch(Actions.toggleTimeseries('compare', false));
store.dispatch(Actions.toggleTimeseries('median', false));
expect(selectAll('g.legend-marker').size()).toBe(1);
});
Bucknell, Mary S.
committed
});