Skip to content

Commit 38aad3c

Browse files
committed
Make only one size property required
1 parent 691bee0 commit 38aad3c

File tree

5 files changed

+99
-28
lines changed

5 files changed

+99
-28
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ Triggers all IntersectionObservers for each of the observed nodes
161161

162162
## Mock ResizeObserver
163163

164-
Mocks `ResizeObserver` class. Resize callbacks are triggered manually using `resize` method returned by the mock. Elements' size must be not 0 for the element to appear in the list of callback entries (you can mock the size using [`mockElementSize`](#mockelementsizeelement-htmlelement-size-size) or `mockElementBoundingClientRect`)
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`)
165165

166166
Example, using `React Testing Library`:
167167

@@ -277,6 +277,11 @@ mockElementSize(myDiv, {
277277
// both contentBoxSize and borderBoxSize accept plain objects instead of arrays
278278
contentBoxSize: { inlineSize: 400, blockSize: 200 },
279279
});
280+
281+
mockElementSize(myOtherDiv, {
282+
// only one dimension is required, the other one will be assumed to be 0
283+
borderBoxSize: { inlineSize: 200 },
284+
});
280285
```
281286

282287
#### .getObservers(element?: HTMLElement)

examples/src/tests/resize-observer.test.tsx

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,74 @@ describe('mockResizeObserver', () => {
271271
]);
272272
});
273273

