test: Fix table empty test (#36290)

This commit is contained in:
二货机器人 2022-06-29 12:07:28 +08:00 committed by GitHub
parent fe52f2c8dc
commit d24f67f66b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 14 deletions

View File

@ -229,7 +229,7 @@ exports[`Table renders empty table with custom emptyText 1`] = `
</div>
`;
exports[`Table renders empty table with fixed columns 1`] = `
exports[`Table renders empty table with fixed columns should work 1`] = `
<div
class="ant-table-wrapper"
>
@ -452,7 +452,7 @@ exports[`Table renders empty table with fixed columns 1`] = `
>
<div
class="ant-table-expanded-row-fixed"
style="width: 0px; position: sticky; left: 0px; overflow: hidden;"
style="width: 1000px; position: sticky; left: 0px; overflow: hidden;"
>
<div
class="ant-empty ant-empty-normal"

View File

@ -1,6 +1,7 @@
import React from 'react';
import { spyElementPrototypes } from 'rc-util/lib/test/domHook';
import Table from '..';
import { render } from '../../../tests/utils';
import { render, triggerResize, waitFor } from '../../../tests/utils';
const columns = [
{ title: 'Column 1', dataIndex: 'address', key: '1' },
@ -50,11 +51,33 @@ describe('Table', () => {
expect(asFragment().firstChild).toMatchSnapshot();
});
it('renders empty table with fixed columns', () => {
const { asFragment } = render(
<Table dataSource={[]} columns={columnsFixed} pagination={false} scroll={{ x: 1 }} />,
);
expect(asFragment().firstChild).toMatchSnapshot();
describe('renders empty table with fixed columns', () => {
let domSpy;
beforeAll(() => {
domSpy = spyElementPrototypes(HTMLDivElement, {
offsetWidth: {
get: () => 1000,
},
});
});
afterAll(() => {
domSpy.mockRestore();
});
it('should work', async () => {
const { container, asFragment } = render(
<Table dataSource={[]} columns={columnsFixed} pagination={false} scroll={{ x: 1 }} />,
);
triggerResize(container.querySelector('.ant-table'));
await waitFor(() => {
expect(container.querySelector('.ant-empty')).toBeTruthy();
});
expect(asFragment().firstChild).toMatchSnapshot();
});
});
it('renders empty table with custom emptyText', () => {

View File

@ -4,8 +4,8 @@ import { StrictMode } from 'react';
import { act } from 'react-dom/test-utils';
import type { RenderOptions } from '@testing-library/react';
import { render } from '@testing-library/react';
import { _rs as onLibResize } from 'rc-resize-observer/lib/utils/observerUtil'
import { _rs as onEsResize } from 'rc-resize-observer/es/utils/observerUtil'
import { _rs as onLibResize } from 'rc-resize-observer/lib/utils/observerUtil';
import { _rs as onEsResize } from 'rc-resize-observer/es/utils/observerUtil';
export function setMockDate(dateString = '2017-09-18T03:30:07.795') {
MockDate.set(dateString);
@ -33,11 +33,14 @@ export { customRender as render };
export const triggerResize = (target: Element) => {
const originGetBoundingClientRect = target.getBoundingClientRect;
target.getBoundingClientRect = () => ({ width: 510, height: 903 }) as DOMRect;
onLibResize([{ target } as ResizeObserverEntry]);
onEsResize([{ target } as ResizeObserverEntry]);
target.getBoundingClientRect = () => ({ width: 510, height: 903 } as DOMRect);
act(() => {
onLibResize([{ target } as ResizeObserverEntry]);
onEsResize([{ target } as ResizeObserverEntry]);
});
target.getBoundingClientRect = originGetBoundingClientRect;
}
};
export * from '@testing-library/react';