-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbutton.test.tsx
More file actions
63 lines (55 loc) · 2.25 KB
/
button.test.tsx
File metadata and controls
63 lines (55 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import React from 'react'
import { render, fireEvent} from '@testing-library/react'
import Button, {ButtonProps, ButtonSize, ButtonType} from './button';
const defaultProps = {
onClick: jest.fn()
}
describe("test Button component", () => {
it("should render the correct default button", () => {
const wrapper = render(<Button {...defaultProps}>Nice</Button>);
const element = wrapper.getByText('Nice') as HTMLButtonElement;
expect(element).toBeInTheDocument();
expect(element.tagName).toEqual("BUTTON");
expect(element).toHaveClass("btn btn-default");
expect(element.disabled).toBeFalsy();
fireEvent.click(element);
expect(defaultProps.onClick).toHaveBeenCalled();
})
const testProps: ButtonProps = {
btnType: ButtonType.Primary,
size: ButtonSize.Large,
className: 'klass',
disabled: true
}
it("should render the correct component based on different props", () => {
const wrapper = render(<Button {...testProps}>Nice</Button>);
const element = wrapper.getByText('Nice');
expect(element).toBeInTheDocument();
//不同的类型测试不同的类
expect(element).toHaveClass("btn btn-primary btn-lg klass");
expect(element).toBeDisabled();
})
const LinktestProps: ButtonProps = {
btnType: ButtonType.Link,
href: "http://www.baidu.com"
}
it("should render a link when btnType equals link and href is provided", () => {
const wrapper = render(<Button {...LinktestProps}>Link</Button>)
const element = wrapper.getByText('Link');
expect(element).toBeInTheDocument();
expect(element.tagName).toEqual("A");
expect(element).toHaveClass("btn btn-Link");
})
const disabledProps = {
disabled: true,
onClick: jest.fn()
}
it("should render disabled button when disabled set to true", () => {
const wrapper = render(<Button {...disabledProps}>Nice</Button>);
const element = wrapper.getByText('Nice') as HTMLButtonElement;
expect(element).toBeInTheDocument();
expect(element.disabled).toBeTruthy();
fireEvent.click(element);
expect(disabledProps.onClick).not.toHaveBeenCalled();
})
})