-
Notifications
You must be signed in to change notification settings - Fork 747
/
Copy pathbutton.test.js
72 lines (59 loc) · 2.33 KB
/
button.test.js
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
64
65
66
67
68
69
70
71
72
import Nerv from 'nervjs'
import { renderIntoDocument, Simulate } from 'nerv-test-utils'
import Button from '../index'
const delay = (ms) => new Promise((resolve) => setTimeout(resolve, ms))
const hoverStartTime = 20
const hoverStayTime = 70
describe('Button', () => {
it('render Button', () => {
const component = renderIntoDocument(<Button>Button</Button>)
expect(component.props.disabled).toBeFalsy()
})
it('render Button disabled', () => {
const component = renderIntoDocument(<Button disabled>Button</Button>)
expect(component.props.disabled).toBeTruthy()
})
it('show loading Botton', () => {
const component = renderIntoDocument(<Button loading>Button</Button>)
expect(component.props.loading).toBeTruthy()
})
it('should trigger touchStart and touchEnd', async () => {
const hoverClass = 'hoverclass'
const onTouchStart = jest.fn()
const onTouchEnd = jest.fn()
let btnIns
const view = <Button ref={c => (btnIns = c)} hoverClass={hoverClass} onTouchStart={onTouchStart} onTouchEnd={onTouchEnd}> Button</Button>
const component = renderIntoDocument(view)
const dom = Nerv.findDOMNode(component)
Simulate.touchStart(dom)
expect(onTouchStart).toHaveBeenCalled()
Simulate.touchEnd(dom)
await delay(hoverStartTime)
expect(dom.getAttribute('class')).not.toContain(hoverClass)
Simulate.touchStart(dom)
await delay(hoverStartTime)
expect(btnIns.state.touch).toBeTruthy()
expect(dom.getAttribute('class')).toContain(hoverClass)
Simulate.touchEnd(dom)
expect(onTouchEnd).toHaveBeenCalled()
Simulate.touchStart(dom)
await delay(hoverStayTime)
expect(dom.getAttribute('class')).toContain(hoverClass)
Simulate.touchEnd(dom)
await delay(hoverStayTime)
expect(dom.getAttribute('class')).not.toContain(hoverClass)
})
it('should not execute set hoverClass when hoverClass is undefined', async () => {
let btnIns
const view = <Button className='class' ref={c => (btnIns = c)}>Button</Button>
const component = renderIntoDocument(view)
const dom = Nerv.findDOMNode(component)
Simulate.touchStart(dom)
expect(btnIns.state.touch).toBeFalsy()
await delay(hoverStartTime)
expect(btnIns.state.hover).toBeTruthy()
Simulate.touchEnd(dom)
await delay(hoverStayTime)
expect(btnIns.state.hover).toBeFalsy()
})
})