-
Notifications
You must be signed in to change notification settings - Fork 0
/
Button.spec.tsx
60 lines (53 loc) · 1.56 KB
/
Button.spec.tsx
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
/**
* @jest-environment jsdom
*/
import React from 'react'
import ReactDOM from 'react-dom'
import { act } from 'react-dom/test-utils'
import Btn from './Button'
// https://reactjs.org/docs/test-utils.html
let container:HTMLElement
beforeEach(() => {
container = document.createElement('div')
document.body.appendChild(container)
})
afterEach(() => {
document.body.removeChild(container)
container = null
})
describe('React', () => {
test('测试属性', () => {
act(() => {
ReactDOM.render(
<div>
<Btn type="primary" id="primary-btn"></Btn>
<Btn id="primary-btn2"></Btn>
<Btn type="danger" id="danger-btn"></Btn>
</div>,
container)
})
const primaryBtn:HTMLElement = container.querySelector('#primary-btn')
expect(primaryBtn.style.color).toBe('blue')
// 测试默认属性
const primaryBtn2:HTMLElement = container.querySelector('#primary-btn2')
expect(primaryBtn2.style.color).toBe('blue')
const dangerBtn:HTMLElement = container.querySelector('#danger-btn')
expect(dangerBtn.style.color).toBe('red')
})
test('测试children', () => {
act(() => {
ReactDOM.render(<Btn type="primary">abc</Btn>, container)
})
const btn = container.querySelector('.btn')
expect(btn.textContent).toBe('abc')
})
test('测试回调', () => {
const cb = jest.fn()
act(() => {
ReactDOM.render(<Btn type="primary" onClick={cb}></Btn>, container)
})
const btn: HTMLElement = container.querySelector('.btn')
btn.click()
expect(cb).toBeCalled()
})
})