Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rezafikkri/299d489d00b084df08d47e5978aab041 to your computer and use it in GitHub Desktop.
Save rezafikkri/299d489d00b084df08d47e5978aab041 to your computer and use it in GitHub Desktop.
test('Should call asyncCreateThread function when save button clicked', async () => {
// Arrange
// create mock for asyncCreateThread module
vi.mock('@/lib/threads/action', async (importOriginal) => {
const mod = await importOriginal();
return {
...mod,
asyncCreateThread: vi.fn(),
};
});
vi.mock('@/hooks/redux-hooks', () => ({
useAppDispatch: () => async () => {},
}));
const { asyncCreateThread } = await import('@/lib/threads/action');
render(<ThreadInput />);
const titleInput = screen.getByPlaceholderText(/title/i);
await userEvent.type(titleInput, 'This is title');
const categoryInput = screen.getByPlaceholderText(/category/i);
await userEvent.type(categoryInput, 'This is category');
const bodyInput = screen.getByPlaceholderText(/body/i);
await userEvent.type(bodyInput, 'This is body');
const saveButton = screen.getByText('Save');
// Action
await userEvent.click(saveButton);
// Assert
expect(asyncCreateThread).toHaveBeenCalled();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment