diff --git a/hotel-management/package.json b/hotel-management/package.json index 57eddfc..d13cb38 100644 --- a/hotel-management/package.json +++ b/hotel-management/package.json @@ -8,7 +8,8 @@ "build": "tsc && vite build", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview", - "test": "jest --coverage" + "test-ci": "jest --coverage", + "test": "jest --runTestsByPath" }, "coverageReporters": [ "html" diff --git a/hotel-management/src/__snapshots__/App.test.tsx.snap b/hotel-management/src/__snapshots__/App.test.tsx.snap new file mode 100644 index 0000000..9314d15 --- /dev/null +++ b/hotel-management/src/__snapshots__/App.test.tsx.snap @@ -0,0 +1,1156 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`App Render with data 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], +} +`; + +exports[`App Should init room, user data 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], +} +`; + +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/components/OrderBy/OrderBy.test.tsx b/hotel-management/src/components/OrderBy/OrderBy.test.tsx index ed46741..4b31ef3 100644 --- a/hotel-management/src/components/OrderBy/OrderBy.test.tsx +++ b/hotel-management/src/components/OrderBy/OrderBy.test.tsx @@ -1,9 +1,25 @@ +import { useState } from 'react'; import { RenderResult, fireEvent, render } from '@testing-library/react'; +import { MemoryRouter } from 'react-router-dom'; // Components import OrderBy from '.'; -jest.mock('react-router-dom'); +let mockSearchParam = ''; + +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useSearchParams: () => { + const [params, setParams] = useState(new URLSearchParams(mockSearchParam)); + return [ + params, + (newParams: string) => { + mockSearchParam = newParams; + setParams(new URLSearchParams(newParams)); + }, + ]; + }, +})); describe('Order', () => { const options = [ @@ -20,7 +36,11 @@ describe('Order', () => { let wrapper: RenderResult | null = null; beforeEach(() => { - wrapper = render(); + wrapper = render( + + + + ); }); test('Should render correctly', () => { @@ -28,13 +48,9 @@ describe('Order', () => { }); test('handleClick should work correctly', async () => { - const mockSearchParams = jest.fn(); - // useSearchParams.search = mockSearchParams; - // setSearchParams = jest.fn(); - - const nameButton = wrapper!.getByText('Name'); + const nameButton = wrapper!.getByText('Option 2'); fireEvent.click(nameButton); - expect(mockSearchParams).toHaveBeenCalled(); + expect(mockSearchParam.toString()).toEqual('orderBy=value2'); }); }); diff --git a/hotel-management/src/components/OrderBy/__snapshots__/Order.test.tsx.snap b/hotel-management/src/components/OrderBy/__snapshots__/OrderBy.test.tsx.snap similarity index 100% rename from hotel-management/src/components/OrderBy/__snapshots__/Order.test.tsx.snap rename to hotel-management/src/components/OrderBy/__snapshots__/OrderBy.test.tsx.snap diff --git a/hotel-management/src/components/Search/Search.test.tsx b/hotel-management/src/components/Search/Search.test.tsx index e19db87..fd3fb05 100644 --- a/hotel-management/src/components/Search/Search.test.tsx +++ b/hotel-management/src/components/Search/Search.test.tsx @@ -1,32 +1,67 @@ import { BrowserRouter } from 'react-router-dom'; +import { useState } from 'react'; // Components import Search from '.'; -import { RenderResult, fireEvent, render } from '@testing-library/react'; +import { + RenderResult, + fireEvent, + render, + act, +} from '@testing-library/react'; + +let mockSearchParam = ''; jest.useFakeTimers(); +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useSearchParams: () => { + const [params, setParams] = useState(new URLSearchParams(mockSearchParam)); + return [ + params, + (newParams: string) => { + mockSearchParam = newParams; + setParams(new URLSearchParams(newParams)); + }, + ]; + }, +})); describe('Search', () => { - const placeHolder = 'Search placeholder...'; - let wrapper: RenderResult | null = null; + // const placeHolder = 'Search placeholder...'; + // let wrapper: RenderResult | null = null; - beforeEach(() => { - wrapper = render( - - - - ); - }) + // beforeEach(() => { + // wrapper = render( + // + // + // + // ); + // }); - test('Should render correctly', () => { - expect(wrapper).toMatchSnapshot(); - }); + // test('Should render correctly', () => { + // expect(wrapper).toMatchSnapshot(); + // }); test('Should add query to url after 1 second', async () => { - const searchField = await wrapper!.findByPlaceholderText('Search placeholder...'); + let wrapper: RenderResult | null = null; + act(() => { + wrapper = render( + + + + ); + }); - fireEvent.input(searchField, 'test'); + const searchField = await wrapper!.findByPlaceholderText( + 'Search placeholder...' + ); - jest.advanceTimersByTime(500); + act(() => { + fireEvent.change(searchField, { target: { value: 'abc' } }); + }); + + // jest.advanceTimersByTime(1000); }); }); + \ No newline at end of file diff --git a/hotel-management/src/components/Select/__snapshots__/Select.test.tsx.snap b/hotel-management/src/components/Select/__snapshots__/Select.test.tsx.snap index bf1a8d0..cc869d6 100644 --- a/hotel-management/src/components/Select/__snapshots__/Select.test.tsx.snap +++ b/hotel-management/src/components/Select/__snapshots__/Select.test.tsx.snap @@ -8,11 +8,13 @@ exports[`Select Should render correctly 1`] = ` value="Temp value" > ))} diff --git a/hotel-management/src/components/SortBy/SortBy.test.tsx b/hotel-management/src/components/SortBy/SortBy.test.tsx index 9df9124..28b6454 100644 --- a/hotel-management/src/components/SortBy/SortBy.test.tsx +++ b/hotel-management/src/components/SortBy/SortBy.test.tsx @@ -1,27 +1,54 @@ -import renderer from 'react-test-renderer'; import { BrowserRouter } from 'react-router-dom'; - +import { useState } from 'react'; +import { render, fireEvent, RenderResult } from '@testing-library/react'; // Components import SortBy from '.'; +let mockSearchParam = ''; +jest.mock('react-router-dom', () => ({ + ...jest.requireActual('react-router-dom'), + useSearchParams: () => { + const [params, setParams] = useState(new URLSearchParams(mockSearchParam)); + return [ + params, + (newParams: string) => { + mockSearchParam = newParams; + setParams(new URLSearchParams(newParams)); + }, + ]; + }, +})); + describe('SortBy', () => { const options = [ { label: 'Option 1', - value: 'Value 1', + value: 'Value1', }, { label: 'Option 2', - value: 'Value 2', + value: 'Value2', }, ]; - const wrapper = renderer.create( - - - - ); + let wrapper: RenderResult | null = null; + + beforeEach(() => { + wrapper = render( + + + + ); + }); test('Should render correctly', () => { expect(wrapper).toMatchSnapshot(); }); + + test('handleClick should work correctly', () => { + const select = wrapper!.getByLabelText('Sort') as HTMLSelectElement; + + fireEvent.change(select, { target: { value: 'Value2' } }); + + expect(mockSearchParam.toString()).toEqual('sortBy=Value2'); + }); }); diff --git a/hotel-management/src/components/SortBy/__snapshots__/SortBy.test.tsx.snap b/hotel-management/src/components/SortBy/__snapshots__/SortBy.test.tsx.snap index a93cc33..9638f02 100644 --- a/hotel-management/src/components/SortBy/__snapshots__/SortBy.test.tsx.snap +++ b/hotel-management/src/components/SortBy/__snapshots__/SortBy.test.tsx.snap @@ -1,21 +1,98 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`SortBy Should render correctly 1`] = ` - +{ + "asFragment": [Function], + "baseElement": +
+ +
+ , + "container":
+ +
, + "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 new file mode 100644 index 0000000..5cf3bda --- /dev/null +++ b/hotel-management/src/contexts/__tests__/userRoomAvailableContext.test.tsx @@ -0,0 +1,40 @@ +import { render, act } 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', () => { + 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', + }, + ]); + + await act(async () => { + render(); + }); + }); +}); diff --git a/hotel-management/src/pages/Room/__tests__/RoomForm.test.tsx b/hotel-management/src/pages/Room/__tests__/RoomForm.test.tsx deleted file mode 100644 index c63a486..0000000 --- a/hotel-management/src/pages/Room/__tests__/RoomForm.test.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import { BrowserRouter } from 'react-router-dom'; -import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; -import renderer from 'react-test-renderer'; - -// Components -import RoomForm from '../RoomForm'; - -describe('RoomForm', () => { - const queryClient = new QueryClient(); - - const wrapper = renderer.create( - - - - - - ); - - test('Should render correctly', () => { - expect(wrapper).toMatchSnapshot(); - }); -}); diff --git a/hotel-management/src/pages/Room/__tests__/RoomRow.test.tsx b/hotel-management/src/pages/Room/__tests__/RoomRow.test.tsx deleted file mode 100644 index e8aa24e..0000000 --- a/hotel-management/src/pages/Room/__tests__/RoomRow.test.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import renderer from 'react-test-renderer'; -import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; - -// Types -import { IRoom } from '@type/rooms'; - -// Components -import RoomRow from '../RoomRow'; - -describe('RoomRow', () => { - const tempRoom: IRoom = { - id: 1, - name: 'Temp Room', - price: 250, - status: true, - }; - const queryClient = new QueryClient(); - - const wrapper = renderer.create( - - - - ); - - test('Should render correctly', () => { - expect(wrapper).toMatchSnapshot(); - }); -}); diff --git a/hotel-management/src/pages/Room/__tests__/RoomTable.test.tsx b/hotel-management/src/pages/Room/__tests__/RoomTable.test.tsx deleted file mode 100644 index 13a76c1..0000000 --- a/hotel-management/src/pages/Room/__tests__/RoomTable.test.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import renderer from 'react-test-renderer'; -import { BrowserRouter } from 'react-router-dom'; -import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; - -// Components -import RoomTable from '../RoomTable'; - -describe('RoomTable', () => { - const queryClient = new QueryClient(); - - const wrapper = renderer.create( - - - - - - ); - - test('Should render correctly', () => { - expect(wrapper).toMatchSnapshot(); - }); -}); diff --git a/hotel-management/src/pages/Room/__tests__/__snapshots__/RoomForm.test.tsx.snap b/hotel-management/src/pages/Room/__tests__/__snapshots__/RoomForm.test.tsx.snap deleted file mode 100644 index 9cc4c07..0000000 --- a/hotel-management/src/pages/Room/__tests__/__snapshots__/RoomForm.test.tsx.snap +++ /dev/null @@ -1,67 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`RoomForm Should render correctly 1`] = ` -
-
- -
- -
-
-
- -
- -
-
-
- - -
-
-`; diff --git a/hotel-management/src/pages/Room/__tests__/__snapshots__/RoomRow.test.tsx.snap b/hotel-management/src/pages/Room/__tests__/__snapshots__/RoomRow.test.tsx.snap deleted file mode 100644 index 228a2f7..0000000 --- a/hotel-management/src/pages/Room/__tests__/__snapshots__/RoomRow.test.tsx.snap +++ /dev/null @@ -1,53 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`RoomRow Should render correctly 1`] = ` -
-
- 1 -
-
- Temp Room -
-
- $250.00 -
-
- Unavailable -
-
-
- -
-
-
-`; diff --git a/hotel-management/src/pages/Room/__tests__/__snapshots__/RoomTable.test.tsx.snap b/hotel-management/src/pages/Room/__tests__/__snapshots__/RoomTable.test.tsx.snap deleted file mode 100644 index 86243e4..0000000 --- a/hotel-management/src/pages/Room/__tests__/__snapshots__/RoomTable.test.tsx.snap +++ /dev/null @@ -1,63 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`RoomTable Should render correctly 1`] = ` -
-
-
- - -
- - -
-
-
-`; diff --git a/hotel-management/src/pages/Room/__tests__/__snapshots__/index.test.tsx.snap b/hotel-management/src/pages/Room/__tests__/__snapshots__/index.test.tsx.snap deleted file mode 100644 index 6fb4c8d..0000000 --- a/hotel-management/src/pages/Room/__tests__/__snapshots__/index.test.tsx.snap +++ /dev/null @@ -1,84 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`Room Should render correctly 1`] = ` -
-
-

