Newer
Older
import { select, selectAll } from 'd3-selection';
import { provide } from '../../lib/redux';
import { attachToNode, timeSeriesGraph, timeSeriesLegend } from './index';
import { Actions, configureStore } from '../../store';
Naab, Daniel James
committed
const TEST_STATE = {
series: {
timeSeries: {
'00010:current': {
points: [{
value: 4,
qualifiers: ['P']
}],
method: 'method1',
Naab, Daniel James
committed
'00060:current': {
points: [{
Naab, Daniel James
committed
value: 10,
qualifiers: ['P']
}],
method: 'method1',
variable: '45807197'
Naab, Daniel James
committed
},
'00060:compare': {
points: [{
Naab, Daniel James
committed
value: 10,
qualifiers: ['P']
}],
method: 'method1',
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']
},
'coll4': {
variable: '45807190',
timeSeries: ['00010:current']
Naab, Daniel James
committed
}
},
Bucknell, Mary S.
committed
queryInfo: {
Bucknell, Mary S.
committed
notes: {
'filter:timeRange': {
mode: 'PERIOD',
periodDays: 7
},
Bucknell, Mary S.
committed
}
}
},
Naab, Daniel James
committed
requests: {
Naab, Daniel James
committed
timeSeriesCollections: ['coll1']
},
timeSeriesCollections: ['coll2', 'col4']
Naab, Daniel James
committed
}
},
variables: {
'45807197': {
variableCode: {
value: '00060'
},
oid: '45807197',
variableDescription: 'Test description for 00060',
Naab, Daniel James
committed
unit: {
unitCode: 'unitCode'
}
},
'45807190': {
variableCode: {
value: '00010'
},
oid: '45807190',
unit: {
unitCode: 'unitCode'
}
Naab, Daniel James
committed
}
},
methods: {
'method1': {
methodDescription: 'method description'
}
Naab, Daniel James
committed
}
},
statisticsData : {
median: {
'00060': {
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
'1234': [
{
month_nu: '2',
day_nu: '20',
p50_va: '40',
begin_yr: '1970',
end_yr: '2017',
loc_web_ds: 'This method'
}, {
month_nu: '2',
day_nu: '21',
p50_va: '41',
begin_yr: '1970',
end_yr: '2017',
loc_web_ds: 'This method'
}, {
month_nu: '2',
day_nu: '22',
p50_va: '42',
begin_yr: '1970',
end_yr: '2017',
loc_web_ds: 'This method'
}
]
}
}
},
Bucknell, Mary S.
committed
currentVariableID: '45807197',
requestedTimeRange: null,
Bucknell, Mary S.
committed
showSeries: {
current: true,
compare: true,
median: true
},
loadingTSKeys: []
Naab, Daniel James
committed
},
Bucknell, Mary S.
committed
ui: {
width: 400
}
Naab, Daniel James
committed
};
describe('Hydrograph charting module', () => {
Bucknell, Mary S.
committed
let graphNode;
Bucknell, Mary S.
committed
beforeEach(() => {
let body = select('body');
Bucknell, Mary S.
committed
let component = body.append('div')
Bucknell, Mary S.
committed
.attr('id', 'hydrograph');
component.append('div').attr('class', 'loading-indicator-container');
Bucknell, Mary S.
committed
component.append('div').attr('class', 'graph-container');
component.append('div').attr('class', 'select-time-series-container');
Bucknell, Mary S.
committed
component.append('div').attr('class', 'provisional-data-alert');
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);
Bucknell, Mary S.
committed
expect(svgNodes.length).toBe(1);
Bucknell, Mary S.
committed
expect(graphNode.innerHTML).toContain('hydrograph-container');
describe('container display', () => {
it('should not be hidden tag if there is data', () => {
const store = configureStore(TEST_STATE);
select(graphNode)
.call(provide(store))
.call(timeSeriesGraph);
expect(select('#hydrograph').attr('hidden')).toBeNull();
});
it('should have a style tag if there is no data', () => {
const store = configureStore({series: {timeSeries: {}}});
select(graphNode)
.call(provide(store))
.call(timeSeriesGraph);
});
});
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() {
Naab, Daniel James
committed
const descText = svg.select('desc').html();
Bucknell, Mary S.
committed
Naab, Daniel James
committed
expect(svg.select('title').html()).toEqual('Test title for 00060');
Bucknell, Mary S.
committed
expect(descText).toContain('Test description for 00060');
expect(descText).toContain('3/23/2018');
expect(descText).toContain('3/30/2018');
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');
xdescribe('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: 1514926800000,
endTime: 1514930400000,
Naab, Daniel James
committed
points: [{
Naab, Daniel James
committed
value: 10,
Naab, Daniel James
committed
qualifiers: ['P']
Naab, Daniel James
committed
qualifiers: ['P', 'FLD']
Bucknell, Mary S.
committed
}]
}
Naab, Daniel James
committed
}
Naab, Daniel James
committed
},
Bucknell, Mary S.
committed
showSeries: {
current: true,
compare: true,
median: true
},
currentDateRange: 'P7D',
loadingTSKeys: []
Bucknell, Mary S.
committed
ui: {
windowWidth: 400,
width: 400
}
Bucknell, Mary S.
committed
attachToNode(store, graphNode, {siteno: '123456788'});
it('should render the correct number of svg nodes', () => {
Bucknell, Mary S.
committed
// one main hydrograph, legend and two sparklines
expect(selectAll('svg').size()).toBe(4);
const titleDiv = selectAll('.time-series-graph-title');
expect(titleDiv.size()).toBe(1);
expect(titleDiv.text()).toEqual('Test title for 00060');
});
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 time series 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.
Bucknell, Mary S.
committed
expect(selectAll('.hydrograph-svg .line-segment').size()).toBe(2);
it('should render a rectangle for masked data', () => {
Bucknell, Mary S.
committed
expect(selectAll('.hydrograph-svg g.current-mask-group').size()).toBe(1);
Naab, Daniel James
committed
it('should have a point for the median stat data with a label', () => {
expect(selectAll('#median-points path').size()).toBe(1);
Bucknell, Mary S.
committed
expect(selectAll('#median-points text').size()).toBe(0);
it('should have tooltips for the select series table', () => {
// one for each of the two parameters
expect(selectAll('table .tooltip-item').size()).toBe(2);
Bucknell, Mary S.
committed
xit('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
});
//TODO: Consider adding a test which checks that the y axis is rescaled by
// examining the contents of the text labels.
let store;
beforeEach(() => {
store = configureStore(TEST_STATE);
select(graphNode)
.call(provide(store))
.call(timeSeriesLegend);
});
it('Should have 6 legend markers', () => {
expect(selectAll('.legend g').size()).toBe(6);
expect(selectAll('.legend g line.median-step').size()).toBe(1);
});
it('Should have four legend markers after the compare time series is removed', () => {
store.dispatch(Actions.toggleTimeSeries('compare', false));
expect(selectAll('.legend g').size()).toBe(4);
it('Should have two 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('.legend g').size()).toBe(2);
Bucknell, Mary S.
committed
});
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
describe('compare line', () => {
let store;
beforeEach(() => {
store = configureStore(TEST_STATE);
attachToNode(store, graphNode, {siteno: '12345678'});
});
it('Should render one lines', () => {
expect(selectAll('#ts-compare-group .line-segment').size()).toBe(1);
});
it('Should remove the lines when removing the compare time series', () => {
store.dispatch(Actions.toggleTimeSeries('compare', false));
expect(selectAll('#ts-compare-group .line-segment').size()).toBe(0);
});
});
describe('median lines', () => {
let store;
beforeEach(() => {
store = configureStore(TEST_STATE);
attachToNode(store, graphNode, {siteno: '12345678'});
});
it('Should render one lines', () => {
expect(selectAll('#median-points .median-data-series').size()).toBe(1);
});
it('Should remove the lines when removing the median statistics data', () => {
store.dispatch(Actions.toggleTimeSeries('median', false));
expect(selectAll('#median-points .median-data-series').size()).toBe(0);
});
});
xdescribe('hiding/show provisional alert', () => {
Bucknell, Mary S.
committed
it('Expects the provisional alert to be visible when time series data is provided', () => {
let store = configureStore(TEST_STATE);
attachToNode(store, graphNode, {siteno: '12345678'});
expect(select(graphNode).select('.provisional-data-alert').attr('hidden')).toBeNull();
});
it('Expects the provisional alert to be hidden when no time series data is provided', () => {
let store = configureStore({
...TEST_STATE,
series: {},
timeSeriesState: {
...TEST_STATE.timeSeriesState,
Bucknell, Mary S.
committed
currentVariableID: ''
}
Bucknell, Mary S.
committed
});
attachToNode(store, graphNode, {siteno: '12345678'});
expect(select(graphNode).select('.provisional-data-alert').attr('hidden')).toBe('true');
});
});
xdescribe('Creating date range controls', () => {
let store;
beforeEach(() => {
store = configureStore(TEST_STATE);
attachToNode(store, graphNode, {siteno: '12345678'});
});
it('Expects the date range controls to be created', () => {
let dateRangeContainer = select(graphNode).select('#ts-daterange-select-container');
let customDateDiv = select(graphNode).select('div#ts-customdaterange-select-container');
expect(dateRangeContainer.size()).toBe(1);
expect(dateRangeContainer.selectAll('input[type=radio]').size()).toBe(4);
expect(customDateDiv.attr('hidden')).toBe('true');
});
it('Expects to retrieve the extended time series when the radio buttons are change', () => {
spyOn(Actions, 'retrieveExtendedTimeSeries');
let lastRadio = select(graphNode).select('#one-year');
lastRadio.attr('checked', true);
lastRadio.dispatch('change');
expect(Actions.retrieveExtendedTimeSeries).toHaveBeenCalledWith('12345678', 'P1Y');
});
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
it('Expects to show the date range from when the Custom radio is selected', () => {
let customRadio = select(graphNode).select('#custom-date-range');
customRadio.attr('checked', true);
customRadio.dispatch('change');
let customDateDiv = select(graphNode).select('div#ts-customdaterange-select-container');
expect(customDateDiv.attr('hidden')).toBeNull();
let customDateAlertDiv = select(graphNode).select('#custom-date-alert-container');
expect(customDateAlertDiv.attr('hidden')).toBe('true');
});
it('Expects an alert to be thrown if custom dates are not provided.', () => {
let submitButton = select(graphNode).select('#custom-date-submit');
submitButton.dispatch('click');
let customDateAlertDiv = select(graphNode).select('#custom-date-alert');
expect(customDateAlertDiv.attr('hidden')).toBeNull();
expect(customDateAlertDiv.select('p').text()).toEqual('Both start and end dates must be specified.');
});
it('Expects and alert to be thrown if the end date is earier than the start date.', () => {
select(graphNode).select('#custom-start-date').property('value', '2063-04-05');
select(graphNode).select('#custom-end-date').property('value', '2063-04-03');
select(graphNode).select('#custom-date-submit').dispatch('click');
let customDateAlertDiv = select(graphNode).select('#custom-date-alert-container');
expect(customDateAlertDiv.attr('hidden')).toBeNull();
expect(customDateAlertDiv.select('p').text()).toEqual('The start date must precede the end date.');
});
it('Expects data to be retrieved if both custom start and end dates are provided', () => {
spyOn(Actions, 'getUserRequestedDataForDateRange');
select(graphNode).select('#custom-start-date').property('value', '2063-04-03');
select(graphNode).select('#custom-end-date').property('value', '2063-04-05');
select(graphNode).select('#custom-date-submit').dispatch('click');
let customDateAlertDiv = select(graphNode).select('#custom-date-alert-container');
expect(customDateAlertDiv.attr('hidden')).toBe('true');
expect(Actions.getUserRequestedDataForDateRange).toHaveBeenCalledWith(
'12345678', '2063-04-03', '2063-04-05'
);
});
});
xdescribe('Tests for loading indicators', () => {
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
it('Expects the graph loading indicator to be visible if the current 7 day data is being loaded', () => {
const newTestState = {
...TEST_STATE,
timeSeriesState: {
...TEST_STATE.timeSeriesState,
currentDateRange: 'P7D',
loadingTSKeys: ['current:P7D']
}
};
let store = configureStore(newTestState);
spyOn(store, 'dispatch');
attachToNode(store, graphNode, {siteno: '12345678'});
expect(select(graphNode).select('.loading-indicator-container').select('.loading-indicator').size()).toBe(1);
});
it('Expects the graph loading indicator to not be visible if the current 7 day data is not being loaded', () => {
let store = configureStore(TEST_STATE);
spyOn(store, 'dispatch');
attachToNode(store, graphNode, {siteno: '12345678'});
expect(select(graphNode).select('.loading-indicator-container').select('.loading-indicator').size()).toBe(0);
});
it('Expects the date range control loading indicator to be visible if loading is in progress for the selected date range', () => {
const newTestState = {
...TEST_STATE,
timeSeriesState: {
...TEST_STATE.timeSeriesState,
currentDateRange: 'P30D',
loadingTSKeys: ['current:P30D:00060']
}
};
let store = configureStore(newTestState);
attachToNode(store, graphNode, {siteno: '12345678'});
expect(select(graphNode).select('#ts-daterange-select-container').select('.loading-indicator').size()).toBe(1);
});
it('Expects the date range control loading indicator to notbe visible if not loading for the selected date range', () => {
const newTestState = {
...TEST_STATE,
timeSeriesState: {
...TEST_STATE.timeSeriesState,
currentDateRange: 'P30D',
loadingTSKeys: ['compare:P30D:00060']
}
};
let store = configureStore(newTestState);
attachToNode(store, graphNode, {siteno: '12345678'});
expect(select(graphNode).select('#ts-daterange-select-container').select('.loading-indicator').size()).toBe(0);
});
it('Expects that the no data alert will not be shown if there is data', () => {
let store = configureStore(TEST_STATE);
attachToNode(store, graphNode, {siteno: '12345678'});
expect(select(graphNode).select('#no-data-message').size()).toBe(0);
});
it('Expects the no data alert to be shown if there is no data', () => {
let newTestState = {
...TEST_STATE,
series: {
...TEST_STATE.series,
requests: {
'current:P7D': {
timeSeriesCollections: []
}
}
}
};
let store = configureStore(newTestState);
attachToNode(store, graphNode, {siteno: '12345678'});
expect(select(graphNode).select('#no-data-message').size()).toBe(1);
});
});