/
test.js
73 lines (61 loc) · 1.63 KB
/
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
73
var test = require('tape')
var createGame = require('./index')
test('create a game loop', function (t) {
var game = createGame({ renderer: {} })
t.ok(game)
t.end()
})
test('start game and use arbitrary function as renderer', function (t) {
var game = createGame({ renderer: function (a) { return a } })
game.once('draw', function (renderer) {
var ok = renderer('ok')
t.equal(ok, 'ok')
t.end()
})
game.start()
})
test('update event emits interval', function (t) {
var game = createGame({ renderer: {} })
game.once('update', function (interval, time) {
t.ok(interval)
t.end()
})
game.start()
})
test('update event emits correct total time elapsed for default fps value', function (t) {
var game = createGame()
var maxNumberOfUpdates = 4
var expectedTimeInSeconds = maxNumberOfUpdates * game.step
game.on('update', function (interval, time) {
if (--maxNumberOfUpdates <= 0) {
game.pause()
t.equal(time, expectedTimeInSeconds)
t.end()
}
})
game.start()
})
test('update event emits correct total time elapsed for custom fps value', function (t) {
var game = createGame({ fps: 20 })
var maxNumberOfUpdates = 4
var expectedTimeInSeconds = maxNumberOfUpdates * game.step
game.on('update', function (interval, time) {
if (--maxNumberOfUpdates <= 0) {
game.pause()
t.equal(time, expectedTimeInSeconds)
t.end()
}
})
game.start()
})
test('end game', function (t) {
var game = createGame({ renderer: {} })
game.on('end', function (data) {
t.ok(data.cool)
t.end()
process.exit()
})
game.start()
game.end({ cool: true })
t.end()
})