forked from didi/mand-mobile
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.spec.js
More file actions
186 lines (163 loc) · 3.96 KB
/
index.spec.js
File metadata and controls
186 lines (163 loc) · 3.96 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import {Captcha} from 'mand-mobile'
import sinon from 'sinon'
import {mount} from '@vue/test-utils'
describe('Captcha - Operation', () => {
let wrapper
afterEach(() => {
wrapper && wrapper.destroy()
})
document.body.removeChild = () => {}
it('create a simple captcha', () => {
wrapper = mount(Captcha, {
propsData: {
system: true,
value: true,
},
sync: false,
})
expect(wrapper.classes('md-captcha')).toBe(true)
})
it('create a captcha and not append to body', () => {
wrapper = mount(Captcha, {
propsData: {
appendTo: false,
},
})
expect(wrapper.vm.$el.parentNode).not.toEqual(document.body)
})
it('create inline captcha', () => {
wrapper = mount(Captcha, {
propsData: {
isView: true,
},
})
expect(wrapper.contains('.md-dialog')).toBe(false)
})
it('should clean code after shown again', () => {
wrapper = mount(Captcha, {
propsData: {
value: false,
},
data: {
code: '123',
},
})
wrapper.setProps({
value: true,
})
expect(wrapper.vm.code).toEqual('')
})
it('show and hide captcha', done => {
wrapper = mount(Captcha, {
propsData: {
value: false,
},
})
const eventStub = sinon.stub(wrapper.vm, '$emit')
wrapper.setProps({
value: true,
})
setTimeout(() => {
expect(eventStub.calledWith('show')).toBe(true)
wrapper.find('.md-dialog-close').trigger('click')
wrapper.setProps({
value: false,
})
}, 300)
setTimeout(() => {
expect(eventStub.calledWith('input', false)).toBe(true)
wrapper.setProps({
value: true,
})
setTimeout(() => {
expect(eventStub.calledWith('show')).toBe(true)
done()
}, 300)
}, 600)
})
it('set and show error message', done => {
wrapper = mount(Captcha, {
propsData: {
value: true,
},
})
wrapper.vm.setError('invalid code')
setTimeout(() => {
expect(wrapper.vm.errorMsg).toEqual('invalid code')
expect(wrapper.vm.code).toEqual('')
wrapper.setData({
code: '123',
})
setTimeout(() => {
expect(wrapper.vm.errorMsg).toEqual('')
done()
}, 0)
}, 10)
})
it('click button and emit send event', done => {
wrapper = mount(Captcha, {
propsData: {
value: true,
count: 2,
},
})
const eventStub = sinon.stub(wrapper.vm, '$emit')
setTimeout(() => {
wrapper.find('.md-captcha-btn').trigger('click')
expect(eventStub.calledWith('send')).toEqual(true)
done()
}, 2500)
})
it('auto send', done => {
wrapper = mount(Captcha, {
propsData: {
autoSend: false,
},
})
const eventStub = sinon.stub(wrapper.vm, '$emit')
wrapper.setProps({
value: true,
})
expect(eventStub.calledWith('send')).toEqual(false)
setTimeout(() => {
wrapper.find('.md-captcha-btn').trigger('click')
expect(eventStub.calledWith('send')).toEqual(true)
done()
}, 2500)
})
it('not countdown', () => {
wrapper = mount(Captcha, {
propsData: {
value: true,
count: 0,
},
})
wrapper.vm.countdown()
expect(wrapper.findAll('.md-captcha-btn').length).toEqual(0)
})
it('emit submit events', done => {
wrapper = mount(Captcha, {
propsData: {
maxlength: 4,
value: false,
isView: true,
appendTo: false,
},
})
const eventStub = sinon.stub(wrapper.vm, '$emit')
wrapper.setProps({
value: true,
})
setTimeout(() => {
const keys = wrapper.findAll('.keyboard-number-item')
keys.at(0).trigger('click')
keys.at(1).trigger('click')
keys.at(2).trigger('click')
keys.at(0).trigger('click')
setTimeout(() => {
expect(eventStub.calledWith('submit', '1231')).toBe(true)
done()
}, 0)
}, 500)
})
})