-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathFixedLayout.test.tsx
More file actions
125 lines (109 loc) · 3.32 KB
/
FixedLayout.test.tsx
File metadata and controls
125 lines (109 loc) · 3.32 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { act, type RefObject } from 'react';
import { render } from '@testing-library/react';
import { baselineComponent, withFakeTimers } from '../../testing/utils';
import { SplitCol } from '../SplitCol/SplitCol';
import { FixedLayout, type FixedLayoutProps } from './FixedLayout';
import styles from './FixedLayout.module.css';
describe('FixedLayout', () => {
baselineComponent(FixedLayout);
it(
'check update width by parent width',
withFakeTimers(async () => {
const parentRef: RefObject<HTMLDivElement | null> = {
current: null,
};
const layoutRef: RefObject<HTMLDivElement | null> = {
current: null,
};
let parentWidth = 500;
const mockParentRef = (element: HTMLDivElement) => {
if (!element) {
return;
}
vi.spyOn(element, 'getBoundingClientRect').mockImplementation(
() => new DOMRect(0, 0, parentWidth, 800),
);
parentRef.current = element;
};
render(
<div ref={mockParentRef} style={{ width: 500, height: 800 }}>
<FixedLayout getRootRef={layoutRef} useParentWidth>
<div style={{ width: 200, height: 400 }} />
</FixedLayout>
</div>,
);
expect(layoutRef.current!).toHaveStyle('width: 500px');
parentWidth = 600;
act(() => {
globalThis.__resizeObserverMock.triggerAll();
vi.runAllTimers();
});
expect(layoutRef.current!).toHaveStyle('width: 600px');
}),
);
it(
'check update width by column width',
withFakeTimers(async () => {
const colRef: RefObject<HTMLDivElement | null> = {
current: null,
};
const layoutRef: RefObject<HTMLDivElement | null> = {
current: null,
};
let colWidth = 280;
const mockColRef = (element: HTMLDivElement) => {
if (!element) {
return;
}
vi.spyOn(element, 'clientWidth', 'get').mockImplementation(() => colWidth);
colRef.current = element;
};
render(
<SplitCol width={colWidth} maxWidth={colWidth} getRootRef={mockColRef}>
<FixedLayout getRootRef={layoutRef}>
<div style={{ width: colWidth, height: 400 }} />
</FixedLayout>
</SplitCol>,
);
expect(layoutRef.current!).toHaveStyle('width: 280px');
colWidth = 360;
act(() => {
globalThis.__resizeObserverMock.triggerAll();
vi.runAllTimers();
});
expect(layoutRef.current!).toHaveStyle('width: 360px');
}),
);
describe('check correct classNames', () => {
it.each<{ props: Partial<FixedLayoutProps>; className: string }>([
{
props: {
filled: true,
},
className: styles.filled,
},
{
props: {
vertical: 'top',
},
className: styles.verticalTop,
},
{
props: {
vertical: 'bottom',
},
className: styles.verticalBottom,
},
])('should have className $className when use props $props', ({ props, className }) => {
const layoutRef: RefObject<HTMLDivElement | null> = {
current: null,
};
render(
<FixedLayout getRootRef={layoutRef} {...props}>
<div />
</FixedLayout>,
);
expect(layoutRef.current!).toHaveClass(className);
});
});
});