Created
February 26, 2024 17:21
-
-
Save MatthewCallis/433f3a0a2262af2d8617c2cc34763330 to your computer and use it in GitHub Desktop.
React Code Snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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