From e5e9807d7592219f5512429b638d469c1fe13e2e Mon Sep 17 00:00:00 2001 From: mbucknell <mbucknell@usgs.gov> Date: Wed, 24 Feb 2021 08:28:44 -0600 Subject: [PATCH] Finished up hydrograph/selectors tests. --- .../hydrograph/selectors/time-series-data.js | 32 +- .../selectors/time-series-data.test.js | 364 +++++------------- 2 files changed, 103 insertions(+), 293 deletions(-) diff --git a/assets/src/scripts/monitoring-location/components/hydrograph/selectors/time-series-data.js b/assets/src/scripts/monitoring-location/components/hydrograph/selectors/time-series-data.js index 7a3ac01bd..c081ec0e1 100644 --- a/assets/src/scripts/monitoring-location/components/hydrograph/selectors/time-series-data.js +++ b/assets/src/scripts/monitoring-location/components/hydrograph/selectors/time-series-data.js @@ -11,11 +11,9 @@ const formatTime = function(timeInMillis) { return DateTime.fromMillis(timeInMillis, {zone: config.locationTimeZone}).toFormat('L/d/yyyy tt ZZZ'); }; /** - * Factory function creates a function that: - * Returns the current show state of a time series. - * @param {Object} state Redux store - * @param {String} tsKey Time series key - * @return {Boolean} Show state of the time series + * Returns a Redux selector function which returns whether the dataKind time series is visible + * @param {String} dataKind - 'primary', 'compare', 'median' + * @return {Function} */ export const isVisible = memoize(dataKind => createSelector( isCompareIVDataVisible, @@ -34,24 +32,9 @@ export const isVisible = memoize(dataKind => createSelector( }) ); -/** - * Returns a Redux selector function which returns the label to be used for the Y axis - */ -export const getYLabel = createSelector( - getPrimaryParameter, - parameter => parameter ? parameter.description : '' -); - -/* - * Returns a Redux selector function which returns the label to be used for the secondary y axis - */ -export const getSecondaryYLabel= function() { - return ''; // placeholder for ticket WDFN-370 -}; - - /** * Returns a Redux selector function which returns the title to be used for the hydrograph + * @return {Function} */ export const getTitle = createSelector( getPrimaryParameter, @@ -59,7 +42,7 @@ export const getTitle = createSelector( getPrimaryMethods, (parameter, methodID, methods) => { let title = parameter ? parameter.name : ''; - if (methodID && methods.length) { + if (methodID && methods.length > 1) { const thisMethod = methods.find(method => method.methodID === methodID); if (thisMethod && thisMethod.methodDescription) { title = `${title}, ${thisMethod.methodDescription}`; @@ -72,6 +55,7 @@ export const getTitle = createSelector( /* * Returns a Redux selector function which returns the description of the hydrograph + * @return {Function} */ export const getDescription = createSelector( getPrimaryParameter, @@ -85,6 +69,10 @@ export const getDescription = createSelector( } ); +/* + * Returns a Redux selector function which returns the primary parameter's unit code. + * @return {Function} + */ export const getPrimaryParameterUnitCode = createSelector( getPrimaryParameter, parameter => parameter ? parameter.unit : null diff --git a/assets/src/scripts/monitoring-location/components/hydrograph/selectors/time-series-data.test.js b/assets/src/scripts/monitoring-location/components/hydrograph/selectors/time-series-data.test.js index 6b6425718..6a5e03a44 100644 --- a/assets/src/scripts/monitoring-location/components/hydrograph/selectors/time-series-data.test.js +++ b/assets/src/scripts/monitoring-location/components/hydrograph/selectors/time-series-data.test.js @@ -1,311 +1,133 @@ -import { - isVisible, getYLabel, getTitle, - getDescription, getTsTimeZone} from './time-series-data'; +import config from 'ui/config'; +import {isVisible, getTitle, getDescription, getPrimaryParameterUnitCode} from './time-series-data'; -const TEST_DATA = { - ianaTimeZone: 'America/Chicago', - ivTimeSeriesData: { - timeSeries: { - '00060': { - tsKey: 'current:P7D', - startTime: 1520351100000, - endTime: 1520948700000, - variable: '45807197', - method: 69929, - points: [{ - value: 10, - qualifiers: ['P'], - approved: false, - estimated: false - }, { - value: null, - qualifiers: ['P', 'ICE'], - approved: false, - estimated: false - }, { - value: null, - qualifiers: ['P', 'FLD'], - approved: false, - estimated: false - }] - }, - '00010': { - tsKey: 'compare:P7D', - startTime: 1520351100000, - endTime: 1520948700000, - variable: '45807196', - method: 69931, - points: [{ - value: 1, - qualifiers: ['P'], - approved: false, - estimated: false - }, { - value: 2, - qualifiers: ['P'], - approved: false, - estimated: false - }, { - value: 3, - qualifiers: ['P'], - approved: false, - estimated: false - }] - }, - '00010:2': { - tsKey: 'current:P7D', - startTime: 1520351100000, - endTime: 1520948700000, - variable: '45807196', - method: 69930, - points: [{ - value: 1, - qualifiers: ['P'], - approved: false, - estimated: false - }, { - value: 2, - qualifiers: ['P'], - approved: false, - estimated: false - }, { - value: 3, - qualifiers: ['P'], - approved: false, - estimated: false - }] - }, - '00011': { - tsKey: 'compare:P7D', - startTime: 1520351100000, - endTime: 1520948700000, - variable: '45807195', - method: 69929, - points: [{ - value: 68, - qualifiers: ['P'], - approved: false, - estimated: false - }, { - value: 70, - qualifiers: ['P'], - approved: false, - estimated: false - }, { - value: 77, - qualifiers: ['P'], - approved: false, - estimated: false - }] - }, - '00060:P30D': { - tsKey: 'current:P30D:00060', - startTime: 1520351100000, - endTime: 1520948700000, - variable: '45807197', - method: 69929, - points: [{ - value: 10, - qualifiers: ['P'], - approved: false, - estimated: false - }, { - value: null, - qualifiers: ['P', 'ICE'], - approved: false, - estimated: false - }, { - value: null, - qualifiers: ['P', 'FLD'], - approved: false, - estimated: false - }] - } - }, - timeSeriesCollections: { - 'coll1': { - variable: 45807197, - timeSeries: ['00060'] - } - }, - requests: { - 'current:P7D': { - timeSeriesCollections: ['coll1'] - }, - 'current:P30D:00060': {} - }, - variables: { - '45807197': { - variableCode: { - value: '00060' - }, - variableName: 'Streamflow', - variableDescription: 'Discharge, cubic feet per second', - oid: '45807197' - }, - '45807196': { - variableCode: { - value: '00010' - }, - variableName: 'Temperature, water, degrees Celsius', - variableDescription: 'Water Temperature in Celsius', - oid: '45807196' - }, - '45807195': { - variableCode: { - value: '00011' - }, - variableName: 'Temperature, water, degrees Fahrenheit', - variableDescription: 'Water Temperature in Fahrenheit', - oid: '45807195' - }, - '55807196' : { - variableCode: { - value: '11111' - }, - variableName: 'My variable', - variableDescription: 'My awesome variable', - oid: '55807196' - } + +const TEST_STATE = { + hydrographData: { + currentTimeRange: { + start: 1613569918466, + end: 1614174718466 }, - methods: { - 69329: { - methodDescription: '', - methodID: 69928 + primaryIVData: { + parameter: { + parameterCode:'00030', + name: 'Dissolved oxygen, water, unfiltered, mg/L', + description: 'Dissolved oxygen, water, unfiltered, milligrams per liter', + unit: 'mg/l' }, - 69330: { - methodDescription: '4.1 ft from riverbed (middle)', - methodID: 69930 - }, - 69331: { - methodDescription: '1.0 ft from riverbed (bottom)', - methodID: 69931 - } - }, - queryInfo: { - 'current:P7D': { - notes: { - requestDT: 1483994767572, - 'filter:timeRange': { - mode: 'PERIOD', - periodDays: 7, - modifiedSince: null + values: { + '69937': { + points: [], + method: { + methodDescription: 'From multiparameter sonde, [Discontinued]', + methodID: '69937' } - } - }, - 'current:P30D:00060': { - notes: { - requestDT: 1483994767572, - 'filter:timeRange': { - mode: 'RANGE', - interval: { - start: 1483941600000, - end: 1486533600000 - }, - modifiedSince: null + }, + '252055': { + points: [], + method: { + methodDescription: 'From multiparameter sonde', + methodID: '252055' } } } } }, - ivTimeSeriesState: { - currentIVVariableID: '45807197', - currentIVDateRange: 'P7D' + hydrographState: { + showCompareIVData: false, + showMedianData: false, + selectedIVMethodID: '252055' } }; describe('monitoring-location/components/hydrograph/time-series module', () => { - + config.locationTimeZone = 'America/Chicago'; describe('isVisible', () => { it('Returns whether the time series is visible', () => { - const store = { - ivTimeSeriesState: { - showIVTimeSeries: { - 'current': true, - 'compare': false, - 'median': true - } + expect(isVisible('primary')(TEST_STATE)).toBe(true); + expect(isVisible('compare')(TEST_STATE)).toBe(false); + expect(isVisible('compare')({ + ...TEST_STATE, + hydrographState: { + ...TEST_STATE.hydrographState, + showCompareIVData: true } - }; - - expect(isVisible('current')(store)).toBe(true); - expect(isVisible('compare')(store)).toBe(false); - expect(isVisible('median')(store)).toBe(true); - }); - }); - - describe('yLabelSelector', () => { - it('Returns string to be used for labeling the y axis', () => { - expect(getYLabel(TEST_DATA)).toBe('Discharge, cubic feet per second'); - }); - - it('Returns empty string if no variable selected', () => { - expect(getYLabel({ - ...TEST_DATA, - ivTimeSeriesState: { - ...TEST_DATA.ivTimeSeriesState, - currentIVVariableID: null + })).toBe(true); + expect(isVisible('median')(TEST_STATE)).toBe(false); + expect(isVisible('median')({ + ...TEST_STATE, + hydrographState: { + ...TEST_STATE.hydrographState, + showMedianData: true } - })).toBe(''); + })).toBe(true); }); }); describe('getTitle', () => { - it('Returns the string to used for graph title', () => { - expect(getTitle(TEST_DATA)).toBe('Streamflow'); - }); - it('Returns the title string with the method description appended', () => { + it('Returns an empty if no IV data exists', () => { expect(getTitle({ - ...TEST_DATA, - ivTimeSeriesState: { - ...TEST_DATA.ivTimeSeriesState, - currentIVMethodID: 69330 - } - })).toBe('Streamflow' + ', ' + '4.1 ft from riverbed (middle)'); + hydrographData: {}, + hydrographState: {} + })).toBe(''); }); - it('Returns empty string if no variable selected', () => { + + it('Returns the parameter name if only one method is defined', () => { expect(getTitle({ - ...TEST_DATA, - ivTimeSeriesState: { - ...TEST_DATA.ivTimeSeriesState, - currentIVVariableID: null + ...TEST_STATE, + hydrographData: { + ...TEST_STATE.hydrographData, + primaryIVData: { + ...TEST_STATE.hydrographData.primaryIVData, + values: { + '252055': { + ...TEST_STATE.hydrographData.primaryIVData.values['252055'] + } + } + } } - })).toBe(''); + })).toBe('Dissolved oxygen, water, unfiltered, mg/L'); + }); + + it('Returns the parameter name with the method description if more than one method is defined', () => { + expect(getTitle(TEST_STATE)).toBe('Dissolved oxygen, water, unfiltered, mg/L, From multiparameter sonde'); }); }); describe('getDescription', () => { - it('Returns a description with the date for the current times series', () => { - const result = getDescription(TEST_DATA); - - expect(result).toContain('Discharge, cubic feet per second'); - expect(result).toContain('1/2/2017'); - expect(result).toContain('1/9/2017'); + it('Returns an empty string if no IV data', () => { + expect(getDescription({ + hydrographData: {} + })).toBe(''); }); - }); - describe('getTsTimeZone', () => { + it('Returns the description of the primary parameter as well as the current time range', () => { + expect(getDescription(TEST_STATE)).toBe( + 'Dissolved oxygen, water, unfiltered, milligrams per liter from 2/17/2021 7:51:58 AM -0600 to 2/24/2021 7:51:58 AM -0600'); + }); - it('Returns local if series is empty', () => { - const result = getTsTimeZone({ - series: {} - }); - expect(result).toEqual('local'); + it('Returns the description without time if current time range is missing', () => { + expect(getDescription({ + ...TEST_STATE, + hydrographData: { + primaryIVData: { + ...TEST_STATE.hydrographData.primaryIVData + } + } + })).toBe('Dissolved oxygen, water, unfiltered, milligrams per liter'); }); + }); - it('Returns local if timezone is null', () => { - const result = getTsTimeZone({ - ianaTimeZone: null - }); - expect(result).toEqual('local'); + describe('getPrimaryParameterUnitCode', () => { + it('Return null if no primary IV data', () => { + expect(getPrimaryParameterUnitCode({ + hydrographData: {} + })).toBeNull(); }); - it('Returns the IANA timezone NWIS and IANA agree', () => { - const result = getTsTimeZone({ - ianaTimeZone: 'America/New_York' - }); - expect(result).toEqual('America/New_York'); + it('Return unit code if primary IV data', () => { + expect(getPrimaryParameterUnitCode(TEST_STATE)).toBe('mg/l'); }); }); }); -- GitLab