Skip to content

Instantly share code, notes, and snippets.

@MatthewCallis
Created February 26, 2024 17:21
Show Gist options
  • Save MatthewCallis/433f3a0a2262af2d8617c2cc34763330 to your computer and use it in GitHub Desktop.
Save MatthewCallis/433f3a0a2262af2d8617c2cc34763330 to your computer and use it in GitHub Desktop.
React Code Snippets
// Testing State Setters
// const [payload, setPayload] = useState({})
it('can update the number of times to loop', () => {
const mockSetPayload = vi.fn()
const mockPayload = {
timeOfLoop: 0,
amount: 0,
units: 'hours',
exitCondition: {
groups: [],
},
}
render(<LoopPanel setPayload={mockSetPayload} />)
fireEvent.change(screen.getByTestId('timeOfLoop'), { target: { value: 10 } })
expect(mockSetPayload).toBeCalledWith(expect.any(Function))
// setPayload is called 2 times before we interact with it, so pull the 3rd iteration
const updateFunction = mockSetPayload.mock.calls[2][0]
// We need to simulate the previous state of the payload
const updated = updateFunction(mockPayload)
expect(updated).toEqual({
...mockPayload,
timeOfLoop: 10,
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment