This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
delayer_spec.coffee
123 lines (109 loc) · 4.01 KB
/
delayer_spec.coffee
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
define [
'underscore'
'chaplin/lib/delayer'
], (_, Delayer) ->
'use strict'
describe 'Delayer', ->
# Set up an object which mixes in Delayer
delayer = {}
_.extend delayer, Delayer
it 'should be a simple object', ->
expect(Delayer).to.be.an 'object'
it 'should allow to set a timeout ', (done) ->
expect(delayer.setTimeout).to.be.a 'function'
handle = delayer.setTimeout 'foo', 1, ->
done()
expect(handle).to.be.a 'number'
it 'should create a timeout handle store', (done) ->
expect(delayer.timeouts).to.be.an 'object'
delayer.setTimeout 'foo', 1, ->
done()
expect(delayer.timeouts.foo).to.be.a 'number'
it 'should set multiple timeouts with different name', (done) ->
spy1 = sinon.spy()
spy2 = sinon.spy()
delayer.setTimeout 'foo', 1, spy1
delayer.setTimeout 'bar', 1, spy2
setTimeout ->
expect(spy1).was.called()
expect(spy2).was.called()
done()
, 1
it 'should not set a timeout twice', (done) ->
spy1 = sinon.spy()
spy2 = sinon.spy()
delayer.setTimeout 'foo', 1, spy1
delayer.setTimeout 'foo', 1, spy2
setTimeout ->
expect(spy1).was.notCalled()
expect(spy2).was.called()
done()
, 1
it 'should remove called timeouts', ->
expect(delayer.timeouts.foo).to.be undefined
it 'should allow to clear a timeout', (done) ->
spy = sinon.spy()
delayer.setTimeout 'foo', 1, spy
expect(delayer.clearTimeout).to.be.a 'function'
delayer.clearTimeout 'foo'
setTimeout (->
expect(spy).was.notCalled()
done()
), 1
it 'should clear all timeouts', (done) ->
spy1 = sinon.spy()
spy2 = sinon.spy()
delayer.setTimeout 'foo', 1, spy1
delayer.setTimeout 'bar', 1, spy2
expect(delayer.clearAllTimeouts).to.be.a 'function'
delayer.clearAllTimeouts()
setTimeout ->
expect(spy1).was.notCalled()
expect(spy2).was.notCalled()
done()
, 1
it 'should allow to set and get an interval', ->
spy = sinon.spy()
setIntervalStub = sinon.stub(window, 'setInterval').callsArg(0).returns(12345)
expect(delayer.setInterval).to.be.a 'function'
handle = delayer.setInterval 'foo', 50, spy
expect(handle).to.be 12345
expect(setIntervalStub).was.called()
expect(spy).was.called()
setIntervalStub.restore()
it 'should create a interval handle store', ->
setIntervalStub = sinon.stub(window, 'setInterval').returns(12345)
expect(delayer.intervals).to.be.an 'object'
delayer.setInterval 'foo', 1, ->
expect(delayer.intervals.foo).to.be 12345
setIntervalStub.restore()
it 'should allow to clear an interval', ->
spy = sinon.spy()
clearIntervalStub = sinon.stub window, 'clearInterval'
handle = delayer.setInterval 'foo', 1, spy
expect(delayer.clearInterval).to.be.a 'function'
delayer.clearInterval 'foo'
expect(clearIntervalStub).was.calledWith handle
clearIntervalStub.restore()
it 'should clear all timeouts', ->
i = 0
setIntervalStub = sinon.stub window, 'setInterval', -> ++i
clearIntervalStub = sinon.stub window, 'clearInterval'
handle1 = delayer.setInterval 'foo', 1, ->
handle2 = delayer.setInterval 'bar', 1, ->
expect(delayer.clearAllIntervals).to.be.a 'function'
delayer.clearAllIntervals()
expect(clearIntervalStub.callCount).to.be 2
expect(clearIntervalStub.getCall(0).args[0]).to.be handle1
expect(clearIntervalStub.getCall(1).args[0]).to.be handle2
setIntervalStub.restore()
clearIntervalStub.restore()
it 'should clear all timeouts and intervals', ->
stub1 = sinon.stub delayer, 'clearAllTimeouts'
stub2 = sinon.stub delayer, 'clearAllIntervals'
expect(delayer.clearDelayed).to.be.a 'function'
delayer.clearDelayed()
expect(stub1).was.called()
expect(stub2).was.called()
stub1.restore()
stub2.restore()