mirror of
https://github.com/Nezumi-2711/react-training.git
synced 2026-09-22 13:38:51 +00:00
Format code style and update test case
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
|
// Helpers
|
||||||
import { formatCurrency } from '@helper/helper';
|
import { formatCurrency } from '@helper/helper';
|
||||||
|
|
||||||
describe('Helpers function test', () => {
|
describe('formatCurrency', () => {
|
||||||
test('Format number to currency format', () => {
|
test('Should format correctly', () => {
|
||||||
const result = formatCurrency(5000);
|
const result = formatCurrency(5000);
|
||||||
|
|
||||||
expect(result).toEqual('$5,000.00');
|
expect(result).toEqual('$5,000.00');
|
||||||
|
|||||||
@@ -1,42 +1,49 @@
|
|||||||
|
// Constants
|
||||||
import { REGEX } from '@constant/commons';
|
import { REGEX } from '@constant/commons';
|
||||||
import { isValidDiscount, isValidRegex, isValidString } from '@helper/validators';
|
|
||||||
|
|
||||||
describe('Check value is valid phone', () => {
|
// Helpers
|
||||||
test('Is valid phone', () => {
|
import {
|
||||||
|
isValidDiscount,
|
||||||
|
isValidRegex,
|
||||||
|
isValidString,
|
||||||
|
} from '@helper/validators';
|
||||||
|
|
||||||
|
describe('isValidPhone', () => {
|
||||||
|
test('Should is valid phone', () => {
|
||||||
const result = isValidRegex(new RegExp(REGEX.PHONE), '0327212321');
|
const result = isValidRegex(new RegExp(REGEX.PHONE), '0327212321');
|
||||||
|
|
||||||
expect(result).toEqual(true);
|
expect(result).toEqual(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Is invalid phone', () => {
|
test('Should is not valid phone', () => {
|
||||||
const result = isValidRegex(new RegExp(REGEX.PHONE), '0327212321Abc');
|
const result = isValidRegex(new RegExp(REGEX.PHONE), '0327212321Abc');
|
||||||
|
|
||||||
expect(result).toEqual(false);
|
expect(result).toEqual(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Check value is valid discount', () => {
|
describe('isValidDiscount', () => {
|
||||||
test('Is valid discount', () => {
|
test('Should is valid discount', () => {
|
||||||
const result = isValidDiscount(23);
|
const result = isValidDiscount(23);
|
||||||
|
|
||||||
expect(result).toEqual(true);
|
expect(result).toEqual(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Is invalid discount', () => {
|
test('Should is not valid discount', () => {
|
||||||
const result = isValidDiscount(101);
|
const result = isValidDiscount(101);
|
||||||
|
|
||||||
expect(result).toEqual(false);
|
expect(result).toEqual(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Check value is valid string', () => {
|
describe('Should is valid string', () => {
|
||||||
test('Is valid string', () => {
|
test('Is valid string', () => {
|
||||||
const result = isValidString('Phan Huu Loi');
|
const result = isValidString('Phan Huu Loi');
|
||||||
|
|
||||||
expect(result).toEqual(true);
|
expect(result).toEqual(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Is valid string', () => {
|
test('Should is not valid string', () => {
|
||||||
const result = isValidString('Phan');
|
const result = isValidString('Phan');
|
||||||
|
|
||||||
expect(result).toEqual(false);
|
expect(result).toEqual(false);
|
||||||
|
|||||||
@@ -1,16 +1,17 @@
|
|||||||
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||||
|
import { act, renderHook, waitFor } from '@testing-library/react';
|
||||||
|
import { MemoryRouter } from 'react-router-dom';
|
||||||
|
import { ReactNode } from 'react';
|
||||||
|
|
||||||
|
// Hooks
|
||||||
import { useRooms } from '@hook/rooms/useRooms';
|
import { useRooms } from '@hook/rooms/useRooms';
|
||||||
import { useCreateRoom } from '@hook/rooms/useCreateRoom';
|
import { useCreateRoom } from '@hook/rooms/useCreateRoom';
|
||||||
import {
|
|
||||||
QueryClient,
|
|
||||||
QueryClientProvider,
|
|
||||||
} from '@tanstack/react-query';
|
|
||||||
import { act, renderHook, waitFor } from '@testing-library/react';
|
|
||||||
import { IRoom } from '@type/rooms';
|
|
||||||
import { ReactNode } from 'react';
|
|
||||||
import { MemoryRouter } from 'react-router-dom';
|
|
||||||
import { useUpdateRoom } from '@hook/rooms/useUpdateRoom';
|
import { useUpdateRoom } from '@hook/rooms/useUpdateRoom';
|
||||||
import { useDeleteRoom } from '@hook/rooms/useDeleteRoom';
|
import { useDeleteRoom } from '@hook/rooms/useDeleteRoom';
|
||||||
|
|
||||||
|
// Types
|
||||||
|
import { IRoom } from '@type/rooms';
|
||||||
|
|
||||||
const mockUseRooms = jest.fn(useRooms);
|
const mockUseRooms = jest.fn(useRooms);
|
||||||
const mockUseCreateRoom = jest.fn(useCreateRoom);
|
const mockUseCreateRoom = jest.fn(useCreateRoom);
|
||||||
const mockUseUpdateRoom = jest.fn(useUpdateRoom);
|
const mockUseUpdateRoom = jest.fn(useUpdateRoom);
|
||||||
@@ -40,8 +41,8 @@ const wrapper = ({ children }: IWrapper) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('CRUD Room testing', () => {
|
describe('Room hooks', () => {
|
||||||
test('Fetch room data', async () => {
|
test('Should fetch data correctly', async () => {
|
||||||
mockUseRooms.mockImplementation(() => ({
|
mockUseRooms.mockImplementation(() => ({
|
||||||
rooms: [
|
rooms: [
|
||||||
{
|
{
|
||||||
@@ -66,11 +67,13 @@ describe('CRUD Room testing', () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Create room', async () => {
|
test('Should create room correctly', async () => {
|
||||||
mockUseCreateRoom.mockImplementation(() => ({
|
mockUseCreateRoom.mockImplementation(() => ({
|
||||||
createRoom: () => {sampleData},
|
createRoom: () => {
|
||||||
|
sampleData;
|
||||||
|
},
|
||||||
isCreating: false,
|
isCreating: false,
|
||||||
isSuccess: true
|
isSuccess: true,
|
||||||
}));
|
}));
|
||||||
const { result } = renderHook(() => mockUseCreateRoom(), { wrapper });
|
const { result } = renderHook(() => mockUseCreateRoom(), { wrapper });
|
||||||
|
|
||||||
@@ -83,11 +86,13 @@ describe('CRUD Room testing', () => {
|
|||||||
expect(result.current.isSuccess).toBeTruthy();
|
expect(result.current.isSuccess).toBeTruthy();
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Edit room', async () => {
|
test('Should edit room correctly', async () => {
|
||||||
mockUseUpdateRoom.mockImplementation(() => ({
|
mockUseUpdateRoom.mockImplementation(() => ({
|
||||||
updateRoom: () => {sampleData},
|
updateRoom: () => {
|
||||||
|
sampleData;
|
||||||
|
},
|
||||||
isUpdating: false,
|
isUpdating: false,
|
||||||
isSuccess: true
|
isSuccess: true,
|
||||||
}));
|
}));
|
||||||
const { result } = renderHook(() => mockUseUpdateRoom(), { wrapper });
|
const { result } = renderHook(() => mockUseUpdateRoom(), { wrapper });
|
||||||
|
|
||||||
@@ -101,11 +106,13 @@ describe('CRUD Room testing', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Delete room', async () => {
|
test('Should delete room correctly', async () => {
|
||||||
mockUseDeleteRoom.mockImplementation(() => ({
|
mockUseDeleteRoom.mockImplementation(() => ({
|
||||||
deleteRoom: () => {sampleData.id},
|
deleteRoom: () => {
|
||||||
|
sampleData.id;
|
||||||
|
},
|
||||||
isDeleting: false,
|
isDeleting: false,
|
||||||
isSuccess: true
|
isSuccess: true,
|
||||||
}));
|
}));
|
||||||
const { result } = renderHook(() => mockUseDeleteRoom(), { wrapper });
|
const { result } = renderHook(() => mockUseDeleteRoom(), { wrapper });
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
import { useDebounce } from '@hook/useDebounce';
|
|
||||||
import { fireEvent, render } from '@testing-library/react';
|
import { fireEvent, render } from '@testing-library/react';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { act } from 'react-test-renderer';
|
import { act } from 'react-test-renderer';
|
||||||
|
|
||||||
|
// Hooks
|
||||||
|
import { useDebounce } from '@hook/useDebounce';
|
||||||
|
|
||||||
|
jest.useFakeTimers();
|
||||||
|
|
||||||
const TestComponent = ({ initialValue = 0 }: { initialValue?: number }) => {
|
const TestComponent = ({ initialValue = 0 }: { initialValue?: number }) => {
|
||||||
const [value, setValue] = useState(initialValue);
|
const [value, setValue] = useState(initialValue);
|
||||||
const debouncedValue = useDebounce<number>(value, 1000);
|
const debouncedValue = useDebounce<number>(value, 1000);
|
||||||
@@ -17,29 +21,25 @@ const TestComponent = ({ initialValue = 0 }: { initialValue?: number }) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
describe('useDebouncedValue', function () {
|
describe('useDebouncedValue', function () {
|
||||||
afterEach(() => {
|
|
||||||
jest.useRealTimers();
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Debounce value should not change before 1 second', () => {
|
test('Debounce value should not change before 1 second', () => {
|
||||||
jest.useFakeTimers();
|
|
||||||
const { getByTestId, getByText } = render(<TestComponent />);
|
const { getByTestId, getByText } = render(<TestComponent />);
|
||||||
const incrementButton = getByText('Increment');
|
const incrementButton = getByText('Increment');
|
||||||
const debouncedValue = getByTestId('debouncedValue');
|
const debouncedValue = getByTestId('debouncedValue');
|
||||||
const value = getByTestId('value');
|
const value = getByTestId('value');
|
||||||
|
|
||||||
const incrementAndPassTime = (passedTime: number) => {
|
const clickAndWaitTime = (passedTime: number) => {
|
||||||
act(() => {
|
act(() => {
|
||||||
fireEvent.click(incrementButton);
|
fireEvent.click(incrementButton);
|
||||||
jest.advanceTimersByTime(passedTime);
|
jest.advanceTimersByTime(passedTime);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
incrementAndPassTime(999);
|
|
||||||
|
clickAndWaitTime(100);
|
||||||
|
|
||||||
expect(debouncedValue.textContent).toBe('0');
|
expect(debouncedValue.textContent).toBe('0');
|
||||||
expect(value.textContent).toBe('1');
|
expect(value.textContent).toBe('1');
|
||||||
|
|
||||||
incrementAndPassTime(1000);
|
clickAndWaitTime(1000);
|
||||||
|
|
||||||
expect(debouncedValue.textContent).toBe('1');
|
expect(debouncedValue.textContent).toBe('1');
|
||||||
expect(value.textContent).toBe('2');
|
expect(value.textContent).toBe('2');
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import { useOutsideClick } from '@hook/useOutsideClick';
|
|
||||||
import { fireEvent, render, renderHook, screen } from '@testing-library/react';
|
import { fireEvent, render, renderHook, screen } from '@testing-library/react';
|
||||||
|
|
||||||
describe('useOutsideClick testing', () => {
|
// Hooks
|
||||||
|
import { useOutsideClick } from '@hook/useOutsideClick';
|
||||||
|
|
||||||
|
describe('useOutsideClick', () => {
|
||||||
test('Call handler when click outside element', () => {
|
test('Call handler when click outside element', () => {
|
||||||
// Arrange
|
// Arrange
|
||||||
const handler = jest.fn();
|
const handler = jest.fn();
|
||||||
|
|||||||
@@ -1,13 +1,17 @@
|
|||||||
import { useCreateUser } from '@hook/users/useCreateUser';
|
|
||||||
import { useUpdateUser } from '@hook/users/useUpdateUser';
|
|
||||||
import { useUsers } from '@hook/users/useUsers';
|
|
||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||||
import { renderHook, waitFor } from '@testing-library/react';
|
import { renderHook, waitFor } from '@testing-library/react';
|
||||||
import { IUser } from '@type/users';
|
|
||||||
import { ReactNode } from 'react';
|
import { ReactNode } from 'react';
|
||||||
import { MemoryRouter } from 'react-router-dom';
|
import { MemoryRouter } from 'react-router-dom';
|
||||||
import { act } from 'react-test-renderer';
|
import { act } from 'react-test-renderer';
|
||||||
|
|
||||||
|
// Hooks
|
||||||
|
import { useCreateUser } from '@hook/users/useCreateUser';
|
||||||
|
import { useUpdateUser } from '@hook/users/useUpdateUser';
|
||||||
|
import { useUsers } from '@hook/users/useUsers';
|
||||||
|
|
||||||
|
// Types
|
||||||
|
import { IUser } from '@type/users';
|
||||||
|
|
||||||
const mockUseUsers = jest.fn(useUsers);
|
const mockUseUsers = jest.fn(useUsers);
|
||||||
const mockUseCreateUser = jest.fn(useCreateUser);
|
const mockUseCreateUser = jest.fn(useCreateUser);
|
||||||
const mockUseUpdateUser = jest.fn(useUpdateUser);
|
const mockUseUpdateUser = jest.fn(useUpdateUser);
|
||||||
@@ -37,8 +41,8 @@ const wrapper = ({ children }: IWrapper) => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('CRUD User testing', () => {
|
describe('Users hook', () => {
|
||||||
test('Fetch user data', async () => {
|
test('Should fetch user data correctly', async () => {
|
||||||
mockUseUsers.mockImplementation(() => ({
|
mockUseUsers.mockImplementation(() => ({
|
||||||
users: [
|
users: [
|
||||||
{
|
{
|
||||||
@@ -55,17 +59,17 @@ describe('CRUD User testing', () => {
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
isLoading: false,
|
isLoading: false,
|
||||||
}))
|
}));
|
||||||
const { result } = renderHook(() => mockUseUsers(), { wrapper });
|
const { result } = renderHook(() => mockUseUsers(), { wrapper });
|
||||||
|
|
||||||
await waitFor(() =>
|
await waitFor(() => expect(result.current.users?.length).toEqual(2));
|
||||||
expect(result.current.users?.length).toEqual(2)
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Create user', async () => {
|
test('Should create user correctly', async () => {
|
||||||
mockUseCreateUser.mockImplementation(() => ({
|
mockUseCreateUser.mockImplementation(() => ({
|
||||||
createUser: () => {sampleData},
|
createUser: () => {
|
||||||
|
sampleData;
|
||||||
|
},
|
||||||
isCreating: false,
|
isCreating: false,
|
||||||
isSuccess: true,
|
isSuccess: true,
|
||||||
}));
|
}));
|
||||||
@@ -81,9 +85,11 @@ describe('CRUD User testing', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Update user', async () => {
|
test('Should update user correctly', async () => {
|
||||||
mockUseUpdateUser.mockImplementation(() => ({
|
mockUseUpdateUser.mockImplementation(() => ({
|
||||||
updateUser: () => {sampleData},
|
updateUser: () => {
|
||||||
|
sampleData;
|
||||||
|
},
|
||||||
isUpdating: false,
|
isUpdating: false,
|
||||||
isSuccess: true,
|
isSuccess: true,
|
||||||
}));
|
}));
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ const sampleData: IRoom = {
|
|||||||
status: true,
|
status: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('CRUD Room testing', () => {
|
describe('Rooms service', () => {
|
||||||
test('Fetch room data', async () => {
|
test('Should fetch room correctly', async () => {
|
||||||
mockGetAllRooms.mockResolvedValue([
|
mockGetAllRooms.mockResolvedValue([
|
||||||
{
|
{
|
||||||
id: 1,
|
id: 1,
|
||||||
@@ -44,7 +44,7 @@ describe('CRUD Room testing', () => {
|
|||||||
await waitFor(() => expect(result.length).toEqual(2));
|
await waitFor(() => expect(result.length).toEqual(2));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Create room', async () => {
|
test('Should create room correctly', async () => {
|
||||||
mockCreateRoom.mockResolvedValue(sampleData);
|
mockCreateRoom.mockResolvedValue(sampleData);
|
||||||
|
|
||||||
const result = await mockCreateRoom(sampleData);
|
const result = await mockCreateRoom(sampleData);
|
||||||
@@ -52,7 +52,7 @@ describe('CRUD Room testing', () => {
|
|||||||
await waitFor(() => expect(result).toBeTruthy());
|
await waitFor(() => expect(result).toBeTruthy());
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Update room', async () => {
|
test('Should update room correctly', async () => {
|
||||||
mockUpdateRoom.mockResolvedValue({
|
mockUpdateRoom.mockResolvedValue({
|
||||||
id: 1,
|
id: 1,
|
||||||
name: 'Nezumi',
|
name: 'Nezumi',
|
||||||
@@ -64,7 +64,7 @@ describe('CRUD Room testing', () => {
|
|||||||
await waitFor(() => expect(result).toBeTruthy());
|
await waitFor(() => expect(result).toBeTruthy());
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Delete room', async () => {
|
test('Should delete room correctly', async () => {
|
||||||
mockDeleteRoom.mockImplementation(() => Promise.resolve());
|
mockDeleteRoom.mockImplementation(() => Promise.resolve());
|
||||||
await mockDeleteRoom(1);
|
await mockDeleteRoom(1);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -17,20 +17,20 @@ const sampleData: IUser = {
|
|||||||
isBooked: true,
|
isBooked: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('CRUD User testing', () => {
|
describe('User services', () => {
|
||||||
test('Fetch user data', async () => {
|
test('Should fetch user data correctly', async () => {
|
||||||
mockGetAllUsers.mockResolvedValue([
|
mockGetAllUsers.mockResolvedValue([
|
||||||
{
|
{
|
||||||
id: 1,
|
id: 1,
|
||||||
name: 'Nezumi',
|
name: 'Nezumi',
|
||||||
phone: '0324422123',
|
phone: '0324422123',
|
||||||
isBooked: true,
|
isBooked: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 2,
|
id: 2,
|
||||||
name: 'Loi Phan',
|
name: 'Loi Phan',
|
||||||
phone: '0324422145',
|
phone: '0324422145',
|
||||||
isBooked: false,
|
isBooked: false,
|
||||||
},
|
},
|
||||||
]);
|
]);
|
||||||
const result = await mockGetAllUsers('name', 'asc', '');
|
const result = await mockGetAllUsers('name', 'asc', '');
|
||||||
@@ -38,14 +38,14 @@ describe('CRUD User testing', () => {
|
|||||||
await waitFor(() => expect(result.length).toEqual(2));
|
await waitFor(() => expect(result.length).toEqual(2));
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Create user', async () => {
|
test('Should create user correctly', async () => {
|
||||||
mockCreateUser.mockResolvedValue(sampleData);
|
mockCreateUser.mockResolvedValue(sampleData);
|
||||||
const result = await mockCreateUser(sampleData);
|
const result = await mockCreateUser(sampleData);
|
||||||
|
|
||||||
await waitFor(() => expect(result).toBeTruthy());
|
await waitFor(() => expect(result).toBeTruthy());
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Update user', async () => {
|
test('Should update user correctly', async () => {
|
||||||
mockUpdateUser.mockResolvedValue(sampleData);
|
mockUpdateUser.mockResolvedValue(sampleData);
|
||||||
const result = await mockUpdateUser(sampleData);
|
const result = await mockUpdateUser(sampleData);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user