mirror of
https://github.com/ant-design/ant-design.git
synced 2024-12-23 23:18:36 +08:00
502dac12aa
* docs: fix code * feat: lint * feat: prettier * feat: test * feat: review * feat: format html * feat: format html
51 lines
1.4 KiB
TypeScript
51 lines
1.4 KiB
TypeScript
import React, { useState } from 'react';
|
|
|
|
import { fireEvent, render } from '../../../tests/utils';
|
|
import Sider from '../Sider';
|
|
|
|
const Content = () => {
|
|
const [breakpoint, setBreakpoint] = useState('sm');
|
|
const toggleBreakpoint = () => {
|
|
if (breakpoint === 'sm') {
|
|
setBreakpoint('lg');
|
|
} else {
|
|
setBreakpoint('sm');
|
|
}
|
|
};
|
|
return (
|
|
<Sider breakpoint={breakpoint as any}>
|
|
<button type="button" id="toggle" onClick={toggleBreakpoint}>
|
|
Toggle
|
|
</button>
|
|
</Sider>
|
|
);
|
|
};
|
|
|
|
it('Dynamic breakpoint in Sider component', () => {
|
|
const add = jest.fn();
|
|
const remove = jest.fn();
|
|
const newMatch = jest.spyOn(window, 'matchMedia').mockReturnValue({
|
|
matches: true,
|
|
addEventListener: add,
|
|
removeEventListener: remove,
|
|
} as any);
|
|
|
|
const { container } = render(<Content />);
|
|
|
|
// Record here since React 18 strict mode will render twice at first mount
|
|
const originCallTimes = newMatch.mock.calls.length;
|
|
expect(originCallTimes <= 2).toBeTruthy();
|
|
|
|
// subscribe at first
|
|
expect(add.mock.calls).toHaveLength(originCallTimes);
|
|
expect(remove.mock.calls).toHaveLength(originCallTimes - 1);
|
|
|
|
fireEvent.click(container.querySelector('#toggle') as Element);
|
|
|
|
expect(newMatch.mock.calls).toHaveLength(originCallTimes + 1);
|
|
expect(add.mock.calls).toHaveLength(originCallTimes + 1);
|
|
expect(remove.mock.calls).toHaveLength(originCallTimes);
|
|
|
|
jest.restoreAllMocks();
|
|
});
|