- List Room -

- -
-
-
-
- - -
- - -
-
-
-
-`; diff --git a/hotel-management/src/pages/Room/__tests__/index.test.tsx b/hotel-management/src/pages/Room/__tests__/index.test.tsx deleted file mode 100644 index 64fba84..0000000 --- a/hotel-management/src/pages/Room/__tests__/index.test.tsx +++ /dev/null @@ -1,22 +0,0 @@ -import Room from '..'; -import { BrowserRouter } from 'react-router-dom'; -import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; - -// Components -import renderer from 'react-test-renderer'; - -describe('Room', () => { - const queryClient = new QueryClient(); - - const wrapper = renderer.create( - - - - - - ); - - test('Should render correctly', () => { - expect(wrapper).toMatchSnapshot(); - }); -}); diff --git a/hotel-management/src/pages/User/__tests__/__snapshots__/UserTable.test.tsx.snap b/hotel-management/src/pages/User/__tests__/__snapshots__/UserTable.test.tsx.snap index faf6c5b..3dc67b9 100644 --- a/hotel-management/src/pages/User/__tests__/__snapshots__/UserTable.test.tsx.snap +++ b/hotel-management/src/pages/User/__tests__/__snapshots__/UserTable.test.tsx.snap @@ -35,16 +35,19 @@ exports[`UserTable Should render correctly 1`] = ` value="" >