import { mount } from 'enzyme'; import React, { useRef } from 'react'; import { Button } from 'antd'; import ProDescriptions from '@ant-design/pro-descriptions'; import type { ProCoreActionType } from '@ant-design/pro-utils'; import { act } from 'react-dom/test-utils'; import { waitForComponentToPaint, waitTime } from '../util'; describe('descriptions', () => { it('🥩 descriptions render valueEnum when data = 0', async () => { const html = mount( ({ data: { status: 0, }, })} />, ); await waitForComponentToPaint(html, 200); expect(html.find('span.ant-badge-status-text').text()).toBe('关闭'); }); it('🎏 onLoadingChange test', async () => { const fn = jest.fn(); const html = mount( { return { data: [], }; }} />, ); await waitForComponentToPaint(html, 1200); expect(fn).toBeCalled(); }); it('🎏 loading test', async () => { const html = mount( { return new Promise((resolve) => { setTimeout(() => { resolve({ data: [] }); }, 5000); }); }} />, ); await waitForComponentToPaint(html, 1200); expect(html.find('.ant-skeleton').exists()).toBeTruthy(); act(() => { html.setProps({ loading: false, }); }); await waitForComponentToPaint(html, 1200); // props 指定为 false 后,无论 request 完成与否都不会出现 spin expect(html.find('.ant-skeleton').exists()).toBeFalsy(); }); it('🥩 test reload', async () => { const fn = jest.fn(); const Reload = () => { const actionRef = useRef(); return ( { fn(); await waitTime(200); return Promise.resolve({ success: true, data: { id: '这是一段文本', date: '20200730', money: '12121' }, }); }} extra={ } > ); }; const html = mount(); await waitForComponentToPaint(html, 300); act(() => { html.find('Button#reload').simulate('click'); }); act(() => { html.find('Button#reload').simulate('click'); }); await waitForComponentToPaint(html); // 因为有 loading 的控制,所有只会触发两次 expect(fn).toBeCalledTimes(2); }); it('🥩 test reload by params', async () => { const fn = jest.fn(); const html = mount( { fn(); return Promise.resolve({ success: true, data: { id: '这是一段文本', date: '20200730', money: '12121' }, }); }} extra={ } > , ); await waitForComponentToPaint(html, 300); act(() => { html.setProps({ params: { name: 'qixian' }, }); }); await waitForComponentToPaint(html); expect(fn).toBeCalledTimes(2); }); it('🥩 test request error', async () => { const fn = jest.fn(); const html = mount( { throw new Error('load error'); }} onRequestError={fn} extra={ } > , ); await waitForComponentToPaint(html, 300); expect(fn).toBeCalledTimes(1); }); it('🏊‍♂️ Progress', () => { const html = mount( 40 -1 100 , ); expect(html.find('.ant-progress-text').at(0).text()).toEqual('40%'); expect(html.find('.ant-progress-text').at(1).find('.anticon-close-circle')).toBeTruthy(); expect(html.find('.ant-progress-text').at(1).find('.anticon-check-circle')).toBeTruthy(); }); it('🏊‍♂️ ProDescriptions support order', () => { const html = mount( 40 -1 100 , ); act(() => { expect(html.render()).toMatchSnapshot(); }); }); });