I’d like to check if the login endpoint occurs after the submit button is clicked, but I don’t know how I can “lockdown” the login function
test('Submitting the form with wrong username', async () => {
const { result } = renderHook(() => authApiSlice.endpoints.login.useMutation(), { wrapper: AllTheProviders })
const { getByTestId, findByRole, findByText } = render(
<Form />
)
await act(() => {
fireEvent.input(getByTestId('usernameField'), {
target: {
value: 'example'
}
})
fireEvent.input(getByTestId('passwordField'), {
target: {
value: '1212'
}
})
fireEvent.submit(getByTestId('submitBtn'))
})
expect(await findByText(/username or password is incorrect/i)).toBeInTheDocument()
expect(getByTestId('usernameField')).toHaveValue('')
expect(getByTestId('passwordField')).toHaveValue('')
})
import React, { ReactElement } from 'react'
import { render, RenderOptions } from '@testing-library/react'
import { Provider } from 'react-redux'
import { ToastContainer } from 'react-toastify'
import { CssBaseline, ThemeProvider } from '@mui/material'
import { store } from '../store/store'
import { theme } from '../theme/theme'
import { BrowserRouter } from 'react-router-dom'
export const AllTheProviders = ({ children }: { children: React.ReactNode }) => {
return (
<Provider store={store}>
<ThemeProvider theme={theme}>
<BrowserRouter>
<CssBaseline />
{children}
</BrowserRouter>
</ThemeProvider>
<ToastContainer />
</Provider>
)
}
const customRender = (
ui: ReactElement,
options?: Omit<RenderOptions, 'wrapper'>,
) => render(ui, { wrapper: AllTheProviders, ...options })
export * from '@testing-library/react'
export { customRender as render }
import { IChangeTaskReq, ICreateTaskReq, ITask, IToken, IUser, IUserReq } from "../../types/types";
import { authApi } from "./auth-api";
export const authApiSlice = authApi.injectEndpoints({
endpoints: builder => ({
login: builder.mutation<IToken, IUserReq>({
query: (body) => ({
url: `auth/login`,
method: 'POST',
body
}),
invalidatesTags: ['Token', 'Tasks']
}),
})
})
export const { useLoginMutation } = authApiSlice
I want to check my login endpoint will be it called or not but i do not know how to mock my RTKQ endpoint. IDK i need test it here or not, i would be glad of help