'Test if axios is called after a button click
I have a page component which contains a button. Clicking that button triggers the API POST request. I want to be able to test if the axios call was actually triggered. My below code is not working. Any advice would be appreciated :)
it('Check if the api call was made', async () => {
const spy = jest.spyOn(axios, 'request')
const { getByRole, getByText, getByTestId } = render(
<MemoryRouter>
<ThemeProvider theme={theme}>
<ConfirmEmailPage /> // Component with the button
</ThemeProvider>
</MemoryRouter>
)
const button = getByText("Didn't receive reset link? Resend it") // Button is detected succesfully and gets logged to the console
userEvent.click(button)
expect(spy).toHaveBeenCalled() // FAIL
})
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