274-
test('I can remock the size', () => {
274+
it('should be possible to omit either inlineSize or blockSize', async () => {
275+
const callback = jest.fn();
276+
const { result } = renderHook(() =>
277+
useResizeObserver(useMemo(() => ({ callback }), []))
278+
);
279+
280+
if (!result.current) {
281+
return;
282+
}
283+
284+
const element = document.createElement('div');
285+
286+
result.current.observe(element);
287+
288+
mockElementSize(element, { contentBoxSize: { inlineSize: 400 } });
289+
290+
act(() => {
291+
resize();
292+
});
293+
294+
expect(callback).toHaveBeenCalledTimes(1);
295+
expect(callback).toHaveBeenCalledWith([
296+
{
297+
target: element,
298+
borderBoxSize: [{ inlineSize: 400, blockSize: 0 }],
299+
contentBoxSize: [{ inlineSize: 400, blockSize: 0 }],
300+
contentRect: expect.objectContaining({
301+
x: 0,
302+
y: 0,
303+
width: 400,
304+
height: 0,
305+
top: 0,
306+
right: 400,
307+
bottom: 0,
308+
left: 0,
309+
}),
310+
devicePixelContentBoxSize: [{ inlineSize: 400, blockSize: 0 }],
311+
},
312+
]);
313+
314+
mockElementSize(element, { contentBoxSize: { blockSize: 200 } });
315+
316+
act(() => {
317+
resize(element);
318+
});
319+
320+
expect(callback).toHaveBeenCalledTimes(2);
321+
expect(callback).toHaveBeenCalledWith([
322+
{
323+
target: element,
324+
borderBoxSize: [{ inlineSize: 0, blockSize: 200 }],
325+
contentBoxSize: [{ inlineSize: 0, blockSize: 200 }],
326+
contentRect: expect.objectContaining({
327+
x: 0,
328+
y: 0,
329+
width: 0,
330+
height: 200,
331+
top: 0,
332+
right: 0,
333+
bottom: 200,
334+
left: 0,
335+
}),
336+
devicePixelContentBoxSize: [{ inlineSize: 0, blockSize: 200 }],
337+
},
338+
]);
339+
});
340+
341+
test('Remocking the size', () => {
275342
const callback = jest.fn();
276343
const { result } = renderHook(() =>
277344
useResizeObserver(useMemo(() => ({ callback }), []))

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsdom-testing-mocks",
3-
"version": "1.6.0-beta.0",
3+
"version": "1.6.0-beta.1",
44
"author": "Ivan Galiatin",
55
"license": "MIT",
66
"description": "A set of tools for emulating browser behavior in jsdom environment",

src/mocks/resize-observer.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,18 @@
1+
import { RequireAtLeastOne } from 'type-fest';
2+
13
type Sizes = {
24
borderBoxSize: ResizeObserverSize[];
35
contentBoxSize: ResizeObserverSize[];
46
contentRect: DOMRectReadOnly;
57
};
68

7-
type BothSizesDefined = {
8-
borderBoxSize: ResizeObserverSize[] | ResizeObserverSize;
9-
contentBoxSize: ResizeObserverSize[] | ResizeObserverSize;
10-
};
11-
type ContentBoxSizeDefined = {
12-
borderBoxSize?: ResizeObserverSize[] | ResizeObserverSize;
13-
contentBoxSize: ResizeObserverSize[] | ResizeObserverSize;
9+
type ResizeObserverSizeInput = RequireAtLeastOne<ResizeObserverSize>;
10+
type SizeInput = {
11+
borderBoxSize: ResizeObserverSizeInput[] | ResizeObserverSizeInput;
12+
contentBoxSize: ResizeObserverSizeInput[] | ResizeObserverSizeInput;
1413
};
15-
type BorderBoxSizeDefined = {
16-
borderBoxSize: ResizeObserverSize[] | ResizeObserverSize;
17-
contentBoxSize?: ResizeObserverSize[] | ResizeObserverSize;
18-
};
19-
// type BothSizesDefined = Pick<Sizes, 'contentBoxSize' | 'borderBoxSize'>;
20-
// type ContentBoxSizeDefined = Pick<Sizes, 'contentBoxSize'> &
21-
// Partial<Pick<Sizes, 'borderBoxSize'>>;
22-
// type BorderBoxSizeDefined = Pick<Sizes, 'borderBoxSize'> &
23-
// Partial<Pick<Sizes, 'contentBoxSize'>>;
2414

25-
type Size = BothSizesDefined | ContentBoxSizeDefined | BorderBoxSizeDefined;
15+
type Size = RequireAtLeastOne<SizeInput>;
2616

2717
type State = {
2818
observers: MockedResizeObserver[];
@@ -42,6 +32,15 @@ function resetState() {
4232
state.elementSizes = new Map();
4333
}
4434

35+
function defineResizeObserverSize(
36+
input: ResizeObserverSizeInput
37+
): ResizeObserverSize {
38+
return {
39+
blockSize: input.blockSize ?? 0,
40+
inlineSize: input.inlineSize ?? 0,
41+
};
42+
}
43+
4544
class MockedResizeObserver implements ResizeObserver {
4645
callback: ResizeObserverCallback;
4746
observationTargets = new Set<HTMLElement>();
@@ -180,15 +179,15 @@ function mockResizeObserver() {
180179
size.contentBoxSize = [size.contentBoxSize];
181180
}
182181

183-
contentBoxSize = size.contentBoxSize;
184-
borderBoxSize = size.contentBoxSize;
182+
contentBoxSize = size.contentBoxSize.map(defineResizeObserverSize);
183+
borderBoxSize = contentBoxSize;
185184
} else if (size.borderBoxSize && !size.contentBoxSize) {
186185
if (!Array.isArray(size.borderBoxSize)) {
187186
size.borderBoxSize = [size.borderBoxSize];
188187
}
189188

190-
contentBoxSize = size.borderBoxSize;
191-
borderBoxSize = size.borderBoxSize;
189+
contentBoxSize = size.borderBoxSize.map(defineResizeObserverSize);
190+
borderBoxSize = contentBoxSize;
192191
} else if (size.borderBoxSize && size.contentBoxSize) {
193192
if (!Array.isArray(size.borderBoxSize)) {
194193
size.borderBoxSize = [size.borderBoxSize];
@@ -198,8 +197,8 @@ function mockResizeObserver() {
198197
size.contentBoxSize = [size.contentBoxSize];
199198
}
200199

201-
contentBoxSize = size.contentBoxSize as ResizeObserverSize[];
202-
borderBoxSize = size.borderBoxSize as ResizeObserverSize[];
200+
contentBoxSize = size.contentBoxSize.map(defineResizeObserverSize);
201+
borderBoxSize = size.borderBoxSize.map(defineResizeObserverSize);
203202

204203
if (borderBoxSize.length !== contentBoxSize.length) {
205204
throw new Error(

0 commit comments

Comments
 (0)