You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Mocks `matchMedia`, allows testing of component's behavior depending on the viewport description (supports all of the [Media Features](http://www.w3.org/TR/css3-mediaqueries/#media1)). `mockViewport` must be called before rendering the component
@@ -154,17 +161,12 @@ Triggers all IntersectionObservers for each of the observed nodes
154
161
155
162
## Mock ResizeObserver
156
163
157
-
Provides a way of triggering resize observer events. It's up to you to mock elements' sizes. If your component uses `contentRect` provided by the callback, you must mock element's `getBoundingClientRect` (for exemple using a helper function `mockElementBoundingClientRect` provided by the lib)
158
-
159
-
_Currently the mock doesn't take into account multi-column layouts, so `borderBoxSize` and `contentBoxSize` will contain only one full-sized item_
164
+
Mocks `ResizeObserver` class. Resize callbacks are triggered manually using `resize` method returned by the mock. Elements' size must not be 0 (at least on one axis) for the element to appear in the list of callback entries (you can mock the size using [`mockElementSize`](#mockelementsizeelement-htmlelement-size-size) or `mockElementBoundingClientRect`)
// on subsequent calls to `resize` you have to include it
225
+
// explicitly, unless observe has been called on it again
215
226
resizeObserver.resize(theDiv);
216
227
});
217
228
218
229
expect(screen.getByText('200 x 500')).toBeInTheDocument();
219
230
});
220
231
```
221
232
233
+
### Caveats
234
+
235
+
#### Triggering the callback on observe
236
+
237
+
Although the mock doesn't call the resize callback on its own, it keeps track of all the cases when it should be implicitly called (like when the element first begins being observed), and it auto-adds them to the list of elements when `resize` is called. You can disable this in `ResizeOptions`
238
+
239
+
#### Mocking element's size
240
+
241
+
The mock uses the size provided by `mockElementSize` if present and fallbacks to `getBoundingClientRect` (that you can mock using `mockElementBoundingClientRect`). The issue with `getBoundingClientRect` however is that in the real world the value it returns takes CSS Transforms into account, while the values returned in the observer callback don't. It doesn't really matter because it is you who mocks sizes, but for consistency it is preferred that you use `mockElementSize`
242
+
222
243
### API
223
244
224
-
`mockResizeObserver` returns an object, that has one method:
245
+
`mockResizeObserver` returns an object, that has several methods:
Triggers all resize observer callbacks for all observers that observe the passed elements. Some elements are implicitly resized by the Resize Observer itself, for example when they first attached using `observe`. This mock doesn't call the callback by itself. Instead, it adds them to the list of `entries` when the next `resize` is called (it happens only once per `observe` per element).
250
+
251
+
In this example the resize callback will be triggered with all observed elements from within `TestedComponent`:
252
+
253
+
```jsx
254
+
// a component that begins to observe elements in a useEffect
255
+
render(<TestedComponent />);
256
+
257
+
// ...don't forget to mock sizes
258
+
259
+
act(() => {
260
+
// triggers the `resize` callback with the elements for which `observe` has been called
261
+
resizeObserver.resize();
262
+
});
263
+
```
264
+
265
+
##### ResizeOptions.ignoreImplicit (`false` by default)
266
+
267
+
If `true`, do not include imlicit elements in the resize callback entries array
Mocks `element`'s size only for the ResizeObserver. `size` accepts 2 properties: `contentBoxSize` and `borderBoxSize` they're both similar to what you see in the ResizeObserver's callback entry. At least one of them must be present (if the other isn't it is set to be equal to the one present), and the other entry properties are derived from these two (and `window.devicePixelRatio`).
272
+
273
+
Example:
274
+
275
+
```jsx
276
+
mockElementSize(myDiv, {
277
+
// both contentBoxSize and borderBoxSize accept plain objects instead of arrays
0 commit comments