-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
state_machine.spec.js
160 lines (128 loc) · 3.8 KB
/
state_machine.spec.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
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
var StateMachine = require('../../../lib/init/state_machine')
describe('init/StateMachine', () => {
var done
var machine
var mockRli = {
close: () => null,
write: () => null,
prompt: () => null,
_deleteLineLeft: () => null,
_deleteLineRight: () => null
}
var mockColors = {
question: () => ''
}
beforeEach(() => {
machine = new StateMachine(mockRli, mockColors)
done = sinon.spy()
})
it('should go through all the questions', () => {
var questions = [
{id: 'framework', options: ['jasmine', 'mocha']},
{id: 'other'}
]
done = sinon.spy((answers) => {
expect(answers.framework).to.equal('jasmine')
expect(answers.other).to.equal('abc')
})
machine.process(questions, done)
machine.onLine('jasmine')
machine.onLine('abc')
expect(done).to.have.been.called
})
it('should allow multiple answers', () => {
var questions = [
{id: 'browsers', multiple: true}
]
done = sinon.spy((answers) => {
expect(answers.browsers).to.deep.equal(['Chrome', 'Safari'])
})
machine.process(questions, done)
machine.onLine('Chrome')
machine.onLine('Safari')
machine.onLine('')
expect(done).to.have.been.called
})
it('should treat spaces as confirmation of multiple answers', () => {
var questions = [
{id: 'browsers', multiple: true}
]
done = sinon.spy((answers) => {
expect(answers.browsers).to.deep.equal(['Chrome'])
})
machine.process(questions, done)
machine.onLine('Chrome')
machine.onLine(' ')
expect(done).to.have.been.called
})
it('should always return array for multiple', () => {
var questions = [
{id: 'empty', multiple: true}
]
done = sinon.spy((answers) => {
expect(answers.empty).to.deep.equal([])
})
machine.process(questions, done)
machine.onLine('')
expect(done).to.have.been.called
})
it('should validate answers', () => {
var validator = sinon.spy()
var questions = [
{id: 'validated', validate: validator}
]
machine.process(questions, done)
machine.onLine('something')
expect(done).to.have.been.called
expect(validator).to.have.been.calledWith('something')
})
it('should allow conditional answers', () => {
var ifTrue = sinon.spy((answers) => {
return answers.first === 'true'
})
var ifFalse = sinon.spy((answers) => {
return answers.first === 'false'
})
done = sinon.spy((answers) => {
expect(answers.first).to.equal('true')
expect(answers.onlyIfTrue).to.equal('something')
expect(answers.onlyIfFalse).to.not.exist
})
var questions = [
{id: 'first'},
{id: 'onlyIfTrue', condition: ifTrue},
{id: 'onlyIfFalse', condition: ifFalse}
]
machine.process(questions, done)
machine.onLine('true')
machine.onLine('something')
expect(done).to.have.been.called
})
it('should parse booleans', () => {
done = sinon.spy((answers) => {
expect(answers.yes).to.equal(true)
expect(answers.no).to.equal(false)
})
var questions = [
{id: 'yes', options: ['yes', 'no'], boolean: true},
{id: 'no', options: ['yes', 'no'], boolean: true}
]
machine.process(questions, done)
machine.onLine('yes')
machine.onLine('no')
expect(done).to.have.been.called
})
it('should parse booleans before validation', () => {
var validator = sinon.spy((value) => {
expect(typeof value).to.equal('boolean')
})
var questions = [
{id: 'what', options: ['yes', 'no'], boolean: true, validate: validator},
{id: 'really', options: ['yes', 'no'], boolean: true, validate: validator}
]
machine.process(questions, done)
machine.onLine('yes')
machine.onLine('no')
expect(validator).to.have.been.calledTwice
})
})