|
1 | | -import React from 'react'; |
2 | | -import Fullscreen from '../index'; |
3 | | -import { render, fireEvent, cleanup } from '@testing-library/react'; |
| 1 | +import { ArrowsAltOutlined, ShrinkOutlined } from '@ant-design/icons'; |
4 | 2 | import '@testing-library/jest-dom/extend-expect'; |
| 3 | +import { cleanup, fireEvent, render, screen } from '@testing-library/react'; |
| 4 | +import React from 'react'; |
| 5 | +import Fullscreen from '..'; |
5 | 6 | describe('test Fullscreen', () => { |
6 | 7 | afterEach(() => { |
7 | 8 | cleanup(); |
8 | 9 | }); |
9 | | - test('should icon render correct', () => { |
10 | | - const { getByTestId } = render(<Fullscreen />); |
11 | | - const element = getByTestId('test_myIcon'); |
12 | | - expect(element).toBeInTheDocument(); |
13 | | - expect(element).toHaveClass('dtc-fullscreen-icon'); |
| 10 | + test('should default render correctly', () => { |
| 11 | + const { container } = render(<Fullscreen />); |
| 12 | + expect(container.firstChild).toMatchSnapshot(); |
| 13 | + expect(container.querySelector('.ant-btn')?.firstChild).toHaveClass('dtc-fullscreen-icon'); |
| 14 | + expect(container.innerHTML).toMatch('全屏'); |
14 | 15 | }); |
15 | | - test('should button render correct', () => { |
16 | | - const { getByTestId } = render(<Fullscreen />); |
17 | | - const element = getByTestId('test_myButton'); |
18 | | - expect(element).toBeInTheDocument(); |
19 | | - expect(element).toHaveTextContent('全屏'); |
| 16 | + test('should customIcon render correctly', () => { |
| 17 | + const customIcon = { |
| 18 | + fullIcon: ( |
| 19 | + <> |
| 20 | + <ArrowsAltOutlined /> |
| 21 | + 全屏 |
| 22 | + </> |
| 23 | + ), |
| 24 | + exitFullIcon: ( |
| 25 | + <> |
| 26 | + <ShrinkOutlined /> |
| 27 | + 退出全屏 |
| 28 | + </> |
| 29 | + ), |
| 30 | + }; |
| 31 | + const { container } = render( |
| 32 | + <Fullscreen fullIcon={customIcon.fullIcon} exitFullIcon={customIcon.exitFullIcon} /> |
| 33 | + ); |
| 34 | + expect(container.firstChild).not.toHaveClass('dtc-fullscreen-icon'); |
| 35 | + }); |
| 36 | + test('should custom iconStyle render correctly', () => { |
| 37 | + const iconStyle = { |
| 38 | + width: 12, |
| 39 | + height: 12, |
| 40 | + marginRight: 5, |
| 41 | + }; |
| 42 | + render(<Fullscreen iconStyle={iconStyle} />); |
| 43 | + const img = screen.getByRole('img'); |
| 44 | + screen.debug(img); |
| 45 | + expect(img).toHaveStyle(`width: 12px; height: 12px; margin-right: 5px;`); |
20 | 46 | }); |
21 | 47 | test('button fireEvent correct', () => { |
22 | | - const { container, getByTestId } = render(<Fullscreen />); |
23 | | - const element = getByTestId('test_myButton'); |
24 | | - fireEvent.click(element); |
| 48 | + const { container } = render(<Fullscreen />); |
25 | 49 | expect(container.innerHTML).toMatch('全屏'); |
| 50 | + const button = screen.getByRole('button', { name: '全屏' }); |
| 51 | + fireEvent.click(button); |
| 52 | + }); |
| 53 | + test('button props onFullscreen correct', () => { |
| 54 | + const callback = jest.fn(); |
| 55 | + render(<Fullscreen onFullscreen={callback} />); |
| 56 | + const button = screen.getByRole('button', { name: '全屏' }); |
| 57 | + fireEvent.click(button); |
| 58 | + expect(callback).toHaveBeenCalledTimes(1); |
26 | 59 | }); |
27 | 60 | }); |
0 commit comments