Format code style and update test case

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