Update test case, fix comments

This commit is contained in:
2023-12-03 22:22:36 +07:00
parent d53b869d65
commit 5d5eb1adaf
135 changed files with 8702 additions and 1491 deletions
@@ -0,0 +1,93 @@
import { act, renderHook } from '@testing-library/react';
import toast from 'react-hot-toast';
import {
QueryClient,
QueryClientProvider,
useQueryClient,
} from '@tanstack/react-query';
import { ReactNode } from 'react';
// Types
import { IRoom } from '@type/room';
// Constants
import { DELETE_SUCCESS } from '@constant/messages';
// Services
import { setIsDeleteRoom } from '@service/roomServices';
// Hooks
import { useSetIsDeleteRoom } from '../useSetIsDeleteRoom';
// Mock the necessary dependencies
jest.mock('@tanstack/react-query', () => ({
...jest.requireActual('@tanstack/react-query'),
useQueryClient: jest.fn(),
}));
jest.mock('@service/roomServices', () => ({
setIsDeleteRoom: jest.fn(),
}));
jest.mock('react-hot-toast');
const tempRoom: IRoom = {
id: 1,
name: 'Room 1',
price: 250,
status: true,
isDelete: false,
};
describe('useUpdateRoom', () => {
const queryClient = new QueryClient();
const env = ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
);
test('Should update room successful', async () => {
(toast.success as jest.Mock).mockImplementation(() => {});
const invalidateQueriesMock = jest.fn();
(useQueryClient as jest.Mock).mockReturnValueOnce({
invalidateQueries: invalidateQueriesMock,
});
(setIsDeleteRoom as jest.Mock).mockResolvedValue({
data: tempRoom,
});
const { result } = renderHook(() => useSetIsDeleteRoom(), {
wrapper: env,
});
// Call function
await act(async () => {
result.current.setIsDeleteRoom(1);
});
expect(toast.success).toHaveBeenCalledWith(DELETE_SUCCESS);
expect(invalidateQueriesMock).toHaveBeenCalledWith({
queryKey: ['rooms'],
});
});
test('should handle createRoom mutation error', async () => {
const errorMessage = 'Error';
(toast.error as jest.Mock).mockImplementation(() => {});
const invalidateQueriesMock = jest.fn();
(useQueryClient as jest.Mock).mockReturnValueOnce({
invalidateQueries: invalidateQueriesMock,
});
(setIsDeleteRoom as jest.Mock).mockRejectedValueOnce(new Error(errorMessage));
const { result } = renderHook(() => useSetIsDeleteRoom(), {
wrapper: env,
});
// Call function
await act(async () => {
result.current.setIsDeleteRoom(1);
});
expect(toast.error).toHaveBeenCalledWith(errorMessage);
});
});