forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollapse.test.js
More file actions
304 lines (262 loc) · 9.12 KB
/
Collapse.test.js
File metadata and controls
304 lines (262 loc) · 9.12 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import * as React from 'react';
import { expect } from 'chai';
import { spy, stub } from 'sinon';
import { act, createRenderer, isJsdom } from '@mui/internal-test-utils';
import { Transition } from 'react-transition-group';
import { ThemeProvider, createTheme } from '@mui/material/styles';
import Collapse, { collapseClasses as classes } from '@mui/material/Collapse';
import describeConformance from '../../test/describeConformance';
const CustomCollapse = React.forwardRef(({ ownerState, ...props }, ref) => (
<div ref={ref} {...props} />
));
const CustomWrapper = React.forwardRef(({ ownerState, ...props }, ref) => (
<div ref={ref} {...props} />
));
const CustomWrapperInner = React.forwardRef(({ ownerState, ...props }, ref) => (
<div ref={ref} {...props} />
));
describe('<Collapse />', () => {
const { clock, render } = createRenderer();
const defaultProps = {
in: true,
children: <div />,
};
describeConformance(<Collapse {...defaultProps} />, () => ({
classes,
inheritComponent: Transition,
render,
refInstanceof: window.HTMLDivElement,
muiName: 'MuiCollapse',
testVariantProps: { orientation: 'horizontal' },
testDeepOverrides: { slotName: 'wrapper', slotClassName: classes.wrapper },
slots: {
root: { expectedClassName: classes.root, testWithElement: CustomCollapse },
wrapper: { expectedClassName: classes.wrapper, testWithElement: CustomWrapper },
wrapperInner: {
expectedClassName: classes.wrapperInner,
testWithElement: CustomWrapperInner,
},
},
}));
it('should render a container around the wrapper', () => {
const { container } = render(
<Collapse {...defaultProps} classes={{ root: 'woofCollapse1' }} />,
);
const collapse = container.firstChild;
expect(collapse).to.have.tagName('div');
expect(collapse).to.have.class(classes.root);
expect(collapse).to.have.class('woofCollapse1');
});
it('should render a wrapper around the children', () => {
const children = <h1>Hello</h1>;
const { container } = render(<Collapse {...defaultProps}>{children}</Collapse>);
const collapse = container.firstChild;
const wrapper = collapse.firstChild;
const innerWrapper = wrapper.firstChild;
expect(wrapper).to.have.tagName('div');
expect(innerWrapper.firstChild).to.have.tagName('h1');
});
describe('transition lifecycle', () => {
clock.withFakeTimers();
let setProps;
let collapse;
let container;
let nodeEnterHeightStyle;
let nodeEnteringHeightStyle;
let nodeExitHeightStyle;
/* We needs to create wrappers here because the node is passed by reference
and it's style is overwritten by the later stages */
const handleEnter = spy();
const handleEnterWrapper = (...args) => {
handleEnter(...args);
nodeEnterHeightStyle = args[0].style.height;
};
const handleEntering = spy();
const handleEnteringWrapper = (...args) => {
handleEntering(...args);
nodeEnteringHeightStyle = args[0].style.height;
};
const handleEntered = spy();
const handleExit = spy();
const handleExitWrapper = (...args) => {
handleExit(...args);
nodeExitHeightStyle = args[0].style.height;
};
const handleExiting = spy();
const handleExited = spy();
const handleAddEndListener = spy();
beforeEach(() => {
// eslint-disable-next-line testing-library/no-render-in-lifecycle
({ container, setProps } = render(
<Collapse
addEndListener={handleAddEndListener}
onEnter={handleEnterWrapper}
onEntering={handleEnteringWrapper}
onEntered={handleEntered}
onExit={handleExitWrapper}
onExiting={handleExiting}
onExited={handleExited}
timeout={300}
>
<div />
</Collapse>,
));
collapse = container.firstChild;
stub(collapse.firstChild, 'clientHeight').get(() => 666);
});
it('should run in', () => {
setProps({ in: true });
expect(nodeEnterHeightStyle).to.equal('0px');
expect(handleEnter.args[0][0]).to.equal(collapse);
expect(handleEnter.args[0][1]).to.equal(false);
expect(nodeEnteringHeightStyle).to.equal('666px');
expect(handleEntering.callCount).to.equal(1);
expect(handleEntering.args[0][0]).to.equal(collapse);
expect(handleEntering.args[0][1]).to.equal(false);
expect(handleAddEndListener.callCount).to.equal(1);
expect(handleAddEndListener.args[0][0]).to.equal(collapse);
expect(typeof handleAddEndListener.args[0][1]).to.equal('function');
clock.tick(300);
expect(handleEntered.args[0][0].style.height).to.equal('auto');
expect(handleEntered.args[0][1]).to.equal(false);
expect(handleEntered.callCount).to.equal(1);
});
it('should run out', () => {
setProps({ in: true });
setProps({ in: false });
expect(nodeExitHeightStyle).to.equal('666px');
expect(handleExiting.args[0][0].style.height).to.equal('0px');
expect(handleExiting.callCount).to.equal(1);
expect(handleExiting.args[0][0]).to.equal(collapse);
clock.tick(300);
expect(handleExited.args[0][0].style.height).to.equal('0px');
clock.tick(300);
expect(handleExited.callCount).to.equal(1);
expect(handleExited.args[0][0]).to.equal(collapse);
});
});
describe('prop: timeout', () => {
clock.withFakeTimers();
it('should delay based on height when timeout is auto', () => {
const theme = createTheme({
transitions: {
getAutoHeightDuration: (n) => n,
},
});
const next1 = spy();
function Test(props) {
return (
<ThemeProvider theme={theme}>
<Collapse timeout="auto" onEntered={next1} {...props}>
<div />
</Collapse>
</ThemeProvider>
);
}
const { setProps: setProps1, container: container1 } = render(<Test />);
const collapse = container1.firstChild;
// Gets wrapper
stub(collapse.firstChild, 'clientHeight').get(() => 10);
setProps1({
in: true,
});
const autoTransitionDuration = 10;
expect(next1.callCount).to.equal(0);
clock.tick(0);
expect(next1.callCount).to.equal(0);
clock.tick(autoTransitionDuration);
expect(next1.callCount).to.equal(1);
const next2 = spy();
const { setProps: setProps2 } = render(
<Collapse timeout="auto" onEntered={next2}>
<div />
</Collapse>,
);
setProps2({ in: true });
expect(next2.callCount).to.equal(0);
clock.tick(0);
expect(next2.callCount).to.equal(1);
});
it('should use timeout as delay when timeout is number', () => {
const timeout = 10;
const next = spy();
const { setProps } = render(
<Collapse timeout={timeout} onEntered={next}>
<div />
</Collapse>,
);
setProps({ in: true });
expect(next.callCount).to.equal(0);
act(() => {
clock.tick(0);
});
expect(next.callCount).to.equal(0);
act(() => {
clock.tick(timeout);
});
expect(next.callCount).to.equal(1);
});
it('should create proper easeOut animation onEntering', () => {
const handleEntering = spy();
const { setProps } = render(
<Collapse
onEntering={handleEntering}
timeout={{
enter: 556,
}}
>
<div />
</Collapse>,
);
setProps({ in: true });
expect(handleEntering.args[0][0].style.transitionDuration).to.equal('556ms');
});
it('should create proper sharp animation onExiting', () => {
const handleExiting = spy();
const { setProps } = render(
<Collapse
{...defaultProps}
onExiting={handleExiting}
timeout={{
exit: 446,
}}
/>,
);
setProps({
in: false,
});
expect(handleExiting.args[0][0].style.transitionDuration).to.equal('446ms');
});
});
describe('prop: collapsedSize', () => {
const collapsedSize = '10px';
it('should work when closed', () => {
const { container } = render(<Collapse {...defaultProps} collapsedSize={collapsedSize} />);
const collapse = container.firstChild;
expect(collapse.style.minHeight).to.equal(collapsedSize);
});
it('should be taken into account in handleExiting', () => {
const handleExiting = spy();
const { setProps } = render(
<Collapse {...defaultProps} collapsedSize={collapsedSize} onExiting={handleExiting} />,
);
setProps({ in: false });
expect(handleExiting.args[0][0].style.height).to.equal(collapsedSize);
});
});
// Test for https://github.com/mui/material-ui/issues/40653
it.skipIf(isJsdom())(
'should render correctly when external ownerState prop is passed',
function test() {
const { container } = render(
<Collapse in ownerState={{}}>
<div style={{ height: '100px' }} />
</Collapse>,
);
const collapse = container.firstChild;
expect(collapse).toHaveComputedStyle({
height: '100px',
});
},
);
});