Update test case for sortBy, select, orderBy component

This commit is contained in:
2023-11-28 17:49:22 +07:00
parent ddf96492c9
commit 7bb83f74c8
20 changed files with 1412 additions and 413 deletions
File diff suppressed because it is too large Load Diff
@@ -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(<OrderBy options={options} />);
wrapper = render(
<MemoryRouter>
<OrderBy options={options} />
</MemoryRouter>
);
});
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');
});
});
@@ -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(
<BrowserRouter>
<Search setPlaceHolder={placeHolder} />
</BrowserRouter>
);
})
// beforeEach(() => {
// wrapper = render(
// <BrowserRouter>
// <Search setPlaceHolder={placeHolder} />
// </BrowserRouter>
// );
// });
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(
<BrowserRouter>
<Search setPlaceHolder={'Search placeholder...'} />
</BrowserRouter>
);
});
fireEvent.input(searchField, 'test');
const searchField = await wrapper!.findByPlaceholderText(
'Search placeholder...'
);
jest.advanceTimersByTime(500);
act(() => {
fireEvent.change(searchField, { target: { value: 'abc' } });
});
// jest.advanceTimersByTime(1000);
});
});
@@ -8,11 +8,13 @@ exports[`Select Should render correctly 1`] = `
value="Temp value"
>
<option
data-testid="select-option"
value="Value 1"
>
Option 1
</option>
<option
data-testid="select-option"
value="Value 2"
>
Option 2
@@ -43,7 +43,7 @@ const Select = ({
: { onChange: onChange })}
>
{options.map((option) => (
<option value={option.value} key={option.value}>
<option value={option.value} key={option.value} data-testid='select-option'>
{option.label}
</option>
))}
@@ -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(
<BrowserRouter>
<SortBy options={options} />
</BrowserRouter>
);
let wrapper: RenderResult | null = null;
beforeEach(() => {
wrapper = render(
<BrowserRouter>
<SortBy options={options} />
</BrowserRouter>
);
});
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');
});
});
@@ -1,21 +1,98 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`SortBy Should render correctly 1`] = `
<select
aria-label="Sort"
className="sc-bdnyFh dqXTRP"
onChange={[Function]}
value=""
>
<option
value="Value 1"
>
Option 1
</option>
<option
value="Value 2"
>
Option 2
</option>
</select>
{
"asFragment": [Function],
"baseElement": <body>
<div>
<select
aria-label="Sort"
class="sc-bdnyFh dqXTRP"
>
<option
data-testid="select-option"
value="Value1"
>
Option 1
</option>
<option
data-testid="select-option"
value="Value2"
>
Option 2
</option>
</select>
</div>
</body>,
"container": <div>
<select
aria-label="Sort"
class="sc-bdnyFh dqXTRP"
>
<option
data-testid="select-option"
value="Value1"
>
Option 1
</option>
<option
data-testid="select-option"
value="Value2"
>
Option 2
</option>
</select>
</div>,
"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],
}
`;
@@ -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(<App />);
});
});
});
@@ -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(
<QueryClientProvider client={queryClient}>
<BrowserRouter>
<RoomForm />
</BrowserRouter>
</QueryClientProvider>
);
test('Should render correctly', () => {
expect(wrapper).toMatchSnapshot();
});
});
@@ -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(
<QueryClientProvider client={queryClient}>
<RoomRow room={tempRoom} key={tempRoom.id} />
</QueryClientProvider>
);
test('Should render correctly', () => {
expect(wrapper).toMatchSnapshot();
});
});
@@ -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(
<BrowserRouter>
<QueryClientProvider client={queryClient}>
<RoomTable />
</QueryClientProvider>
</BrowserRouter>
);
test('Should render correctly', () => {
expect(wrapper).toMatchSnapshot();
});
});
@@ -1,67 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`RoomForm Should render correctly 1`] = `
<form
className="sc-dlniIP bCPNqw"
onSubmit={[Function]}
>
<div
className="sc-eCAqax eozPUK"
>
<label
className="sc-jSFipO cMYxin"
>
Name
</label>
<div>
<input
className="sc-gtsqUy fGZPSU"
id="name"
name="name"
onBlur={[Function]}
onChange={[Function]}
type="text"
/>
</div>
</div>
<div
className="sc-eCAqax eozPUK"
>
<label
className="sc-jSFipO cMYxin"
>
Price
</label>
<div>
<input
className="sc-gtsqUy fGZPSU"
id="price"
name="price"
onBlur={[Function]}
onChange={[Function]}
type="text"
/>
</div>
</div>
<div
className="sc-hKFymg dteuGK"
>
<button
className="sc-bdnyFh sc-iCoHzw hHmoPp jAHgeA"
disabled={false}
name="submit"
type="submit"
variations="primary"
>
Add
</button>
<button
className="sc-bdnyFh sc-iCoHzw bjdOKm jAHgeA"
type="button"
variations="secondary"
>
Close
</button>
</div>
</form>
`;
@@ -1,53 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`RoomRow Should render correctly 1`] = `
<div
className="sc-crzpnZ sc-bqGGcD gIMFtm jqrNRF"
>
<div>
1
</div>
<div>
Temp Room
</div>
<div>
$250.00
</div>
<div>
Unavailable
</div>
<div>
<div
className="sc-fnVZQs CCxtf"
>
<button
aria-label="Menu item 1"
className="sc-bkbjWr fCztjK"
onClick={[Function]}
>
<svg
aria-hidden="true"
fill="currentColor"
height="1em"
stroke="currentColor"
strokeWidth="0"
style={
{
"color": undefined,
}
}
viewBox="0 0 24 24"
width="1em"
xmlns="http://www.w3.org/2000/svg"
>
<path
clipRule="evenodd"
d="M10.5 6a1.5 1.5 0 113 0 1.5 1.5 0 01-3 0zm0 6a1.5 1.5 0 113 0 1.5 1.5 0 01-3 0zm0 6a1.5 1.5 0 113 0 1.5 1.5 0 01-3 0z"
fillRule="evenodd"
/>
</svg>
</button>
</div>
</div>
</div>
`;
@@ -1,63 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`RoomTable Should render correctly 1`] = `
<div
className="sc-iJCSeZ crZyCc"
type="vertical"
>
<div
className="sc-lmgPJu cuMSiO"
>
<div
className="sc-crzpnZ iahNiP"
>
<button
active={true}
className="sc-dItHI guVVWw"
disabled={true}
onClick={[Function]}
>
Ascending
</button>
<button
active={false}
className="sc-dItHI fIRqtD"
disabled={false}
onClick={[Function]}
>
Descending
</button>
</div>
<select
aria-label="Sort"
className="sc-iqAbyq czAJZn"
onChange={[Function]}
value=""
>
<option
value="id"
>
Sort by id
</option>
<option
value="name"
>
Sort by name
</option>
<option
value="price"
>
Sort by price
</option>
</select>
<input
className="sc-kEqXeH jMVPeG"
onChange={[Function]}
placeholder="Search by name..."
/>
</div>
<div
className="sc-giAruI hMNXks"
/>
</div>
`;
@@ -1,84 +0,0 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Room Should render correctly 1`] = `
<main
className="sc-iwaiBT gPCYSC"
>
<div
className="sc-iJCSeZ jDHUlN"
type="horizontal"
>
<h2
className="sc-cxNGUP ffcGOQ"
>
List Room
</h2>
<button
className="sc-bqGGcD cVmQmy"
onClick={[Function]}
variations="primary"
>
Add room
</button>
</div>
<div
className="sc-iJCSeZ crZyCc"
type="vertical"
>
<div
className="sc-lmgPJu cuMSiO"
>
<div
className="sc-crzpnZ iahNiP"
>
<button
active={true}
className="sc-dItHI guVVWw"
disabled={true}
onClick={[Function]}
>
Ascending
</button>
<button
active={false}
className="sc-dItHI fIRqtD"
disabled={false}
onClick={[Function]}
>
Descending
</button>
</div>
<select
aria-label="Sort"
className="sc-iqAbyq czAJZn"
onChange={[Function]}
value=""
>
<option
value="id"
>
Sort by id
</option>
<option
value="name"
>
Sort by name
</option>
<option
value="price"
>
Sort by price
</option>
</select>
<input
className="sc-kEqXeH jMVPeG"
onChange={[Function]}
placeholder="Search by name..."
/>
</div>
<div
className="sc-giAruI hMNXks"
/>
</div>
</main>
`;
@@ -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(
<QueryClientProvider client={queryClient}>
<BrowserRouter>
<Room />
</BrowserRouter>
</QueryClientProvider>
);
test('Should render correctly', () => {
expect(wrapper).toMatchSnapshot();
});
});
@@ -35,16 +35,19 @@ exports[`UserTable Should render correctly 1`] = `
value=""
>
<option
data-testid="select-option"
value="id"
>
Sort by id
</option>
<option
data-testid="select-option"
value="name"
>
Sort by name
</option>
<option
data-testid="select-option"
value="price"
>
Sort by price
@@ -55,16 +55,19 @@ exports[`User Should render correctly 1`] = `
value=""
>
<option
data-testid="select-option"
value="id"
>
Sort by id
</option>
<option
data-testid="select-option"
value="name"
>
Sort by name
</option>
<option
data-testid="select-option"
value="phone"
>
Sort by phone