From 990a6026361366b08a09b3cb3936e53b834e39c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A4=BE=E9=95=BF=E9=95=BF?= Date: Wed, 25 May 2022 20:31:36 +0800 Subject: [PATCH] test: Replace alert part test with testing lib (#35736) Co-authored-by: chenkan1 --- components/alert/__tests__/index.test.tsx | 61 ++++++++++++----------- 1 file changed, 32 insertions(+), 29 deletions(-) diff --git a/components/alert/__tests__/index.test.tsx b/components/alert/__tests__/index.test.tsx index c25f6191da..8bebb5ecb0 100644 --- a/components/alert/__tests__/index.test.tsx +++ b/components/alert/__tests__/index.test.tsx @@ -1,13 +1,12 @@ import React from 'react'; -import { mount } from 'enzyme'; import { act } from 'react-dom/test-utils'; -import Button from '../../button'; -import Tooltip from '../../tooltip'; -import Popconfirm from '../../popconfirm'; -import rtlTest from '../../../tests/shared/rtlTest'; -import accessibilityTest from '../../../tests/shared/accessibilityTest'; -import { sleep } from '../../../tests/utils'; import Alert from '..'; +import accessibilityTest from '../../../tests/shared/accessibilityTest'; +import rtlTest from '../../../tests/shared/rtlTest'; +import { fireEvent, render, sleep } from '../../../tests/utils'; +import Button from '../../button'; +import Popconfirm from '../../popconfirm'; +import Tooltip from '../../tooltip'; const { ErrorBoundary } = Alert; @@ -25,7 +24,7 @@ describe('Alert', () => { it('could be closed', () => { const onClose = jest.fn(); - const wrapper = mount( + const { container } = render( { onClose={onClose} />, ); + act(() => { jest.useFakeTimers(); - wrapper.find('.ant-alert-close-icon').simulate('click'); + fireEvent.click(container.querySelector('.ant-alert-close-icon')!); jest.runAllTimers(); jest.useRealTimers(); }); @@ -44,7 +44,7 @@ describe('Alert', () => { describe('action of Alert', () => { it('custom action', () => { - const wrapper = mount( + const { container } = render( { closable />, ); - expect(wrapper.render()).toMatchSnapshot(); + expect(container.firstChild).toMatchSnapshot(); }); }); it('support closeIcon', () => { - const wrapper = mount( + const { container } = render( close} @@ -70,26 +70,26 @@ describe('Alert', () => { type="warning" />, ); - expect(wrapper.render()).toMatchSnapshot(); + expect(container.firstChild).toMatchSnapshot(); }); describe('data and aria props', () => { it('sets data attributes on input', () => { - const wrapper = mount(); - const input = wrapper.find('.ant-alert').getDOMNode(); + const { container } = render(); + const input = container.querySelector('.ant-alert')!; expect(input.getAttribute('data-test')).toBe('test-id'); expect(input.getAttribute('data-id')).toBe('12345'); }); it('sets aria attributes on input', () => { - const wrapper = mount(); - const input = wrapper.find('.ant-alert').getDOMNode(); + const { container } = render(); + const input = container.querySelector('.ant-alert')!; expect(input.getAttribute('aria-describedby')).toBe('some-label'); }); it('sets role attribute on input', () => { - const wrapper = mount(); - const input = wrapper.find('.ant-alert').getDOMNode(); + const { container } = render(); + const input = container.querySelector('.ant-alert')!; expect(input.getAttribute('role')).toBe('status'); }); }); @@ -101,13 +101,13 @@ describe('Alert', () => { // @ts-expect-error // eslint-disable-next-line react/jsx-no-undef const ThrowError = () => ; - const wrapper = mount( + const { container } = render( , ); // eslint-disable-next-line jest/no-standalone-expect - expect(wrapper.text()).toContain('ReferenceError: NotExisted is not defined'); + expect(container.textContent).toContain('ReferenceError: NotExisted is not defined'); // eslint-disable-next-line no-console (console.error as any).mockRestore(); }); @@ -115,7 +115,7 @@ describe('Alert', () => { it('could be used with Tooltip', async () => { const ref = React.createRef(); jest.useRealTimers(); - const wrapper = mount( + const { container } = render( { /> , ); - wrapper.find('.ant-alert').simulate('mouseenter'); + // wrapper.find('.ant-alert').simulate('mouseenter'); + fireEvent.mouseEnter(container.querySelector('.ant-alert')!); await sleep(0); expect(ref.current.getPopupDomNode()).toBeTruthy(); jest.useFakeTimers(); @@ -132,7 +133,7 @@ describe('Alert', () => { it('could be used with Popconfirm', async () => { const ref = React.createRef(); jest.useRealTimers(); - const wrapper = mount( + const { container } = render( { /> , ); - wrapper.find('.ant-alert').simulate('click'); + fireEvent.click(container.querySelector('.ant-alert')!); await sleep(0); expect(ref.current.getPopupDomNode()).toBeTruthy(); jest.useFakeTimers(); }); it('could accept none react element icon', () => { - const wrapper = mount(); - expect(wrapper.render()).toMatchSnapshot(); + const { container } = render( + , + ); + expect(container.firstChild).toMatchSnapshot(); }); it('should not render message div when no message', () => { - const wrapper = mount(); - expect(wrapper.exists('.ant-alert-message')).toBe(false); + const { container } = render(); + expect(!!container.querySelector('.ant-alert-message')).toBe(false); }); });