-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathunit.test.js
More file actions
288 lines (253 loc) · 10.3 KB
/
unit.test.js
File metadata and controls
288 lines (253 loc) · 10.3 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/* eslint-env jest */
/* eslint-disable no-new */
import YarnBound from './src/index'
import bondage from '@mnbroatch/bondage/src/index'
const getMockGenerator = (results) => function * () {
for (let i = 0, len = results.length; i < len; i++) {
yield Object.assign(
results[i],
{
getGeneratorHere: () => getMockGenerator(results.slice(i))()
}
)
}
}
jest.spyOn(bondage.Runner.prototype, 'load').mockImplementation()
jest.spyOn(bondage.Runner.prototype, 'registerFunction').mockImplementation()
jest.spyOn(bondage.Runner.prototype, 'setVariableStorage').mockImplementation()
jest.spyOn(bondage.Runner.prototype, 'run')
describe('constructor', () => {
beforeAll(() => {
bondage.Runner.prototype.run.mockImplementation(getMockGenerator([
new bondage.TextResult('hello')
]))
})
const dialogue = []
test('should load a dialogue object into the runner', () => {
new YarnBound({ dialogue })
expect(bondage.Runner.prototype.load)
.toHaveBeenCalledWith(dialogue)
})
test('should load a dialogue into the runner', () => {
const dialogueWithLeadingWhitespace = `
title:Start
---
text
===
`
new YarnBound({ dialogue: dialogueWithLeadingWhitespace })
expect(bondage.Runner.prototype.load)
.toHaveBeenCalledWith(dialogueWithLeadingWhitespace)
})
test('should set the variable storage if provided', () => {
const variableStorage = new Map()
new YarnBound({ variableStorage })
expect(bondage.Runner.prototype.setVariableStorage)
.toHaveBeenCalledWith(variableStorage)
})
test('should register initially provided functions', () => {
const functions = {
functionOne: () => {},
functionTwo: () => {}
}
new YarnBound({ functions })
Object.entries(functions).forEach(([key, func]) => {
expect(bondage.Runner.prototype.registerFunction)
.toHaveBeenCalledWith(key, func)
})
})
test('can register functions after initialization', () => {
const functions = {
functionOne: () => {},
functionTwo: () => {}
}
const runner = new YarnBound({})
Object.entries(functions).forEach(([key, func]) => {
runner.registerFunction(key, func)
expect(bondage.Runner.prototype.registerFunction)
.toHaveBeenCalledWith(key, func)
})
})
test('should start the generator at the node with the provided "startAt" title', () => {
const startAt = 'someStartingNode'
new YarnBound({ startAt })
expect(bondage.Runner.prototype.run).toHaveBeenCalledWith(startAt)
})
test('should start the generator at the "Start" node if startAt is undefined', () => {
new YarnBound({})
expect(bondage.Runner.prototype.run).toHaveBeenCalledWith('Start')
})
test('should attach the generator to the instance', () => {
const runner = new YarnBound({})
expect(runner.generator).toEqual(bondage.Runner.prototype.run.mock.results[0].value)
})
test('should advance the generator', () => {
jest.spyOn(YarnBound.prototype, 'advance')
new YarnBound({})
expect(YarnBound.prototype.advance).toHaveBeenCalled()
})
})
describe('jump', () => {
test('should jump the generator to the node with the provided "jumpTo" title', () => {
jest.spyOn(YarnBound.prototype, 'advance')
const jumpTo = 'someJumpNode'
const yarnbound = new YarnBound({})
yarnbound.jump(jumpTo)
expect(YarnBound.prototype.advance).toHaveBeenCalled()
expect(bondage.Runner.prototype.run).toHaveBeenCalledWith(jumpTo)
})
})
describe('advance', () => {
const mockCommandName1 = 'blah'
const mockCommandName2 = 'bleh'
const mockCommandResult1 = new bondage.CommandResult(mockCommandName1)
const mockCommandResult2 = new bondage.CommandResult(mockCommandName2)
const mockTextResult1 = new bondage.TextResult('marge')
const mockTextResult2 = new bondage.TextResult('maggie')
const mockTextResult3 = new bondage.TextResult('homer')
const mockOptionsResult = new bondage.OptionsResult([
{ text: 'bart' },
{ text: 'lisa' }
])
describe('where next results are a TextResult followed by OptionsResult', () => {
beforeAll(() => {
bondage.Runner.prototype.run.mockImplementation(getMockGenerator([
mockTextResult1,
mockOptionsResult,
mockTextResult2,
mockTextResult3
]))
})
test('should set currentResult to a the TextResult object', () => {
const runner = new YarnBound({})
expect(runner.currentResult).toBe(mockTextResult1)
runner.advance()
expect(runner.currentResult).toBe(mockOptionsResult)
})
test('should attach a markup array to a the TextResult object', () => {
const runner = new YarnBound({})
expect(runner.currentResult.markup).toEqual([])
})
test('should set currentResult to an Options object with the text attached if combineTextAndOptionsResults is false', () => {
const runner = new YarnBound({ combineTextAndOptionsResults: true })
expect(runner.currentResult).toEqual({ ...mockOptionsResult, ...mockTextResult1 })
expect(runner.currentResult).toBeInstanceOf(bondage.OptionsResult)
})
test('should select the option with the index passed in, if there is one', () => {
const runner = new YarnBound({})
expect(runner.currentResult).toBe(mockTextResult1)
runner.advance()
const currentResult = runner.currentResult
expect(runner.currentResult).toBe(mockOptionsResult)
jest.spyOn(currentResult, 'select')
runner.advance(1)
expect(currentResult.select).toHaveBeenCalledWith(1)
expect(runner.currentResult).toBe(mockTextResult2)
})
test('should set currentResult to a the TextResult object', () => {
const runner = new YarnBound({})
expect(runner.currentResult).toBe(mockTextResult1)
runner.advance()
expect(runner.currentResult).toBe(mockOptionsResult)
})
})
describe('where next results are CommandResults followed by TextResults', () => {
beforeAll(() => {
bondage.Runner.prototype.run.mockImplementation(getMockGenerator([
mockCommandResult1,
mockCommandResult2,
mockTextResult1,
mockTextResult2
]))
})
test('should set currentResult to the command result if handleCommand is not supplied', () => {
const runner = new YarnBound({})
expect(runner.currentResult).toBe(mockCommandResult1)
runner.advance()
expect(runner.currentResult).toBe(mockCommandResult2)
})
test('should add previous results to history', () => {
const runner = new YarnBound({})
expect(runner.currentResult).toBe(mockCommandResult1)
runner.advance()
expect(runner.history).toEqual([mockCommandResult1])
runner.advance()
expect(runner.history).toEqual([mockCommandResult1, mockCommandResult2])
runner.advance()
expect(runner.history).toEqual([mockCommandResult1, mockCommandResult2, mockTextResult1])
})
test('should not add command results to history if handleCommand is supplied', () => {
const runner = new YarnBound({ handleCommand: () => {} })
expect(runner.currentResult).toBe(mockTextResult1)
runner.advance()
expect(runner.history).toEqual([mockTextResult1])
})
test('should set currentResult to the next non-command result if handleCommand is supplied', () => {
const runner = new YarnBound({ handleCommand: () => {} })
expect(runner.currentResult).toBe(mockTextResult1)
})
test('should call the command handler for each command result', () => {
const handleCommand = jest.fn()
new YarnBound({ handleCommand })
expect(handleCommand).toHaveBeenNthCalledWith(1, mockCommandResult1)
expect(handleCommand).toHaveBeenNthCalledWith(2, mockCommandResult2)
})
})
describe('when dialogue ends', () => {
test('should include an "isDialogueEnd" property on the last result if it is a text result', () => {
bondage.Runner.prototype.run.mockImplementation(getMockGenerator([
mockTextResult1
]))
const runner = new YarnBound({})
expect(runner.currentResult).toEqual({ ...mockTextResult1, isDialogueEnd: true })
})
test('should include an "isDialogueEnd" property on the last result if it is a command result', () => {
bondage.Runner.prototype.run.mockImplementation(getMockGenerator([
mockTextResult1,
mockCommandResult1
]))
const runner = new YarnBound({})
expect(runner.currentResult).toEqual(mockTextResult1)
runner.advance()
expect(runner.currentResult).toEqual({ ...mockCommandResult1, isDialogueEnd: true })
})
test('should include an "isDialogueEnd" property on the last text result before only command results remain, if handleCommand is supplied', () => {
bondage.Runner.prototype.run.mockImplementation(getMockGenerator([
mockTextResult1,
mockCommandResult1,
mockCommandResult2
]))
const handleCommand = jest.fn()
const runner = new YarnBound({ handleCommand })
expect(runner.currentResult).toEqual({ ...mockTextResult1, isDialogueEnd: true })
})
test('should handle trailing and leading commands', () => {
bondage.Runner.prototype.run.mockImplementation(getMockGenerator([
mockCommandResult1,
mockTextResult1,
mockCommandResult2
]))
const handleCommand = jest.fn()
const runner = new YarnBound({ handleCommand })
expect(runner.currentResult).toEqual({ ...mockTextResult1, isDialogueEnd: true })
expect(handleCommand).toHaveBeenNthCalledWith(1, mockCommandResult1)
expect(handleCommand).toHaveBeenNthCalledWith(2, mockCommandResult2)
})
test('should not handle commands prematurely', () => {
bondage.Runner.prototype.run.mockImplementation(getMockGenerator([
mockCommandResult1,
mockTextResult1,
mockCommandResult2,
mockTextResult2
]))
const handleCommand = jest.fn()
const runner = new YarnBound({ handleCommand })
expect(runner.currentResult).toEqual({ ...mockTextResult1, isDialogueEnd: true })
expect(handleCommand).toHaveBeenCalledTimes(1)
expect(handleCommand).toHaveBeenCalledWith(mockCommandResult1)
runner.advance()
expect(handleCommand).toHaveBeenCalledTimes(2)
expect(handleCommand).toHaveBeenCalledWith(mockCommandResult2)
})
})
})