From 400adaa9a845d79436a018665ea05415a7f35546 Mon Sep 17 00:00:00 2001 From: Brett Edwards Date: Mon, 6 Nov 2023 15:55:32 -0800 Subject: [PATCH] adds row filtering tests --- .../components/ForecastSummaryDataGrid.tsx | 34 +++++++----- .../forecastSummaryDataGrid.test.tsx | 53 +++++++++++++++++++ 2 files changed, 75 insertions(+), 12 deletions(-) create mode 100644 web/src/features/moreCast2/components/forecastSummaryDataGrid.test.tsx diff --git a/web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx b/web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx index ff8b73840..72804fa52 100644 --- a/web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx +++ b/web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx @@ -37,6 +37,25 @@ interface ForecastSummaryDataGridProps { handleClose: () => void } +export const filterRowsForSimulationFromEdited = ( + editedRow: MoreCast2Row, + allRows: MoreCast2Row[] +): MoreCast2Row[] | undefined => { + if (validForecastPredicate(editedRow)) { + const validRowsForStation = allRows.filter( + row => row.stationCode === editedRow.stationCode && validActualOrForecastPredicate(row) + ) + + const yesterday = editedRow.forDate.minus({ days: 1 }) + const yesterdayRow = validRowsForStation.find(row => row.forDate.toISODate() === yesterday.toISODate()) + + if (yesterdayRow) { + const rowsForSimulation = validRowsForStation.filter(row => row.forDate >= yesterday) + return rowsForSimulation + } else return undefined + } +} + const ForecastSummaryDataGrid = ({ loading, rows, @@ -50,18 +69,9 @@ const ForecastSummaryDataGrid = ({ const processRowUpdate = async (editedRow: MoreCast2Row) => { dispatch(storeUserEditedRows([editedRow])) - if (validForecastPredicate(editedRow)) { - const validRowsForStation = rows.filter( - row => row.stationCode === editedRow.stationCode && validActualOrForecastPredicate(row) - ) - - const yesterday = editedRow.forDate.minus({ days: 1 }) - const yesterdayRow = validRowsForStation.find(row => row.forDate.toISODate() === yesterday.toISODate()) - - if (yesterdayRow) { - const rowsForSimulation = validRowsForStation.filter(row => row.forDate >= yesterday) - dispatch(getSimulatedIndices(rowsForSimulation)) - } + const rowsForSimulation = filterRowsForSimulationFromEdited(editedRow, rows) + if (rowsForSimulation) { + dispatch(getSimulatedIndices(rowsForSimulation)) } return editedRow diff --git a/web/src/features/moreCast2/components/forecastSummaryDataGrid.test.tsx b/web/src/features/moreCast2/components/forecastSummaryDataGrid.test.tsx new file mode 100644 index 000000000..30942be93 --- /dev/null +++ b/web/src/features/moreCast2/components/forecastSummaryDataGrid.test.tsx @@ -0,0 +1,53 @@ +import { filterRowsForSimulationFromEdited } from 'features/moreCast2/components/ForecastSummaryDataGrid' +import { DateTime } from 'luxon' +import { createEmptyMoreCast2Row } from 'features/moreCast2/slices/dataSlice' +import { MoreCast2Row } from 'features/moreCast2/interfaces' + +const TEST_DATE = DateTime.fromISO('2023-02-16T20:00:00+00:00') + +const buildValidForecastRow = (stationCode: number, forDate: DateTime): MoreCast2Row => { + const forecastRow = createEmptyMoreCast2Row('id', stationCode, 'stationName', forDate, 1, 2) + forecastRow.precipForecast = { choice: 'FORECAST', value: 2 } + forecastRow.tempForecast = { choice: 'FORECAST', value: 2 } + forecastRow.rhForecast = { choice: 'FORECAST', value: 2 } + forecastRow.windSpeedForecast = { choice: 'FORECAST', value: 2 } + + return forecastRow +} + +const buildValidActualRow = (stationCode: number, forDate: DateTime): MoreCast2Row => { + const actualRow = createEmptyMoreCast2Row('id', stationCode, 'stationName', forDate, 1, 2) + actualRow.precipActual = 1 + actualRow.tempActual = 1 + actualRow.rhActual = 1 + actualRow.windSpeedActual = 1 + + return actualRow +} + +const buildInvalidForecastRow = (stationCode: number, forDate: DateTime): MoreCast2Row => { + const forecastRow = createEmptyMoreCast2Row('id', stationCode, 'stationName', forDate, 1, 2) + + return forecastRow +} + +// rows to filter in +const actual1A = buildValidActualRow(1, TEST_DATE) +const forecast1A = buildValidForecastRow(1, TEST_DATE.plus({ days: 1 })) // edited row +const forecast1B = buildValidForecastRow(1, TEST_DATE.plus({ days: 2 })) + +// rows to filter out +const actual1B = buildValidActualRow(1, TEST_DATE.minus({ days: 1 })) +const forecast1C = buildInvalidForecastRow(1, TEST_DATE.plus({ days: 3 })) +const actual2A = buildValidActualRow(2, TEST_DATE.minus({ days: 1 })) +const forecast2A = buildValidForecastRow(2, TEST_DATE.plus({ days: 1 })) + +const rows = [actual1A, actual1B, forecast1A, forecast1B, forecast1C, actual2A, forecast2A] + +describe('filterRowsForSimulationFromEdited', () => { + it('should filter for valid rows before and after the edited row', () => { + const filteredRows = filterRowsForSimulationFromEdited(forecast1A, rows) + expect(filteredRows).toEqual(expect.arrayContaining([actual1A, forecast1A, forecast1B])) + expect(filteredRows).not.toEqual(expect.arrayContaining([actual1B, forecast1C, actual2A, forecast2A])) + }) +})