diff --git a/hotel-management/src/App.test.tsx b/hotel-management/src/App.test.tsx new file mode 100644 index 0000000..3de1685 --- /dev/null +++ b/hotel-management/src/App.test.tsx @@ -0,0 +1,51 @@ +import { render, act, RenderResult } from '@testing-library/react'; +import * as userServices from '@service/userServices'; +import * as roomServices from '@service/roomServices'; +import App from './App'; + +jest.mock('@service/userServices'); +jest.mock('@service/roomServices'); + +describe('App', () => { + let wrapper: RenderResult | null = null; + + beforeEach(async () => { + await act(async () => { + wrapper = render(); + }); + }); + + test('should render correctly', () => { + expect(wrapper).toMatchSnapshot(); + }); + + test('Should init room, user data', async () => { + const mockGetUserNotBooked = jest.spyOn(userServices, 'getUserNotBooked'); + const mockGetRoomAvailable = jest.spyOn(roomServices, 'getRoomsAvailable'); + + mockGetUserNotBooked.mockResolvedValue([ + { + id: 1, + name: 'Nezumi', + }, + { + id: 2, + name: 'Loi Phan', + }, + ]); + + mockGetRoomAvailable.mockResolvedValue([ + { + id: 1, + name: 'Room 1', + }, + { + id: 2, + name: 'Room 2', + }, + ]); + + expect(mockGetUserNotBooked).toHaveBeenCalled(); + expect(mockGetRoomAvailable).toHaveBeenCalled(); + }); +}); diff --git a/hotel-management/src/__snapshots__/App.test.tsx.snap b/hotel-management/src/__snapshots__/App.test.tsx.snap index 9314d15..5a3bca4 100644 --- a/hotel-management/src/__snapshots__/App.test.tsx.snap +++ b/hotel-management/src/__snapshots__/App.test.tsx.snap @@ -1154,3 +1154,388 @@ exports[`App Should render correctly 1`] = ` "unmount": [Function], } `; + +exports[`App should render correctly 1`] = ` +{ + "asFragment": [Function], + "baseElement": +
+
+
+

+ Hi, + Admin + ! +

+
    +
  • + +
  • +
  • + +
  • +
+
+ +
+
+

+ This page currently still develop. Please try again later! +

+
+
+
+
+
+ , + "container":
+
+
+

+ Hi, + Admin + ! +

+
    +
  • + +
  • +
  • + +
  • +
+
+ +
+
+

+ This page currently still develop. Please try again later! +

+
+
+
+
+
, + "debug": [Function], + "findAllByAltText": [Function], + "findAllByDisplayValue": [Function], + "findAllByLabelText": [Function], + "findAllByPlaceholderText": [Function], + "findAllByRole": [Function], + "findAllByTestId": [Function], + "findAllByText": [Function], + "findAllByTitle": [Function], + "findByAltText": [Function], + "findByDisplayValue": [Function], + "findByLabelText": [Function], + "findByPlaceholderText": [Function], + "findByRole": [Function], + "findByTestId": [Function], + "findByText": [Function], + "findByTitle": [Function], + "getAllByAltText": [Function], + "getAllByDisplayValue": [Function], + "getAllByLabelText": [Function], + "getAllByPlaceholderText": [Function], + "getAllByRole": [Function], + "getAllByTestId": [Function], + "getAllByText": [Function], + "getAllByTitle": [Function], + "getByAltText": [Function], + "getByDisplayValue": [Function], + "getByLabelText": [Function], + "getByPlaceholderText": [Function], + "getByRole": [Function], + "getByTestId": [Function], + "getByText": [Function], + "getByTitle": [Function], + "queryAllByAltText": [Function], + "queryAllByDisplayValue": [Function], + "queryAllByLabelText": [Function], + "queryAllByPlaceholderText": [Function], + "queryAllByRole": [Function], + "queryAllByTestId": [Function], + "queryAllByText": [Function], + "queryAllByTitle": [Function], + "queryByAltText": [Function], + "queryByDisplayValue": [Function], + "queryByLabelText": [Function], + "queryByPlaceholderText": [Function], + "queryByRole": [Function], + "queryByTestId": [Function], + "queryByText": [Function], + "queryByTitle": [Function], + "rerender": [Function], + "unmount": [Function], +} +`; diff --git a/hotel-management/src/contexts/__tests__/userRoomAvailableContext.test.tsx b/hotel-management/src/contexts/__tests__/userRoomAvailableContext.test.tsx index 5cf3bda..672476c 100644 --- a/hotel-management/src/contexts/__tests__/userRoomAvailableContext.test.tsx +++ b/hotel-management/src/contexts/__tests__/userRoomAvailableContext.test.tsx @@ -1,40 +1,129 @@ -import { render, act } from '@testing-library/react'; -import * as userServices from '@service/userServices'; -import * as roomServices from '@service/roomServices'; -import App from '../../App'; +import { reducer } from '@context/UserRoomAvailableContext'; +import { IDataState } from '@type/common'; jest.mock('@service/userServices'); jest.mock('@service/roomServices'); -describe('App', () => { - test('Should init room, user data', async () => { - const mockGetUserNotBooked = jest.spyOn(userServices, 'getUserNotBooked'); - const mockGetRoomAvailable = jest.spyOn(roomServices, 'getRoomsAvailable'); +describe('userRoomAvailableContext', () => { + let initialState: { + roomsAvailable: IDataState[]; + usersAvailable: IDataState[]; + }; - mockGetUserNotBooked.mockResolvedValue([ - { - id: 1, - name: 'Nezumi', - }, - { - id: 2, - name: 'Loi Phan', - }, - ]); + const sampleRooms = [ + { + id: 2, + name: 'Room 2', + }, + { + id: 3, + name: 'Room 3', + }, + ]; - mockGetRoomAvailable.mockResolvedValue([ - { - id: 1, - name: 'Room 1', - }, - { - id: 2, - name: 'Room 2', - }, - ]); + const sampleUsers = [ + { + id: 2, + name: 'Loi Phan', + }, + { + id: 3, + name: 'Van A', + }, + ]; - await act(async () => { - render(); + beforeEach(() => { + initialState = { + roomsAvailable: [ + { + id: 1, + name: 'Room 1', + }, + ], + usersAvailable: [ + { + id: 1, + name: 'Nezumi', + }, + ], + }; + }); + + test('Should init user correctly', async () => { + const state = reducer(initialState, { + type: 'initUser', + payload: sampleUsers, + }); + + expect(state.usersAvailable.length).toEqual(2); + }); + + test('Should add user correctly', async () => { + const state = reducer(initialState, { + type: 'addUser', + payload: [{ id: 2, name: 'Test Name' }], + }); + + expect(state.usersAvailable.length).toEqual(2); + }); + + test('Should remove user correctly', async () => { + const state = reducer(initialState, { + type: 'removeUser', + payload: [{ id: 1 }], + }); + + expect(state.usersAvailable.length).toEqual(0); + }); + + test('Should update user correctly', async () => { + const state = reducer(initialState, { + type: 'updateUserName', + payload: [{ id: 1, name: 'Update name' }], + }); + + expect(state.usersAvailable[0]).toEqual({ + id: 1, + name: 'Update name' + }); + }); + + test('Should init room correctly', async () => { + const state = reducer(initialState, { + type: 'initRoom', + payload: sampleRooms, + }); + + expect(state.roomsAvailable.length).toEqual(2); + }); + + test('Should add room correctly', async () => { + const state = reducer(initialState, { + type: 'addRoom', + payload: [{ id: 2, name: 'Test Room' }], + }); + + expect(state.roomsAvailable.length).toEqual(2); + }); + + test('Should remove room correctly', async () => { + const state = reducer(initialState, { + type: 'removeRoom', + payload: [{ id: 1 }], + }); + + expect(state.roomsAvailable.length).toEqual(0); + }); + + test('Should update room correctly', async () => { + const state = reducer(initialState, { + type: 'updateRoomName', + payload: [{ id: 1, name: 'Update name' }], + }); + + expect(state.roomsAvailable[0]).toEqual({ + id: 1, + name: 'Update name' }); }); }); diff --git a/hotel-management/src/pages/User/__tests__/UserForm.test.tsx b/hotel-management/src/pages/User/__tests__/UserForm.test.tsx index 0313107..5be12ff 100644 --- a/hotel-management/src/pages/User/__tests__/UserForm.test.tsx +++ b/hotel-management/src/pages/User/__tests__/UserForm.test.tsx @@ -1,22 +1,49 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; import { BrowserRouter } from 'react-router-dom'; -import renderer from 'react-test-renderer'; + +jest.mock('@hook/users/useCreateUser.ts', () => ({ + ...jest.requireActual('@hook/users/useCreateUser.ts'), + useCreateUser: () => ({ + isCreating: true, + createUser: jest.fn(), + }), +})); // Components import UserForm from '../UserForm'; +import { RenderResult, fireEvent, render } from '@testing-library/react'; +import { act } from 'react-test-renderer'; describe('UserForm', () => { const queryClient = new QueryClient(); - const wrapper = renderer.create( - - - - - - ); + let wrapper: RenderResult | null = null; + + beforeEach(() => { + wrapper = render( + + + + + + ); + }); test('Should render correctly', () => { expect(wrapper).toMatchSnapshot(); }); + + test('Should submit correctly', async () => { + const nameField = wrapper!.container.querySelector('#name'); + const phoneField = wrapper!.container.querySelector('#phone'); + const submitBtn = wrapper!.getByText('Add'); + + await act(async () => { + fireEvent.input(nameField!, { target: { value: 'Phan Huu Loi' } }); + fireEvent.input(phoneField!, { target: { value: '0324432321' } }); + fireEvent.click(submitBtn); + }); + + wrapper?.debug(); + }); }); diff --git a/hotel-management/src/types/common.ts b/hotel-management/src/types/common.ts index 825c8f3..b98d8b5 100644 --- a/hotel-management/src/types/common.ts +++ b/hotel-management/src/types/common.ts @@ -2,7 +2,7 @@ type Nullable = T | null; interface IDataState { id: number; - name: string; + name?: string; } export type { Nullable, IDataState };