Skip to content

Commit

Permalink
adds row filtering tests
Browse files Browse the repository at this point in the history
  • Loading branch information
brettedw committed Nov 6, 2023
1 parent 01dc387 commit 400adaa
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
34 changes: 22 additions & 12 deletions web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Check warning on line 55 in web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx#L55

Added line #L55 was not covered by tests
}
}

const ForecastSummaryDataGrid = ({
loading,
rows,
Expand All @@ -50,18 +69,9 @@ const ForecastSummaryDataGrid = ({
const processRowUpdate = async (editedRow: MoreCast2Row) => {
dispatch(storeUserEditedRows([editedRow]))

Check warning on line 70 in web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx#L68-L70

Added lines #L68 - L70 were not covered by tests

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)

Check warning on line 72 in web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx#L72

Added line #L72 was not covered by tests
if (rowsForSimulation) {
dispatch(getSimulatedIndices(rowsForSimulation))

Check warning on line 74 in web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx#L74

Added line #L74 was not covered by tests
}

return editedRow

Check warning on line 77 in web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx

View check run for this annotation

Codecov / codecov/patch

web/src/features/moreCast2/components/ForecastSummaryDataGrid.tsx#L77

Added line #L77 was not covered by tests
Expand Down
Original file line number Diff line number Diff line change
@@ -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]))
})
})

0 comments on commit 400adaa

Please sign in to comment.