chore: make testing more stable

This commit is contained in:
Rain 2023-07-28 11:02:13 +08:00
parent 880f7ed7e4
commit e7ade9ee12
4 changed files with 23 additions and 19 deletions

View File

@ -1,5 +1,5 @@
import React from 'react';
import { fireEvent, render, screen, sleep, userEvent } from 'testUtils';
import { fireEvent, render, screen, userEvent, waitFor } from 'testUtils';
import App1 from '../demos/demo1';
import App2 from '../demos/demo2';
@ -33,8 +33,9 @@ describe('Cascader', () => {
// 因为是异步加载,所以需要等待一下
expect(screen.queryByText('Zhejiang Dynamic 1')).not.toBeInTheDocument();
await sleep(300);
expect(screen.getByText('Zhejiang Dynamic 1')).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByText('Zhejiang Dynamic 1')).toBeInTheDocument();
});
fireEvent.click(screen.getByText('Zhejiang Dynamic 1'));

View File

@ -1,5 +1,5 @@
import React from 'react';
import { render, screen, sleep, userEvent, waitFor } from 'testUtils';
import { render, screen, userEvent, waitFor } from 'testUtils';
import App1 from '../demos/demo1';
import App2 from '../demos/demo2';
import App3 from '../demos/demo3';
@ -17,10 +17,10 @@ describe('FormV2', () => {
await userEvent.type(input, '李四');
await userEvent.click(submit);
await sleep(100);
// notification 的内容
expect(screen.getByText(/\{"nickname":"李四"\}/i)).toBeInTheDocument();
await waitFor(() => {
// notification 的内容
expect(screen.getByText(/\{"nickname":"李四"\}/i)).toBeInTheDocument();
});
});
it('initial values', async () => {

View File

@ -1,5 +1,5 @@
import React from 'react';
import { render, sleep } from 'testUtils';
import { render, waitFor } from 'testUtils';
import App1 from '../demos/demo1';
// jsdom does not support canvas, so we need to skip this test
@ -7,9 +7,9 @@ describe.skip('G2Plot', () => {
it('basic', async () => {
render(<App1 />);
await sleep(100);
const g2plot = document.querySelector('.g2plot') as HTMLDivElement;
expect(g2plot).toBeInTheDocument();
await waitFor(() => {
const g2plot = document.querySelector('.g2plot') as HTMLDivElement;
expect(g2plot).toBeInTheDocument();
});
});
});

View File

@ -1,5 +1,5 @@
import React from 'react';
import { render, screen, sleep, userEvent, within } from 'testUtils';
import { render, screen, userEvent, waitFor, within } from 'testUtils';
import App1 from '../demos/demo1';
describe('RecordPicker', () => {
@ -10,17 +10,20 @@ describe('RecordPicker', () => {
expect(selector).toBeInTheDocument();
await userEvent.click(selector);
await sleep(100);
// 弹窗标题
expect(screen.getByText(/select record/i)).toBeInTheDocument();
await waitFor(() => {
// 弹窗标题
expect(screen.getByText(/select record/i)).toBeInTheDocument();
});
const checkboxes = document.querySelectorAll('.ant-checkbox');
// 第 3 个选项的内容是: “软件开发”
await userEvent.click(checkboxes[2]);
await userEvent.click(screen.getByText(/submit/i));
expect(within(selector).getByText(/软件开发/i)).toBeInTheDocument();
expect(screen.getByText(/软件开发/i, { selector: '.test-record-picker-read-pretty-item' })).toBeInTheDocument();
await waitFor(() => {
expect(within(selector).getByText(/软件开发/i)).toBeInTheDocument();
expect(screen.getByText(/软件开发/i, { selector: '.test-record-picker-read-pretty-item' })).toBeInTheDocument();
});
});
});