Skip to content

Commit

Permalink
fix infinite API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
lincent committed Aug 21, 2024
1 parent 6116120 commit 3537973
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 166 deletions.
43 changes: 29 additions & 14 deletions air-quality-ui/src/components/header/Toolbar.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ import {
ForecastWindowSelectorProps,
} from './ForecastWindowSelector'
import { Toolbar } from './Toolbar'
import { SetForcastDetailsType } from '../../context'

const mockSetForecastBaseDate: (val: DateTime) => void = jest.fn()
const mockSetMaxForecastDate: (val: number) => void = jest.fn()
const mockSetMaxInSituDate: (val: number) => void = jest.fn()
const mockSetDetails: (val: SetForcastDetailsType) => void = jest.fn()

const dateNow = DateTime.fromISO('2024-06-01T12:00:00', { zone: 'UTC' })
Settings.now = () => dateNow.toMillis()

jest.mock('../../context', () => ({
useForecastContext: jest.fn().mockReturnValue({
forecastBaseDate: DateTime.fromISO('2024-06-01T12:00:00', { zone: 'UTC' }),
maxInSituDate: DateTime.fromISO('2024-06-10T09:00:00', { zone: 'utc' }),
setForecastBaseDate: (x: DateTime) => mockSetForecastBaseDate(x),
setMaxForecastDate: (x: number) => mockSetMaxForecastDate(x),
setMaxInSituDate: (x: number) => mockSetMaxInSituDate(x),
forecastDetails: {
forecastBaseDate: DateTime.fromISO('2024-06-01T12:00:00', {
zone: 'UTC',
}),
maxMeasurementDate: DateTime.fromISO('2024-06-10T09:00:00', {
zone: 'utc',
}),
},
setDetails: (x: SetForcastDetailsType) => mockSetDetails(x),
}),
}))

Expand Down Expand Up @@ -58,6 +61,9 @@ jest.mock('./ForecastWindowSelector', () => ({
}))

