diff --git a/hotel-management/src/components/Menus/Menus.test.tsx b/hotel-management/src/components/Menus/Menus.test.tsx index 5e4039d..781e901 100644 --- a/hotel-management/src/components/Menus/Menus.test.tsx +++ b/hotel-management/src/components/Menus/Menus.test.tsx @@ -1,31 +1,60 @@ -import renderer from 'react-test-renderer'; - // Components import Menus from '.'; import { RiEditBoxFill, RiDeleteBin2Line } from 'react-icons/ri'; +import { RenderResult, fireEvent, render } from '@testing-library/react'; describe('Menus', () => { const id = '123'; const handleOnClick = jest.fn(); - const wrapper = renderer.create( - - - + let wrapper: RenderResult | null = null; - - }> - Edit - - }> - Delete - - - - - ); + beforeEach(() => { + wrapper = render( + + + - test('Should render correctly', () => { + + }> + Edit + + }> + Delete + + + + + ); + }); + + test('Should render correctly', async () => { expect(wrapper).toMatchSnapshot(); }); + + test('Should show context menu', async () => { + const btn = await wrapper!.findByRole('button'); + + fireEvent.click(btn); + + const editBtn = wrapper!.getByText('Edit'); + + expect(editBtn).toBeInTheDocument(); + + fireEvent.click(btn); + + expect(editBtn).not.toBeInTheDocument(); + }); + + test('HandleOnClick should work', async () => { + const btn = await wrapper!.findByRole('button'); + + fireEvent.click(btn); + + const editBtn = wrapper!.getByText('Edit'); + + fireEvent.click(editBtn); + + expect(handleOnClick).toHaveBeenCalled(); + }); }); diff --git a/hotel-management/src/components/Menus/__snapshots__/Menus.test.tsx.snap b/hotel-management/src/components/Menus/__snapshots__/Menus.test.tsx.snap index e6606ee..7dee797 100644 --- a/hotel-management/src/components/Menus/__snapshots__/Menus.test.tsx.snap +++ b/hotel-management/src/components/Menus/__snapshots__/Menus.test.tsx.snap @@ -1,35 +1,114 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`Menus Should render correctly 1`] = ` -
- +
+ + , + "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/components/OrderBy/Order.test.tsx b/hotel-management/src/components/OrderBy/Order.test.tsx deleted file mode 100644 index e3e47a0..0000000 --- a/hotel-management/src/components/OrderBy/Order.test.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import renderer from 'react-test-renderer'; -import { BrowserRouter } from 'react-router-dom'; - -// Components -import Order from '.'; - -describe('Order', () => { - const options = [ - { - value: 'Value 1', - label: 'Option 1', - }, - { - value: 'Value 2', - label: 'Option 2', - }, - ]; - - const wrapper = renderer.create( - - - - ); - - test('Should render correctly', () => { - expect(wrapper).toMatchSnapshot(); - }); -}); diff --git a/hotel-management/src/components/OrderBy/OrderBy.test.tsx b/hotel-management/src/components/OrderBy/OrderBy.test.tsx new file mode 100644 index 0000000..ed46741 --- /dev/null +++ b/hotel-management/src/components/OrderBy/OrderBy.test.tsx @@ -0,0 +1,40 @@ +import { RenderResult, fireEvent, render } from '@testing-library/react'; + +// Components +import OrderBy from '.'; + +jest.mock('react-router-dom'); + +describe('Order', () => { + const options = [ + { + value: 'value1', + label: 'Option 1', + }, + { + value: 'value2', + label: 'Option 2', + }, + ]; + + let wrapper: RenderResult | null = null; + + beforeEach(() => { + wrapper = render(); + }); + + test('Should render correctly', () => { + expect(wrapper).toMatchSnapshot(); + }); + + test('handleClick should work correctly', async () => { + const mockSearchParams = jest.fn(); + // useSearchParams.search = mockSearchParams; + // setSearchParams = jest.fn(); + + const nameButton = wrapper!.getByText('Name'); + fireEvent.click(nameButton); + + expect(mockSearchParams).toHaveBeenCalled(); + }); +}); diff --git a/hotel-management/src/components/OrderBy/__snapshots__/Order.test.tsx.snap b/hotel-management/src/components/OrderBy/__snapshots__/Order.test.tsx.snap index 915c1ab..a72f02d 100644 --- a/hotel-management/src/components/OrderBy/__snapshots__/Order.test.tsx.snap +++ b/hotel-management/src/components/OrderBy/__snapshots__/Order.test.tsx.snap @@ -1,24 +1,94 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`Order 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/components/Search/Search.test.tsx b/hotel-management/src/components/Search/Search.test.tsx index 5068e51..e19db87 100644 --- a/hotel-management/src/components/Search/Search.test.tsx +++ b/hotel-management/src/components/Search/Search.test.tsx @@ -1,18 +1,32 @@ -import renderer from 'react-test-renderer'; import { BrowserRouter } from 'react-router-dom'; // Components import Search from '.'; +import { RenderResult, fireEvent, render } from '@testing-library/react'; + +jest.useFakeTimers(); describe('Search', () => { const placeHolder = 'Search placeholder...'; - const wrapper = renderer.create( - - - - ); + let wrapper: RenderResult | null = null; + + beforeEach(() => { + wrapper = render( + + + + ); + }) test('Should render correctly', () => { expect(wrapper).toMatchSnapshot(); }); + + test('Should add query to url after 1 second', async () => { + const searchField = await wrapper!.findByPlaceholderText('Search placeholder...'); + + fireEvent.input(searchField, 'test'); + + jest.advanceTimersByTime(500); + }); }); diff --git a/hotel-management/src/components/Search/__snapshots__/Search.test.tsx.snap b/hotel-management/src/components/Search/__snapshots__/Search.test.tsx.snap index a0f238e..366b3b6 100644 --- a/hotel-management/src/components/Search/__snapshots__/Search.test.tsx.snap +++ b/hotel-management/src/components/Search/__snapshots__/Search.test.tsx.snap @@ -1,9 +1,72 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`Search 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/constants/commons.ts b/hotel-management/src/constants/commons.ts index 9e62af2..1d99359 100644 --- a/hotel-management/src/constants/commons.ts +++ b/hotel-management/src/constants/commons.ts @@ -51,7 +51,7 @@ const FORM = { }; const REGEX = { - PHONE: /(84|0[3|5|7|8|9])+([0-9]{8})\b/g, + PHONE: /(0[3|5|7|8|9])+([0-9]{8})\b/g, NAME: /^[a-zA-Z]{2,}(?: [a-zA-Z]+){0,2}$/gm, NUMBER: /^[0-9]*$/, }; diff --git a/hotel-management/src/helpers/__tests__/validators.test.ts b/hotel-management/src/helpers/__tests__/validators.test.ts index f2e31d6..61d6b94 100644 --- a/hotel-management/src/helpers/__tests__/validators.test.ts +++ b/hotel-management/src/helpers/__tests__/validators.test.ts @@ -9,43 +9,82 @@ import { } from '@helper/validators'; describe('isValidPhone', () => { - test('Should is valid phone', () => { - const result = isValidRegex(new RegExp(REGEX.PHONE), '0327212321'); + const isValidPhone = (phone: string) => + isValidRegex(new RegExp(REGEX.PHONE), phone); - expect(result).toEqual(true); + test('Should is valid phone', () => { + const case_1 = isValidPhone('0321123212'); + const case_2 = isValidPhone('0533245422'); + const case_3 = isValidPhone('0723342321'); + const case_4 = isValidPhone('0823654321'); + const case_5 = isValidPhone('0943212343'); + + expect(case_1).toBeTruthy(); + expect(case_2).toBeTruthy(); + expect(case_3).toBeTruthy(); + expect(case_4).toBeTruthy(); + expect(case_5).toBeTruthy(); }); test('Should is not valid phone', () => { - const result = isValidRegex(new RegExp(REGEX.PHONE), '0327212321Abc'); + const case_1 = isValidPhone('0327212321Abc'); + const case_2 = isValidPhone('0021432123'); + const case_3 = isValidPhone('@325542321'); + const case_4 = isValidPhone('A321443232'); + const case_5 = isValidPhone('ValidPhone'); + const case_6 = isValidPhone('000'); - expect(result).toEqual(false); + expect(case_1).toBeFalsy(); + expect(case_2).toBeFalsy(); + expect(case_3).toBeFalsy(); + expect(case_4).toBeFalsy(); + expect(case_5).toBeFalsy(); + expect(case_6).toBeFalsy(); }); }); describe('isValidDiscount', () => { test('Should is valid discount', () => { - const result = isValidDiscount(23); + const case_1 = isValidDiscount(99.99); + const case_2 = isValidDiscount(50); + const case_3 = isValidDiscount(0); + const case_4 = isValidDiscount(100); - expect(result).toEqual(true); + expect(case_1).toBeTruthy(); + expect(case_2).toBeTruthy(); + expect(case_3).toBeTruthy(); + expect(case_4).toBeTruthy(); }); test('Should is not valid discount', () => { - const result = isValidDiscount(101); + const case_1 = isValidDiscount(-1); + const case_2 = isValidDiscount(-0.99); + const case_3 = isValidDiscount(101); - expect(result).toEqual(false); + expect(case_1).toBeFalsy(); + expect(case_2).toBeFalsy(); + expect(case_3).toBeFalsy(); }); }); describe('Should is valid string', () => { test('Is valid string', () => { - const result = isValidString('Phan Huu Loi'); + const case_1 = isValidString('Phan Huu Loi'); + const case_2 = isValidString('Nezumi'); - expect(result).toEqual(true); + expect(case_1).toBeTruthy(); + expect(case_2).toBeTruthy(); }); test('Should is not valid string', () => { - const result = isValidString('Phan'); + const case_1 = isValidString('Le'); + const case_2 = isValidString('1tét'); + const case_3 = isValidString('@!'); + const case_4 = isValidString(' '); - expect(result).toEqual(false); + expect(case_1).toBeFalsy(); + expect(case_2).toBeFalsy(); + expect(case_3).toBeFalsy(); + expect(case_4).toBeFalsy(); }); }); diff --git a/hotel-management/src/services/__tests__/roomService.test.ts b/hotel-management/src/services/__tests__/roomService.test.ts index 6519042..a348130 100644 --- a/hotel-management/src/services/__tests__/roomService.test.ts +++ b/hotel-management/src/services/__tests__/roomService.test.ts @@ -10,6 +10,7 @@ import { // Types import { IRoom } from '@type/rooms'; +import supabase from '@service/supabaseService'; const mockGetAllRooms = jest.fn(getAllRooms); const mockUpdateRoom = jest.fn(updateRoom); @@ -44,6 +45,18 @@ describe('Rooms service', () => { await waitFor(() => expect(result.length).toEqual(2)); }); + test('Can not fetch data', async () => { + jest.spyOn(supabase.from('rooms').select(), 'single').mockRejectedValue(() => { + return { + error: 'Error' + } + }) + + await mockGetAllRooms('name', 'asc', ''); + + expect(mockGetAllRooms).toThrow(); + }); + test('Should create room correctly', async () => { mockCreateRoom.mockResolvedValue(sampleData);