|  | 
|  | 1 | +--- | 
|  | 2 | +id: api | 
|  | 3 | +title: API | 
|  | 4 | +sidebar_label: API | 
|  | 5 | +--- | 
|  | 6 | + | 
|  | 7 | +- [`@testing-library/dom`](#testing-library-dom) | 
|  | 8 | +- [`render`](#render) | 
|  | 9 | +- [`cleanup`](#cleanup) | 
|  | 10 | +- [`act`](#act) | 
|  | 11 | +- [`fireEvent`](#fireevent) | 
|  | 12 | + | 
|  | 13 | +--- | 
|  | 14 | + | 
|  | 15 | +## `@testing-library/dom` | 
|  | 16 | + | 
|  | 17 | +This library re-exports everything from the DOM Testing Library | 
|  | 18 | +(`@testing-library/dom`). See the | 
|  | 19 | +[documentation](../dom-testing-library/api-queries.md) to see what goodies you | 
|  | 20 | +can use. | 
|  | 21 | + | 
|  | 22 | +## `render` | 
|  | 23 | + | 
|  | 24 | +```jsx | 
|  | 25 | +import { render } from '@testing-library/preact' | 
|  | 26 | + | 
|  | 27 | +const { results } = render(<YourComponent />, { options }) | 
|  | 28 | +``` | 
|  | 29 | + | 
|  | 30 | +### Options | 
|  | 31 | + | 
|  | 32 | +| Option | Description | Default | | 
|  | 33 | +| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- | | 
|  | 34 | +| `container` | The HTML element the component is mounted to. | baseElement | | 
|  | 35 | +| `baseElement` | The root HTML element to which the container is appended to. | document.body | | 
|  | 36 | +| `queries` | Queries to bind to the baseElement. See [getQueriesForElement](../dom-testing-library/api-helpers#within-and-getqueriesforelement-apis). | null | | 
|  | 37 | +| `hydrate` | Used when the component has already been mounted and requires a rerender. Not needed for most people. The rerender function passed back to you does this already. | false | | 
|  | 38 | +| `wrapper` | A parent component to wrap YourComponent. | null | | 
|  | 39 | + | 
|  | 40 | +### Results | 
|  | 41 | + | 
|  | 42 | +| Result | Description | | 
|  | 43 | +| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | 
|  | 44 | +| `container` | The HTML element the component is mounted to. | | 
|  | 45 | +| `baseElement` | The root HTML element to which the container is appended to. | | 
|  | 46 | +| `debug` | Logs the baseElement using [prettyDom](https://testing-library.com/docs/dom-testing-library/api-helpers#prettydom). | | 
|  | 47 | +| `unmount` | Unmounts the component from the container. | | 
|  | 48 | +| `rerender` | Calls render again passing in the original arguments and sets hydrate to true. | | 
|  | 49 | +| `asFragment` | Returns the innerHTML of the container. | | 
|  | 50 | +| `...queries` | Returns all [query functions](https://testing-library.com/docs/dom-testing-library/api-queries) to be used on the baseElement. If you pass in `query` arguments than this will be those, otherwise all. | | 
|  | 51 | + | 
|  | 52 | +## `cleanup` | 
|  | 53 | + | 
|  | 54 | +Unmounts the component from the container and destroys the container. | 
|  | 55 | + | 
|  | 56 | +📝 When you import anything from the library, this automatically runs after each | 
|  | 57 | +test. If you'd like to disable this then set `process.env.PTL_SKIP_AUTO_CLEANUP` | 
|  | 58 | +to true when running your tests. | 
|  | 59 | + | 
|  | 60 | +```jsx | 
|  | 61 | +import { render, cleanup } from '@testing-library/preact' | 
|  | 62 | + | 
|  | 63 | +afterEach(() => { | 
|  | 64 | + cleanup() | 
|  | 65 | +}) // Default on import: runs it after each test. | 
|  | 66 | + | 
|  | 67 | +render(<YourComponent />) | 
|  | 68 | + | 
|  | 69 | +cleanup() // Or like this for more control. | 
|  | 70 | +``` | 
|  | 71 | + | 
|  | 72 | +## `act` | 
|  | 73 | + | 
|  | 74 | +Just a convenience export that points to preact/test-utils/act. All renders and | 
|  | 75 | +events being fired are wrapped in `act`, so you don't really need this. It's | 
|  | 76 | +responsible for flushing all effects and rerenders after invoking it. | 
|  | 77 | + | 
|  | 78 | +📝 If you'd love to learn more, checkout | 
|  | 79 | +[this explanation](https://github.com/threepointone/react-act-examples/blob/master/sync.md). | 
|  | 80 | +Even thought it's for React, it gives you an idea of why it's needed. | 
|  | 81 | + | 
|  | 82 | +## `fireEvent` | 
|  | 83 | + | 
|  | 84 | +Passes it to the @testing-library/dom | 
|  | 85 | +[fireEvent](../dom-testing-library/api-events). It's also wrapped in `act` so | 
|  | 86 | +you don't need to worry about doing it. | 
|  | 87 | + | 
|  | 88 | +📝 Keep in mind mainly when using `h / Preact.createElement` that React uses a | 
|  | 89 | +Synthetic event system, and Preact uses the browser's native `addEventListener` | 
|  | 90 | +for event handling. This means events like `onChange` and `onDoubleClick` in | 
|  | 91 | +React, are `onInput` and `onDblClick` in Preact. The double click example will | 
|  | 92 | +give you an idea of how to test using those functions. | 
|  | 93 | + | 
|  | 94 | +### Example 1 | 
|  | 95 | + | 
|  | 96 | +```jsx | 
|  | 97 | +const cb = jest.fn(); | 
|  | 98 | + | 
|  | 99 | +function Counter() { | 
|  | 100 | + useEffect(cb); | 
|  | 101 | + | 
|  | 102 | + const [count, setCount] = useState(0); | 
|  | 103 | + | 
|  | 104 | + return <button onClick={() => setCount(count + 1)}>{count}</button>; | 
|  | 105 | +} | 
|  | 106 | + | 
|  | 107 | +const { container: { firstChild: buttonNode }, } = render(<Counter />); | 
|  | 108 | + | 
|  | 109 | +// Clear the first call to useEffect that the initial render triggers. | 
|  | 110 | +cb.mockClear(); | 
|  | 111 | + | 
|  | 112 | +// Fire event Option 1. | 
|  | 113 | +fireEvent.click(buttonNode); | 
|  | 114 | + | 
|  | 115 | +// Fire event Option 2. | 
|  | 116 | +fireEvent( | 
|  | 117 | +button, | 
|  | 118 | +new Event('MouseEvent', { | 
|  | 119 | + bubbles: true, | 
|  | 120 | + cancelable: true, | 
|  | 121 | + button: 0, | 
|  | 122 | +}); | 
|  | 123 | + | 
|  | 124 | +expect(buttonNode).toHaveTextContent('1'); | 
|  | 125 | +expect(cb).toHaveBeenCalledTimes(1); | 
|  | 126 | +``` | 
|  | 127 | +
 | 
|  | 128 | +### Example 2 | 
|  | 129 | +
 | 
|  | 130 | +```jsx | 
|  | 131 | +const handler = jest.fn() | 
|  | 132 | + | 
|  | 133 | +const { | 
|  | 134 | + container: { firstChild: input }, | 
|  | 135 | +} = render(<input type="text" onInput={handler} />) | 
|  | 136 | + | 
|  | 137 | +fireEvent.input(input, { target: { value: 'a' } }) | 
|  | 138 | + | 
|  | 139 | +expect(handler).toHaveBeenCalledTimes(1) | 
|  | 140 | +``` | 
|  | 141 | +
 | 
|  | 142 | +### Example 3 | 
|  | 143 | +
 | 
|  | 144 | +```jsx | 
|  | 145 | +const ref = createRef() | 
|  | 146 | +const spy = jest.fn() | 
|  | 147 | + | 
|  | 148 | +render( | 
|  | 149 | + h(elementType, { | 
|  | 150 | + onDblClick: spy, | 
|  | 151 | + ref, | 
|  | 152 | + }) | 
|  | 153 | +) | 
|  | 154 | + | 
|  | 155 | +fireEvent['onDblClick'](ref.current) | 
|  | 156 | + | 
|  | 157 | +expect(spy).toHaveBeenCalledTimes(1) | 
|  | 158 | +``` | 
0 commit comments