Newer
Older
import {select, selectAll} from 'd3-selection';
Bucknell, Mary S.
committed
import {enableFetchMocks, disableFetchMocks} from 'jest-fetch-mock';
import mockConsole from 'jest-mock-console';
Bucknell, Mary S.
committed
import * as utils from 'ui/utils';
import config from 'ui/config';
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
import {Actions as floodDataActions} from 'ml/store/flood-data';
import * as groundwaterLevelData from 'ml/store/groundwater-level-field-visits';
Bucknell, Mary S.
committed
import * as hydrographData from 'ml/store/hydrograph-data';
import * as hydrographParameters from 'ml/store/hydrograph-parameters';
import * as hydrographParameterSelectors from 'ml/selectors/hydrograph-parameters-selector';
Bucknell, Mary S.
committed
import {attachToNode} from './index';
Bucknell, Mary S.
committed
import {
TEST_CURRENT_TIME_RANGE,
TEST_GW_LEVELS,
TEST_HYDROGRAPH_PARAMETERS, TEST_STATS_DATA,
Bucknell, Mary S.
committed
TEST_PRIMARY_IV_DATA
} from './mock-hydrograph-state';
Naab, Daniel James
committed
Bucknell, Mary S.
committed
describe('monitoring-location/components/hydrograph module', () => {
Bucknell, Mary S.
committed
utils.mediaQuery = jest.fn().mockReturnValue(true);
utils.wrap = jest.fn();
Bucknell, Mary S.
committed
config.locationTimeZone = 'America/Chicago';
Bucknell, Mary S.
committed
config.ivPeriodOfRecord = {
Bucknell, Mary S.
committed
'72019': {
Bucknell, Mary S.
committed
begin_date: '01-02-2001',
end_date: '10-15-2015'
},
'00060': {
begin_date: '04-01-1991',
end_date: '10-15-2007'
},
Bucknell, Mary S.
committed
'00065': {
begin_date: '04-01-1991',
Bucknell, Mary S.
committed
end_date: '10-15-2007'
},
'00093': {
begin_date: '11-25-2001',
end_date: '03-01-2020'
},
'00067': {
begin_date: '04-01-1990',
end_date: '10-15-2006'
}
};
Bucknell, Mary S.
committed
config.gwPeriodOfRecord = {
Bucknell, Mary S.
committed
'72019': {
begin_date: '01-02-2000',
end_date: '10-15-2015'
}
};
Bucknell, Mary S.
committed
let graphNode;
Bucknell, Mary S.
committed
let nodeElem;
const INITIAL_PARAMETERS = {
siteno: '11112222',
Bucknell, Mary S.
committed
sitename: 'Site name',
parameterCode: '72019'
};
Bucknell, Mary S.
committed
let restoreConsole;
Bucknell, Mary S.
committed
beforeAll(() => {
enableFetchMocks();
restoreConsole = mockConsole();
});
afterAll(() => {
disableFetchMocks();
restoreConsole();
});
Bucknell, Mary S.
committed
beforeEach(() => {
Bucknell, Mary S.
committed
body.append('a')
.attr('id', 'classic-page-link')
Bucknell, Mary S.
committed
.attr('href', 'https://fakeserver/link');
Bucknell, Mary S.
committed
let component = body.append('div')
Bucknell, Mary S.
committed
.attr('id', 'hydrograph');
Bucknell, Mary S.
committed
component.append('div').attr('id', 'hydrograph-method-picker-container');
component.append('div').attr('class', 'graph-container')
.append('div')
.attr('id', 'hydrograph-loading-indicator-container')
.attr('class', 'loading-indicator-container');
component.append('div').attr('class', 'short-cut-time-span-container');
Bucknell, Mary S.
committed
component.append('div').attr('class', 'select-actions-container');
component.append('div').attr('class', 'select-time-series-container');
Bucknell, Mary S.
committed
component.append('div').attr('id', 'iv-data-table-container');
component.append('div').attr('class', 'daily-statistical-data');
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
graphNode = document.getElementById('hydrograph');
Bucknell, Mary S.
committed
nodeElem = select(graphNode);
Bucknell, Mary S.
committed
});
afterEach(() => {
select('#hydrograph').remove();
Bucknell, Mary S.
committed
select('#classic-page-link').remove();
Bucknell, Mary S.
committed
});
Bucknell, Mary S.
committed
describe('Tests for initial data fetching and setting', () => {
let store;
let retrieveHydrographDataSpy, retrieveHydrographParametersSpy,
retrieveFloodLevelsSpy;
beforeEach(() => {
store = configureStore({
Bucknell, Mary S.
committed
hydrographData: {},
groundwaterLevelData: {
all: []
},
Bucknell, Mary S.
committed
hydrographParameters: {},
floodData: {},
ui: {
width: 1000
Bucknell, Mary S.
committed
retrieveHydrographDataSpy = jest.spyOn(hydrographData, 'retrieveHydrographData');
retrieveHydrographParametersSpy = jest.spyOn(hydrographParameters, 'retrieveHydrographParameters');
Bucknell, Mary S.
committed
retrieveFloodLevelsSpy = jest.spyOn(floodDataActions, 'retrieveFloodLevels');
});
Bucknell, Mary S.
committed
it('Loading indicator should be shown', () => {
attachToNode(store, graphNode, INITIAL_PARAMETERS);
expect(nodeElem.select('.loading-indicator').size()).toBe(1);
});
Bucknell, Mary S.
committed
describe('Fetching initial hydrograph data', () => {
it('With just parameter code set', () => {
attachToNode(store, graphNode, INITIAL_PARAMETERS);
Bucknell, Mary S.
committed
expect(retrieveHydrographDataSpy).toHaveBeenCalledWith('11112222', 'USGS', {
Bucknell, Mary S.
committed
parameterCode: '72019',
Bucknell, Mary S.
committed
period: 'P7D',
startTime: null,
endTime: null,
loadCompare: false
},
true
);
Bucknell, Mary S.
committed
expect(store.getState().hydrographState).toEqual({
selectedParameterCode: '72019',
selectedIVMethodID: undefined
Bucknell, Mary S.
committed
});
});
Bucknell, Mary S.
committed
it('With custom period', () => {
attachToNode(store, graphNode, {
Bucknell, Mary S.
committed
...INITIAL_PARAMETERS,
period: 'P45D'
});
expect(retrieveHydrographDataSpy).toHaveBeenCalledWith('11112222', 'USGS', {
Bucknell, Mary S.
committed
parameterCode: '72019',
Bucknell, Mary S.
committed
period: 'P45D',
startTime: null,
endTime: null,
loadCompare: false
},
true
);
Bucknell, Mary S.
committed
expect(store.getState().hydrographState).toEqual({
selectedParameterCode: '72019',
selectedIVMethodID: undefined
Bucknell, Mary S.
committed
});
});
Bucknell, Mary S.
committed
it('With custom time range', () => {
attachToNode(store, graphNode, {
Bucknell, Mary S.
committed
...INITIAL_PARAMETERS,
startDT: '2020-02-01',
endDT: '2020-02-15'
expect(retrieveHydrographDataSpy).toHaveBeenCalledWith('11112222', 'USGS', {
Bucknell, Mary S.
committed
parameterCode: '72019',
Bucknell, Mary S.
committed
period: null,
startTime: '2020-02-01T00:00:00.000-06:00',
endTime: '2020-02-15T23:59:59.999-06:00',
loadCompare: false
},
true
);
Bucknell, Mary S.
committed
expect(store.getState().hydrographState).toEqual({
selectedParameterCode: '72019',
Bucknell, Mary S.
committed
start: '2020-02-01',
end: '2020-02-15'
},
selectedIVMethodID: undefined
Bucknell, Mary S.
committed
});
});
Bucknell, Mary S.
committed
it('With compare enabled', () => {
attachToNode(store, graphNode, {
Bucknell, Mary S.
committed
...INITIAL_PARAMETERS,
compare: true
expect(retrieveHydrographDataSpy).toHaveBeenCalledWith('11112222', 'USGS', {
Bucknell, Mary S.
committed
parameterCode: '72019',
Bucknell, Mary S.
committed
period: 'P7D',
startTime: null,
endTime: null,
loadCompare: true
},
true
);
Bucknell, Mary S.
committed
expect(store.getState().hydrographState).toEqual({
selectedParameterCode: '72019',
Bucknell, Mary S.
committed
showCompareIVData: true,
selectedIVMethodID: undefined
});
});
});
Bucknell, Mary S.
committed
Bucknell, Mary S.
committed
it('Should fetch the hydrograph parameters', () => {
attachToNode(store, graphNode, INITIAL_PARAMETERS);
expect(retrieveHydrographParametersSpy).toHaveBeenCalledWith('11112222', 'USGS');
});
Bucknell, Mary S.
committed
it('Should fetch the flood levels', () => {
Bucknell, Mary S.
committed
attachToNode(store, graphNode, INITIAL_PARAMETERS);
Bucknell, Mary S.
committed
expect(retrieveFloodLevelsSpy).toHaveBeenCalledWith('11112222');
});
Bucknell, Mary S.
committed
it('Should fetch the data and set the hydrograph state but not does not fetch hydrograph parameters when showOnlyGraph is true', () => {
attachToNode(store, graphNode, {
Bucknell, Mary S.
committed
...INITIAL_PARAMETERS,
showOnlyGraph: true
});
expect(retrieveHydrographDataSpy).toHaveBeenCalledWith('11112222', 'USGS', {
Bucknell, Mary S.
committed
parameterCode: '72019',
Bucknell, Mary S.
committed
period: 'P7D',
startTime: null,
endTime: null,
loadCompare: false
},
true
);
Bucknell, Mary S.
committed
expect(store.getState().hydrographState).toEqual({
selectedParameterCode: '72019',
Bucknell, Mary S.
committed
});
Bucknell, Mary S.
committed
expect(retrieveFloodLevelsSpy).toHaveBeenCalled();
Bucknell, Mary S.
committed
expect(retrieveHydrographParametersSpy).not.toHaveBeenCalled();
});
});
describe('Tests for rendering once fetching is complete when showOnlyGraph is false', () => {
Bucknell, Mary S.
committed
let store;
beforeEach(() => {
Bucknell, Mary S.
committed
store = configureStore({
Bucknell, Mary S.
committed
hydrographData: {
primaryIVData: TEST_PRIMARY_IV_DATA,
currentTimeRange: TEST_CURRENT_TIME_RANGE,
statisticsData: TEST_STATS_DATA
Bucknell, Mary S.
committed
},
groundwaterLevelData: {
all: [TEST_GW_LEVELS]
},
hydrographState: {
selectedIVMethodID: '90649',
selectedParameterCode: null,
Bucknell, Mary S.
committed
hydrographParameters: TEST_HYDROGRAPH_PARAMETERS,
floodData: {},
Bucknell, Mary S.
committed
ui: {
Bucknell, Mary S.
committed
width: 1000
Bucknell, Mary S.
committed
}
Bucknell, Mary S.
committed
});
hydrographData.retrieveHydrographData = jest.fn(() => {
return function() {
return Promise.resolve();
};
});
hydrographParameters.retrieveHydrographParameters = jest.fn(() => {
return function() {
return Promise.resolve();
};
});
hydrographParameterSelectors.latestSelectedParameterValue = jest.fn().mockReturnValue(() => {
return function() {
return 123;
attachToNode(store, graphNode, {
...INITIAL_PARAMETERS,
showOnlyGraph: false
jest.useFakeTimers('modern');
jest.setSystemTime(new Date(2020, 0, 1));
});
afterEach(() => {
jest.useRealTimers();
Bucknell, Mary S.
committed
it('loading indicator should be hidden', () => {
expect(nodeElem.select('.loading-indicator').size()).toBe(0);
Bucknell, Mary S.
committed
});
it('should render the correct number of svg nodes', () => {
expect(select('#hydrograph').select('.graph-container').selectAll('svg').size()).toBe(5);
Bucknell, Mary S.
committed
it('should have a title div', () => {
const titleDiv = selectAll('.time-series-graph-title');
expect(titleDiv.size()).toBe(1);
expect(titleDiv.select('div').text()).toContain('Depth to water level');
expect(titleDiv.select('.usa-tooltip').text()).toEqual('Depth to water level, feet');
});
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(3);
it('should render time series data as a line', () => {
// There should be four segments
expect(selectAll('.hydrograph-svg .line-segment').size()).toBe(4);
});
it('should render a rectangle for masked data', () => {
expect(selectAll('.hydrograph-svg g.iv-mask-group').size()).toBe(1);
});
Bucknell, Mary S.
committed
it('should have a point for the median stat data with a label', () => {
expect(selectAll('#median-points path').size()).toBe(2);
expect(selectAll('#median-points text').size()).toBe(0);
});
Bucknell, Mary S.
committed
it('should have brush element for the hydrograph', () => {
expect(selectAll('.brush').size()).toBe(1);
});
Bucknell, Mary S.
committed
it('should have .cursor-slider-svg element', () => {
expect(selectAll('.cursor-slider-svg').size()).toBe(1);
});
Bucknell, Mary S.
committed
it('should have method select element', () => {
expect(selectAll('.method-picker-container').size()).toBe(1);
it('should have the select time series element', () => {
expect(selectAll('.main-parameter-selection-container').size()).toBe(1);
it('should have data tables for hydrograph data', () => {
expect(select('#iv-primary-table-container').size()).toBe(1);
expect(select('#iv-secondary-table-container').size()).toBe(1);
Bucknell, Mary S.
committed
expect(select('#gw-table-container').size()).toBe(1);
it('expects to create the daily statistics section', () => {
expect(select('.stats-accordion').size()).toBe(1);
});
describe('Tests for rendering once fetching is complete when showOnlyGraph is true', () => {
let store;
beforeEach(() => {
store = configureStore({
hydrographData: {
primaryIVData: TEST_PRIMARY_IV_DATA,
currentTimeRange: TEST_CURRENT_TIME_RANGE,
statisticsData: TEST_STATS_DATA
groundwaterLevelData: {
all: [TEST_GW_LEVELS]
},
hydrographState: {
selectedIVMethodID: '90649',
},
hydrographParameters: TEST_HYDROGRAPH_PARAMETERS,
floodData: {},
ui: {
width: 1000
}
hydrographData.retrieveHydrographData = jest.fn(() => {
return function() {
return Promise.resolve();
};
});
groundwaterLevelData.retrieveGroundwaterLevelData = jest.fn(() => {
return function() {
return Promise.resolve();
};
});
hydrographParameters.retrieveHydrographParameters = jest.fn(() => {
return function() {
return Promise.resolve();
};
});
attachToNode(store, graphNode, {
...INITIAL_PARAMETERS,
showOnlyGraph: true
});
});
Bucknell, Mary S.
committed
it('loading indicator should be hidden', () => {
expect(nodeElem.select('.loading-indicator').size()).toBe(0);
});
Bucknell, Mary S.
committed
it('should render the correct number of svg nodes', () => {
expect(selectAll('svg').size()).toBe(1);
Bucknell, Mary S.
committed
it('should have a title div', () => {
const titleDiv = selectAll('.time-series-graph-title');
expect(titleDiv.size()).toBe(1);
});
Bucknell, Mary S.
committed
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(3);
Bucknell, Mary S.
committed
it('should render time series data as a line', () => {
// There should be four segments
expect(selectAll('.hydrograph-svg .line-segment').size()).toBe(4);
});
Bucknell, Mary S.
committed
it('should render a rectangle for masked data', () => {
expect(selectAll('.hydrograph-svg g.iv-mask-group').size()).toBe(1);
});
it('should have a point for the median stat data with a label', () => {
expect(selectAll('#median-points path').size()).toBe(2);
expect(selectAll('#median-points text').size()).toBe(0);
});
it('should not have brush element for the hydrograph', () => {
expect(selectAll('.brush').size()).toBe(0);
});
it('should not have .cursor-slider-svg element', () => {
expect(selectAll('.cursor-slider-svg').size()).toBe(0);
});
it('should not have time span elements', () => {
expect(selectAll('#change-time-span-container').size()).toBe(0);
});
it('should not have the download data element', () => {
expect(selectAll('#download-graph-data-container-select-actions').size()).toBe(0);
});
it('should not have method select element', () => {
expect(selectAll('#ts-method-select-container').size()).toBe(0);
});
it('should not have the select time series element', () => {
expect(selectAll('#select-time-series').size()).toBe(0);
});
it('should not have data tables for hydrograph data', () => {
expect(select('#iv-primary-table-container').size()).toBe(0);
expect(select('#iv-secondary-table-container').size()).toBe(0);
expect(select('#gw-table-container').size()).toBe(0);
it('expects to not create the daily statistics section', () => {
expect(select('.stats-accordion').size()).toBe(0);