forked from cloudscape-design/components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize-observer.ts
41 lines (33 loc) · 1.19 KB
/
resize-observer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* eslint-env es6 */
/* eslint-disable header/header */
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
import { ResizeObserver, ResizeObserverEntry } from '@juggle/resize-observer';
const callbackField = Symbol();
const mockObserve = jest.fn(function (this: MockResizeObserver, el: HTMLElement) {
// JSDOM does not support CSS. This mock allows to set element sizes via inline styles
// and they will be passed into the handlers
const { width, height } = el.style;
const size = { inlineSize: parseInt(width) || 0, blockSize: parseInt(height) || 0 };
const cb = this[callbackField];
cb([{ borderBoxSize: [size], contentBoxSize: [size], target: el }], this);
});
const mockUnobserve = jest.fn();
const mockDisconnect = jest.fn();
class MockResizeObserver implements ResizeObserver {
[callbackField]: (...args: any[]) => void;
constructor(cb: (...args: any[]) => void) {
this[callbackField] = cb;
}
get observe() {
return mockObserve;
}
get unobserve() {
return mockUnobserve;
}
get disconnect() {
return mockDisconnect;
}
}
export { ResizeObserverEntry };
export { MockResizeObserver as ResizeObserver };