describe('Toolbar component', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('renders toolbar', () => {
render(<Toolbar />)
expect(screen.getByRole('toolbar')).toBeInTheDocument()
Expand Down Expand Up @@ -85,9 +91,12 @@ describe('Toolbar component', () => {
})
fireEvent.click(screen.getByText('Ok'))
await waitFor(() => {
expect(mockSetForecastBaseDate).toHaveBeenCalledWith(
DateTime.fromISO('2024-06-10T09:00:00', { zone: 'utc' }),
)
expect(mockSetDetails).toHaveBeenCalledWith({
forecastBaseDate: DateTime.fromISO('2024-06-10T09:00:00', {
zone: 'utc',
}),
forecastWindow: 1,
})
})
})
it('Setting invalid date and clicking button does not update context', async () => {
Expand All @@ -102,7 +111,7 @@ describe('Toolbar component', () => {
})
fireEvent.click(screen.getByText('Ok'))
await waitFor(() => {
expect(mockSetForecastBaseDate).not.toHaveBeenCalledWith(
expect(mockSetDetails).not.toHaveBeenCalledWith(
DateTime.fromISO('2024-08-10T09:00:00', { zone: 'utc' }),
)
})
Expand Down Expand Up @@ -130,7 +139,7 @@ describe('Toolbar component', () => {
expected: 5,
},
].forEach(({ testData, expected }) => {
it(`When forecast window selector is changed to ${testData}, when the ok button is clicked, selectedForecastWindow is ${expected}`, async () => {
it(`When forecast window selector is changed to ${testData.label}, when the ok button is clicked, selectedForecastWindow is ${expected}`, async () => {
render(<Toolbar />)
const beforeSetting = selectedForecastWindow
await waitFor(() => {
Expand All @@ -140,8 +149,14 @@ describe('Toolbar component', () => {
await waitFor(() => {
expect(beforeSetting).toStrictEqual({ value: 1, label: '1' })
})
const expects = {
forecastBaseDate: DateTime.fromISO('2024-06-01T12:00:00', {
zone: 'utc',
}),
forecastWindow: expected,
}
await waitFor(() => {
expect(mockSetMaxForecastDate).toHaveBeenCalledWith(expected)
expect(mockSetDetails).toHaveBeenCalledWith(expects)
})
})
})
Expand Down
22 changes: 10 additions & 12 deletions air-quality-ui/src/components/header/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import { useForecastContext } from '../../context'
import { VAirifyButton } from '../common/button/VAirifyButton'

export const Toolbar = () => {
const {
forecastBaseDate,
setForecastBaseDate,
setMaxForecastDate,
setMaxInSituDate,
} = useForecastContext()
const [selectedForecastBaseDate, setSelectedForecastBaseDate] =
useState<DateTime<boolean>>(forecastBaseDate)
const { forecastDetails, setDetails } = useForecastContext()

const [selectedForecastBaseDate, setSelectedForecastBaseDate] = useState<
DateTime<boolean>
>(forecastDetails.forecastBaseDate)
const [selectedForecastWindow, setSelectedForecastWindow] =
useState<ForecastWindowOption>({ value: 1, label: '1' })
const [isInvalidDateTime, setIsInvalidDateTime] = useState<boolean>(false)
Expand All @@ -37,7 +34,7 @@ export const Toolbar = () => {
<ForecastBaseDatePicker
setSelectedForecastBaseDate={setSelectedForecastBaseDate}
setIsInvalidDateTime={setIsInvalidDateTime}
forecastBaseDate={forecastBaseDate}
forecastBaseDate={forecastDetails.forecastBaseDate}
/>
</div>
<div className={classes['forecast-window-main-div']}>
Expand All @@ -50,10 +47,11 @@ export const Toolbar = () => {
<VAirifyButton
onClick={() => {
if (selectedForecastBaseDate) {
setForecastBaseDate(selectedForecastBaseDate)
setDetails({
forecastBaseDate: selectedForecastBaseDate,
forecastWindow: selectedForecastWindow.value,
})
}
setMaxForecastDate(selectedForecastWindow.value)
setMaxInSituDate(selectedForecastWindow.value)
}}
text={'Ok'}
isButtonDisabled={isInvalidDateTime}
Expand Down
8 changes: 5 additions & 3 deletions air-quality-ui/src/components/single-city/SingleCity.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ jest.mock('@tanstack/react-query', () => ({

jest.mock('../../context', () => ({
useForecastContext: jest.fn().mockReturnValue({
forecastBaseDate: DateTime.now(),
maxInSituDate: DateTime.now(),
maxForecastDate: DateTime.now(),
forecastDetails: {
forecastBaseDate: DateTime.now(),
maxMeasurementDate: DateTime.now(),
maxForecastDate: DateTime.now(),
},
}),
}))

Expand Down
33 changes: 18 additions & 15 deletions air-quality-ui/src/components/single-city/SingleCity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const getSiteName = (measurement: MeasurementsResponseDto): string => {
}

export const SingleCity = () => {
const { forecastBaseDate, maxInSituDate, maxForecastDate } =
useForecastContext()
const { forecastDetails } = useForecastContext()

const { name: locationName = '' } = useParams()
const [
{
Expand All @@ -43,31 +43,34 @@ export const SingleCity = () => {
queries: [
{
queryKey: [
forecastBaseDate,
forecastDetails.forecastBaseDate,
locationName,
maxInSituDate,
maxForecastDate,
forecastDetails.maxMeasurementDate,
forecastDetails.maxForecastDate,
],
queryFn: () =>

Check warning on line 51 in air-quality-ui/src/components/single-city/SingleCity.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
getForecastData(
forecastBaseDate,
maxForecastDate,
forecastBaseDate,
forecastDetails.forecastBaseDate,
forecastDetails.maxForecastDate,
forecastDetails.forecastBaseDate,
locationName,

Check warning on line 56 in air-quality-ui/src/components/single-city/SingleCity.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
),
},
{
queryKey: [
'measurements',
locationName,
forecastBaseDate,
maxInSituDate,
maxForecastDate,
forecastDetails.forecastBaseDate,
forecastDetails.maxMeasurementDate,
forecastDetails.maxForecastDate,
],
queryFn: () =>

Check warning on line 67 in air-quality-ui/src/components/single-city/SingleCity.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
getMeasurements(forecastBaseDate, maxInSituDate, 'city', [
locationName,
]),
getMeasurements(
forecastDetails.forecastBaseDate,
forecastDetails.maxMeasurementDate,
'city',
[locationName],

Check warning on line 72 in air-quality-ui/src/components/single-city/SingleCity.tsx

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
),
},
],
})
Expand Down Expand Up @@ -201,7 +204,7 @@ export const SingleCity = () => {
cityName={locationName}
forecastData={forecastData}
measurementsData={measurements}
forecastBaseTime={forecastBaseDate}
forecastBaseTime={forecastDetails.forecastBaseDate}
/>
</div>
{measurementsByPollutantBySite &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ jest.mock('echarts-for-react', () => () => <div>Mock Chart</div>)

jest.mock('../../../context', () => ({
useForecastContext: jest.fn().mockReturnValue({
forecastBaseDate: DateTime.now(),
maxInSituDate: DateTime.now(),
maxForecastDate: DateTime.now(),
forecastDetails: {
forecastBaseDate: DateTime.now(),
maxMeasurementDate: DateTime.now(),
maxForecastDate: DateTime.now(),
},
}),
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ export const AverageComparisonChart = (
props: AverageComparisonChartProps,
): JSX.Element => {
const chartRef = useRef<ReactECharts>(null)
const { forecastBaseDate, maxForecastDate, maxInSituDate } =
useForecastContext()

const { forecastDetails } = useForecastContext()

const measurementsAveragedData = useMemo(() => {
if (props.measurementsData) {
Expand All @@ -44,15 +44,15 @@ export const AverageComparisonChart = (
}, [props.measurementsData, props.forecastBaseTime])

const zoomPercent = getInSituPercentage(
forecastBaseDate,
maxForecastDate,
maxInSituDate,
forecastDetails.forecastBaseDate,
forecastDetails.maxForecastDate,
forecastDetails.maxMeasurementDate,
)

const zoomEventHandler = useCallback(() => {
const instance = chartRef.current?.getEchartsInstance()
updateChartSubtext(instance!, forecastBaseDate)
}, [forecastBaseDate])
updateChartSubtext(instance!, forecastDetails.forecastBaseDate)
}, [forecastDetails])

return (
<ReactECharts
Expand All @@ -61,7 +61,11 @@ export const AverageComparisonChart = (
onEvents={{ dataZoom: zoomEventHandler }}
option={getForecastOptions(
props.cityName,
createSubtext(forecastBaseDate, forecastBaseDate, maxInSituDate),
createSubtext(
forecastDetails.forecastBaseDate,
forecastDetails.forecastBaseDate,
forecastDetails.maxMeasurementDate,
),
zoomPercent,
props.forecastData,
measurementsAveragedData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ jest.mock('echarts-for-react', () => () => <div>Mock Chart</div>)

jest.mock('../../../context', () => ({
useForecastContext: jest.fn().mockReturnValue({
forecastBaseDate: DateTime.now(),
maxInSituDate: DateTime.now(),
maxForecastDate: DateTime.now(),
forecastDetails: {
forecastBaseDate: DateTime.now(),
maxMeasurementDate: DateTime.now(),
maxForecastDate: DateTime.now(),
},
}),
}))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ export const SiteMeasurementsChart = ({
}: SiteMeasurementsChartProps): JSX.Element => {
const chartRef = useRef<ReactECharts>(null)

const { forecastBaseDate, maxForecastDate, maxInSituDate } =
useForecastContext()
const { forecastDetails } = useForecastContext()

const eChartEventHandler = useCallback(
({
Expand All @@ -60,15 +59,15 @@ export const SiteMeasurementsChart = ({
)

const zoomPercent = getInSituPercentage(
forecastBaseDate,
maxForecastDate,
maxInSituDate,
forecastDetails.forecastBaseDate,
forecastDetails.maxForecastDate,
forecastDetails.maxMeasurementDate,
)

const zoomEventHandler = useCallback(() => {
const instance = chartRef.current?.getEchartsInstance()
updateChartSubtext(instance!, forecastBaseDate)
}, [forecastBaseDate])
updateChartSubtext(instance!, forecastDetails.forecastBaseDate)
}, [forecastDetails])

return (
<>
Expand All @@ -85,7 +84,11 @@ export const SiteMeasurementsChart = ({
zoomPercent,
measurementsBySite,
forecastData,
createSubtext(forecastBaseDate, forecastBaseDate, maxInSituDate),
createSubtext(
forecastDetails.forecastBaseDate,
forecastDetails.forecastBaseDate,
forecastDetails.maxMeasurementDate,
),
cityName,
seriesColorsBySite,
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ jest.mock('@tanstack/react-query', () => ({

jest.mock('../../context', () => ({
useForecastContext: jest.fn().mockReturnValue({
forecastBaseDate: DateTime.now(),
maxInSituDate: DateTime.now(),
forecastDetails: {
forecastBaseDate: DateTime.now(),
maxMeasurementDate: DateTime.now(),
maxForecastDate: DateTime.now(),
},
}),
}))

Expand Down
Loading

0 comments on commit 3537973

Please sign in to comment.