README.md85 lines · main
1# UI Testing Notes
2
3## Rules
4
5- All tests should be run consistently (avoid situations whereby tests fails "sometimes")
6
7- Group tests in folders based on the feature they are testing. Avoid file/folder based folder names since those can change and we will forget to update the tests.
8
9Examples: /logs /reports /projects /database-settings /auth
10
11## Custom Render and Custom Render Hook
12
13`customRender` and `customRenderHook` are wrappers around `render` and `renderHook` that add some necessary providers like `QueryClientProvider`, `TooltipProvider` and `NuqsTestingAdapter`.
14
15Generally use those instead of the default `render` and `renderHook` functions.
16
17```ts
18import { customRender, customRenderHook } from 'tests/lib/custom-render'
19
20customRender(<MyComponent />)
21customRenderHook(() => useMyHook())
22```
23
24## Mocking API Requests
25
26To mock API requests, we use the `msw` library.
27
28Global mocks can be found in `tests/lib/msw-global-api-mocks.ts`.
29
30To mock an endpoint you can use the `addAPIMock` function. Make sure to add the mock in the `beforeEach` hook. It won't work with `beforeAll` if you have many tests.
31
32```ts
33beforeEach(() => {
34 addAPIMock({
35 method: 'get',
36 path: '/api/my-endpoint',
37 response: {
38 data: { foo: 'bar' },
39 },
40 })
41})
42```
43
44### API Mocking Tips:
45
46- Keep mocks in the same folder as the tests that use them
47- Add a test to verify the mock is working
48
49This will make debugging and updating the mocks easier.
50
51```ts
52test('mock is working', async () => {
53 const response = await fetch('/api/my-endpoint')
54 expect(response.json()).resolves.toEqual({ data: { foo: 'bar' } })
55})
56```
57
58## Mocking Nuqs URL Parameters
59
60To render a component that uses Nuqs with some predefined query parameters, you can use `customRender` with the `nuqs` prop.
61
62```ts
63
64customRender(<MyComponent />, {
65 nuqs: {
66 searchParams: {
67 search: 'hello world',
68 },
69 },
70})
71```
72
73## `<Popover>` vs `<Dropdown>`
74
75When simulating clicks on these components, do the following:
76
77```js
78// for Popovers
79import userEvent from '@testing-library/user-event'
80await userEvent.click('Hello world')
81
82// for Dropdowns
83import clickDropdown from 'tests/helpers'
84clickDropdown('Hello world')
85```
Preview

UI Testing Notes

Rules

  • All tests should be run consistently (avoid situations whereby tests fails "sometimes")

  • Group tests in folders based on the feature they are testing. Avoid file/folder based folder names since those can change and we will forget to update the tests.

Examples: /logs /reports /projects /database-settings /auth

Custom Render and Custom Render Hook

customRender and customRenderHook are wrappers around render and renderHook that add some necessary providers like QueryClientProvider, TooltipProvider and NuqsTestingAdapter.

Generally use those instead of the default render and renderHook functions.

import { customRender, customRenderHook } from 'tests/lib/custom-render'

customRender(<MyComponent />)
customRenderHook(() => useMyHook())

Mocking API Requests

To mock API requests, we use the msw library.

Global mocks can be found in tests/lib/msw-global-api-mocks.ts.

To mock an endpoint you can use the addAPIMock function. Make sure to add the mock in the beforeEach hook. It won't work with beforeAll if you have many tests.

beforeEach(() => {
  addAPIMock({
    method: 'get',
    path: '/api/my-endpoint',
    response: {
      data: { foo: 'bar' },
    },
  })
})

API Mocking Tips:

  • Keep mocks in the same folder as the tests that use them
  • Add a test to verify the mock is working

This will make debugging and updating the mocks easier.

test('mock is working', async () => {
  const response = await fetch('/api/my-endpoint')
  expect(response.json()).resolves.toEqual({ data: { foo: 'bar' } })
})

Mocking Nuqs URL Parameters

To render a component that uses Nuqs with some predefined query parameters, you can use customRender with the nuqs prop.


customRender(<MyComponent />, {
  nuqs: {
    searchParams: {
      search: 'hello world',
    },
  },
})

<Popover> vs <Dropdown>

When simulating clicks on these components, do the following:

// for Popovers
import userEvent from '@testing-library/user-event'
await userEvent.click('Hello world')

// for Dropdowns
import clickDropdown from 'tests/helpers'
clickDropdown('Hello world')