mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 20:01:59 +00:00
Add room hook testing and update config test
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
const DEFAULT_SORT_BY = 'id';
|
||||
const DEFAULT_ORDER_BY = 'asc';
|
||||
const supabaseUrl = 'https://pjqujsjzzdrlrgqbxepe.supabase.co'
|
||||
const supabaseKey = import.meta.env.VITE_SUPABASE_KEY
|
||||
const supabaseUrl = 'https://pjqujsjzzdrlrgqbxepe.supabase.co';
|
||||
const supabaseKey = process.env.VITE_SUPABASE_KEY;
|
||||
|
||||
export { DEFAULT_SORT_BY, DEFAULT_ORDER_BY, supabaseKey, supabaseUrl };
|
||||
|
||||
@@ -1,11 +1,84 @@
|
||||
import { useCreateRoom } from '@hook/rooms/useCreateRoom';
|
||||
import { useDeleteRoom } from '@hook/rooms/useDeleteRoom';
|
||||
import { useRooms } from '@hook/rooms/useRooms';
|
||||
import { useUpdateRoom } from '@hook/rooms/useUpdateRoom';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { renderHook, waitFor } from '@testing-library/react';
|
||||
import { IRoom } from '@type/rooms';
|
||||
import { ReactNode } from 'react';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { act } from 'react-test-renderer';
|
||||
|
||||
interface IWrapper {
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
const sampleData: IRoom = {
|
||||
id: 999,
|
||||
name: 'Room name test',
|
||||
price: 9999,
|
||||
status: true,
|
||||
};
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
retry: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
const wrapper = ({ children }: IWrapper) => {
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<MemoryRouter>{children}</MemoryRouter>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
};
|
||||
|
||||
describe('CRUD Room testing', () => {
|
||||
test('Fetch room data', async () => {
|
||||
const { result } = renderHook(() => useRooms());
|
||||
const { rooms } = result.current;
|
||||
const { result } = renderHook(() => useRooms(), { wrapper });
|
||||
|
||||
await waitFor(() => expect(rooms?.length).toBeGreaterThan(0))
|
||||
})
|
||||
})
|
||||
await waitFor(() =>
|
||||
expect(result.current.rooms?.length).toBeGreaterThan(0)
|
||||
);
|
||||
});
|
||||
|
||||
test('Create room', async () => {
|
||||
const { result } = renderHook(() => useCreateRoom(), { wrapper });
|
||||
|
||||
act(() => {
|
||||
const testMethod = async () => {
|
||||
result.current.createRoom(sampleData);
|
||||
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
|
||||
};
|
||||
|
||||
testMethod();
|
||||
});
|
||||
});
|
||||
|
||||
test('Edit room', async () => {
|
||||
const { result } = renderHook(() => useUpdateRoom(), { wrapper });
|
||||
|
||||
act(() => {
|
||||
const testMethod = async () => {
|
||||
result.current.updateRoom(sampleData);
|
||||
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
|
||||
};
|
||||
|
||||
testMethod();
|
||||
});
|
||||
});
|
||||
|
||||
test('Delete room', async () => {
|
||||
const { result } = renderHook(() => useDeleteRoom(), { wrapper });
|
||||
|
||||
act(() => {
|
||||
const testMethod = async () => {
|
||||
result.current.deleteRoom(sampleData.id);
|
||||
await waitFor(() => expect(result.current.isSuccess).toBeTruthy());
|
||||
};
|
||||
|
||||
testMethod();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -16,7 +16,11 @@ const useCreateRoom = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { dispatch } = useUserRoomAvailable();
|
||||
|
||||
const { mutate: createRoom, isPending: isCreating } = useMutation({
|
||||
const {
|
||||
mutate: createRoom,
|
||||
isPending: isCreating,
|
||||
isSuccess,
|
||||
} = useMutation({
|
||||
mutationFn: createRoomFn,
|
||||
onSuccess: (room) => {
|
||||
toast.success(ADD_SUCCESS);
|
||||
@@ -31,7 +35,7 @@ const useCreateRoom = () => {
|
||||
onError: (err) => toast.error(err.message),
|
||||
});
|
||||
|
||||
return { isCreating, createRoom };
|
||||
return { isCreating, createRoom, isSuccess };
|
||||
};
|
||||
|
||||
export { useCreateRoom };
|
||||
|
||||
@@ -14,7 +14,11 @@ import * as messages from '@constant/messages';
|
||||
const useDeleteRoom = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
const { isPending: isDeleting, mutate: deleteRoom } = useMutation({
|
||||
const {
|
||||
isPending: isDeleting,
|
||||
mutate: deleteRoom,
|
||||
isSuccess,
|
||||
} = useMutation({
|
||||
mutationFn: deleteRoomFn,
|
||||
onSuccess: () => {
|
||||
toast.success(messages.DELETE_SUCCESS);
|
||||
@@ -25,7 +29,7 @@ const useDeleteRoom = () => {
|
||||
onError: (err) => toast.error(err.message),
|
||||
});
|
||||
|
||||
return { isDeleting, deleteRoom };
|
||||
return { isDeleting, deleteRoom, isSuccess };
|
||||
};
|
||||
|
||||
export { useDeleteRoom };
|
||||
|
||||
@@ -18,7 +18,7 @@ const useRooms = () => {
|
||||
const {
|
||||
isLoading,
|
||||
data: rooms,
|
||||
error,
|
||||
error
|
||||
} = useQuery({
|
||||
queryKey: ['rooms', sortByValue, orderByValue, phoneSearch],
|
||||
queryFn: () => getAllRooms(sortByValue, orderByValue, phoneSearch),
|
||||
|
||||
@@ -16,7 +16,11 @@ const useUpdateRoom = () => {
|
||||
const queryClient = useQueryClient();
|
||||
const { dispatch } = useUserRoomAvailable();
|
||||
|
||||
const { mutate: updateRoom, isPending: isUpdating } = useMutation({
|
||||
const {
|
||||
mutate: updateRoom,
|
||||
isPending: isUpdating,
|
||||
isSuccess,
|
||||
} = useMutation({
|
||||
mutationFn: updateRoomFn,
|
||||
onSuccess: (room) => {
|
||||
toast.success(UPDATE_SUCCESS);
|
||||
@@ -31,7 +35,7 @@ const useUpdateRoom = () => {
|
||||
onError: (err) => toast.error(err.message),
|
||||
});
|
||||
|
||||
return { isUpdating, updateRoom };
|
||||
return { isUpdating, updateRoom, isSuccess };
|
||||
};
|
||||
|
||||
export { useUpdateRoom };
|
||||
|
||||
Reference in New Issue
Block a user