Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

52 changes: 41 additions & 11 deletions tests/index.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
/* eslint-disable react/no-render-return-value, max-classes-per-file, func-names, no-console */
import { render } from '@testing-library/react';
import { fireEvent, render } from '@testing-library/react';
import type { ReactWrapper } from 'enzyme';
import { mount } from 'enzyme';
import { Provider } from 'rc-motion';
import KeyCode from 'rc-util/lib/KeyCode';
import React, { cloneElement, useEffect } from 'react';
import { act } from 'react-dom/test-utils';
import type { DialogProps } from '../src';
import Dialog from '../src';

describe('dialog', () => {
async function runFakeTimer() {
for (let i = 0; i < 100; i += 1) {
await act(async () => {
jest.advanceTimersByTime(100);
await Promise.resolve();
});
}
}

beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});

Expand Down Expand Up @@ -251,15 +262,17 @@ describe('dialog', () => {
});

it('trap focus after shift-tabbing', () => {
const wrapper = mount(<Dialog visible />, { attachTo: document.body });
wrapper.find('.rc-dialog-wrap').simulate('keyDown', {
render(<Dialog visible />);

document.querySelector<HTMLDivElement>('.rc-dialog > div').focus();

fireEvent.keyDown(document.querySelector('.rc-dialog-wrap'), {
keyCode: KeyCode.TAB,
key: 'Tab',
shiftKey: true,
});
const sentinelEnd = document.querySelectorAll('.rc-dialog-content + div')[0];
const sentinelEnd = document.querySelector('.rc-dialog-content + div');
expect(document.activeElement).toBe(sentinelEnd);

wrapper.unmount();
});
});

Expand Down Expand Up @@ -506,15 +519,32 @@ describe('dialog', () => {
});

describe('afterOpenChange', () => {
it('should trigger afterOpenChange when visible changed', () => {
beforeEach(() => {
jest.useFakeTimers();
});

afterEach(() => {
jest.clearAllTimers();
jest.useRealTimers();
});

it('should trigger afterOpenChange when visible changed', async () => {
const afterOpenChange = jest.fn();

const wrapper = mount(<Dialog afterOpenChange={afterOpenChange} visible />);
jest.runAllTimers();
const Demo = (props: any) => (
<Provider motion={false}>
<Dialog afterOpenChange={afterOpenChange} {...props} />
</Provider>
);

wrapper.setProps({ visible: false });
jest.runAllTimers();
const { rerender } = render(<Demo visible />);
await runFakeTimer();
expect(afterOpenChange).toHaveBeenCalledWith(true);
expect(afterOpenChange).toHaveBeenCalledTimes(1);

rerender(<Demo />);
await runFakeTimer();
expect(afterOpenChange).toHaveBeenCalledWith(false);
expect(afterOpenChange).toHaveBeenCalledTimes(2);
});
});
Expand Down
15 changes: 13 additions & 2 deletions tests/setup.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
/* eslint-disable no-console */
global.requestAnimationFrame = cb => setTimeout(cb, 0);
global.requestAnimationFrame = (cb) => {
return global.setTimeout(cb, 0);
};
global.cancelAnimationFrame = (cb) => {
return global.clearTimeout(cb, 0);
};
window.requestAnimationFrame = (cb) => {
return window.setTimeout(cb, 0);
};
window.cancelAnimationFrame = (cb) => {
return window.clearTimeout(cb, 0);
};

const originError = console.error;
const ignoreList = [
'Rendering components directly into document.body',
'Warning: unmountComponentAtNode():',
];
console.error = (...args) => {
if (ignoreList.some(str => args[0].includes(str))) {
if (ignoreList.some((str) => args[0].includes(str))) {
return;
}

Expand Down