|
5 | 5 | import 'react-native'; |
6 | 6 | import React from 'react'; |
7 | 7 | import Button from '../lib/Button'; |
| 8 | +import { colors } from '../lib/utils'; |
8 | 9 | // Note: test renderer must be required after react-native. |
9 | | -import renderer from 'react-test-renderer'; |
10 | | - |
11 | | -it('Button', () => { |
12 | | - // const component = TestRenderer.create(<Radio value="1">Radio</Radio>); |
13 | | - const component = renderer.create( |
14 | | - <Button type="primary" rounded disabled loading={true} size="large" bordered color="red"> |
15 | | - 按钮 |
16 | | - </Button>, |
17 | | - ); |
18 | | - expect(component.root.props.type).toBe('primary'); |
19 | | - expect(component.root.props.color).toBe('red'); |
20 | | - expect(component.root.props.rounded).toBeTruthy(); |
21 | | - expect(component.root.props.disabled).toBeTruthy(); |
22 | | - expect(component.root.props.loading).toBeTruthy(); |
23 | | - expect(component.root.props.bordered).toBeTruthy(); |
24 | | - expect(component.root.props.size).toBe('large'); |
25 | | - expect(component.root.props.children).toBe('按钮'); |
| 10 | +import TestRenderer from 'react-test-renderer'; |
| 11 | +import { colorRgb } from '../utils'; |
| 12 | +import { render, screen, fireEvent } from '@testing-library/react-native'; |
| 13 | + |
| 14 | +describe('Button', () => { |
| 15 | + test('renders with color', () => { |
| 16 | + const testInstance = TestRenderer.create( |
| 17 | + <> |
| 18 | + <Button type="primary">主要按钮</Button> |
| 19 | + <Button type="success">成功按钮</Button> |
| 20 | + <Button color="#1EABCD">自定义颜色</Button> |
| 21 | + </>, |
| 22 | + ) as any; |
| 23 | + expect(testInstance.toJSON()[0].props.style.backgroundColor).toBe(colorRgb(colors.blue)); |
| 24 | + expect(testInstance.toJSON()[1].props.style.backgroundColor).toBe(colorRgb(colors.green)); |
| 25 | + expect(testInstance.toJSON()[2].props.style.backgroundColor).toBe(colorRgb('#1EABCD')); |
| 26 | + }); |
| 27 | + test('loading with ActivityIndicator', () => { |
| 28 | + const testInstance = TestRenderer.create(<Button loading>加载中按钮</Button>) as any; |
| 29 | + const testRoot = testInstance.root; |
| 30 | + |
| 31 | + expect(testRoot.props.loading).toBeTruthy(); |
| 32 | + expect(testInstance.toJSON().children[0].type).toBe('ActivityIndicator'); |
| 33 | + }); |
| 34 | + |
| 35 | + // test('renders with onClick', () => { |
| 36 | + // const DefaultButton = () => { |
| 37 | + // const [loading, setLoading] = React.useState(true) |
| 38 | + // return ( |
| 39 | + // <Button onPress={() => setLoading(false)} loading={loading}> |
| 40 | + // click loading |
| 41 | + // </Button> |
| 42 | + // ) |
| 43 | + // } |
| 44 | + |
| 45 | + // render(<DefaultButton />); |
| 46 | + |
| 47 | + // const { getByText } = render(<DefaultButton />) |
| 48 | + |
| 49 | + // const btn = getByText('click loading') |
| 50 | + |
| 51 | + // }) |
26 | 52 | }); |
0 commit comments