Skip to content

Commit

Permalink
fix(observation-unit): empty case
Browse files Browse the repository at this point in the history
 - Had delay trigger of 5sec on the form.
 - filter only init trigger (oldValue === newValue)
 - Add tests
  • Loading branch information
xtiannyeto committed Sep 2, 2024
1 parent 1b9d8db commit 4e0323f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as usePatchModule from '@features/pam/mission/hooks/use-patch-mission-env'
import { vi } from 'vitest'
import { fireEvent, render, screen, waitFor } from '../../../../../test-utils.tsx'
import MissionObservationByUnit from './mission-observations-unit.tsx'
import * as usePatchModule from '@features/pam/mission/hooks/use-patch-mission-env'

const patchMock = vi.fn()

Expand Down Expand Up @@ -32,12 +32,33 @@ describe('MissionObservation', () => {
})
})

it('should not call update observation under 4', () => {
it('should trigger 5 secondes after typing', () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
const observationsByUnit = 'my observations!!!!!'
const wrapper = render(<MissionObservationByUnit missionId={1} observationsByUnit={'My beautiful observation'} />)
const element = wrapper.getByTestId('mission-general-observation')
fireEvent.change(element, {
target: { value: 'my' }
target: { value: observationsByUnit }
})
expect(patchMock).not.toHaveBeenCalled()
vi.advanceTimersByTime(5000)
expect(patchMock).toHaveBeenCalled()
expect(patchMock).toHaveBeenCalledWith({
variables: { mission: expect.objectContaining({ observationsByUnit }) }
})
})

it('should call update observations event empty', async () => {
vi.useFakeTimers({ shouldAdvanceTime: true })
const wrapper = render(<MissionObservationByUnit missionId={1} observationsByUnit={'My beautiful observation'} />)
const element = wrapper.getByTestId('mission-general-observation')
fireEvent.change(element, {
target: { value: '' }
})
vi.advanceTimersByTime(5000)
expect(patchMock).toHaveBeenCalled()
expect(patchMock).toHaveBeenCalledWith({
variables: { mission: expect.objectContaining({ observationsByUnit: '' }) }
})
})
})
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { FormikEffect, FormikTextarea } from '@mtes-mct/monitor-ui'
import { Formik } from 'formik'
import React, { useEffect, useState } from 'react'
import React, { useEffect, useRef, useState } from 'react'
import usePatchMissionEnv from '../../hooks/use-patch-mission-env.tsx'

const DEBOUNCE_TIME_TRIGGER = 5000

type ObservationsByUnit = {
observations?: string
}
Expand All @@ -14,20 +16,25 @@ interface MissionObservationsByUnitProps {

const MissionObservationsUnit: React.FC<MissionObservationsByUnitProps> = ({ missionId, observationsByUnit }) => {
const [patchMissionObservation] = usePatchMissionEnv()
const timerRef = useRef<ReturnType<typeof setTimeout>>()
const [initValue, setInitValue] = useState<ObservationsByUnit>()

useEffect(() => {
setInitValue({ observations: observationsByUnit })
}, [observationsByUnit])

const handleSubmit = async ({ observations }: ObservationsByUnit) => {
if (!observations || observations.length < 4 || observations === observationsByUnit) return
const handleSubmit = async ({ observations }: ObservationsByUnit): Promise<void> => {
clearTimeout(timerRef.current)
timerRef.current = setTimeout(() => patchObservationUnit(observations), DEBOUNCE_TIME_TRIGGER)
}

const patchObservationUnit = async (observations?: string) => {
if (observations === observationsByUnit) return
await patchMissionObservation({
variables: {
mission: {
missionId,
observationsByUnit: observations
observationsByUnit: observations || ''
}
}
})
Expand Down

0 comments on commit 4e0323f

Please sign in to comment.