-
Notifications
You must be signed in to change notification settings - Fork 1
/
mocha-test.js
224 lines (200 loc) · 7.89 KB
/
mocha-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
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
var expect = chai.expect
describe('smoax', function() {
beforeEach(function() {
chai.use(smoax.setup())
})
afterEach(function() {
smoax.release()
})
it('gets initialised', function() {
expect(smoax).not.to.have.beenInvoked()
expect(smoax).not.to.have.beenInvokedWith('post', '/url', {data:false})
})
it('handles $.get', function() {
smoax.register('get', '/url', { data: "yeah" })
var gotSyncCall = false
$.get('/url', function(resp) {
gotSyncCall = true
expect(resp.data).to.equal('yeah')
}, 'json')
expect(gotSyncCall).to.equal(true)
expect(smoax).to.have.beenInvoked()
expect(smoax).to.have.beenInvokedWith('get', '/url')
expect(smoax).not.to.have.beenInvokedWith('get', '/urlX')
})
it('handles $.post', function() {
smoax.register('post', '/url', { data: "yeah" })
$.post('/url', { request: true }, function(resp) {
expect(resp.data).to.equal('yeah')
expect(smoax).to.have.beenInvokedWith('post', '/url', { request:true })
expect(smoax).not.to.have.beenInvokedWith('post', '/url', { request:false })
}, 'json')
})
it('handles generic ajax response', function(done) {
smoax.register('pow!')
$.get('/url').success(function(resp) {
expect(resp).to.equal('pow!')
$.post('/url').success(function(resp) {
expect(resp).to.equal('pow!')
expect(smoax).to.have.beenInvokedWith('get', '/url')
expect(smoax).to.have.beenInvokedWith('post', '/url')
done()
})
})
})
it('handles error callback', function() {
smoax.registerError('get', '/url', 404, 'notfound')
var successGotCalled = false
var errorGotCalled = false
$.ajax({ type: 'get', url: '/url',
success: function() { successGotCalled = true },
error: function(xhr, statusText, errorThrown) {
errorGotCalled = true
expect(xhr.status).to.equal(404)
expect(statusText).to.equal('error')
expect(errorThrown).to.equal('notfound')
}
})
expect(successGotCalled).to.equal(false)
expect(errorGotCalled).to.equal(true)
expect(smoax).to.have.beenInvoked()
expect(smoax).to.have.beenInvokedWith('get', '/url')
})
it('handles generic ajax error response', function(done) {
smoax.registerError(418, 'teapot!')
$.get('/url').error(function(xhr, txt, err) {
expect(xhr.status).to.equal(418)
expect(txt).to.equal('error')
expect(err).to.equal('teapot!')
$.post('/other-url').error(function(xhr, txt, err) {
expect(xhr.status).to.equal(418)
expect(txt).to.equal('error')
expect(err).to.equal('teapot!')
expect(smoax).to.have.beenInvokedWith('get', '/url')
expect(smoax).to.have.beenInvokedWith('post', '/other-url')
done()
})
})
})
it('handles multiple deferred success callbacks', function() {
smoax.register('get', '/url', { data: "yeah" })
var calls = 0
$.get('/url')
.success(function() { calls++ })
.success(function() { calls++ })
expect(calls).to.equal(2)
})
it('handles multiple deferred error callbacks', function() {
smoax.registerError('get', '/url', 404, 'notfound')
var calls = 0
$.get('/url')
.error(function() { calls++ })
.error(function() { calls++ })
expect(calls).to.equal(2)
})
it('registers calls and call count', function() {
smoax.register('get', '/url', { data: "yeah" })
expect(smoax.calls.map).to.eql({})
$.get('/url', function() {})
expect(smoax.calls.map['GET /url']).to.exist
expect(smoax.calls.count).to.equal(1)
})
it('keeps track of latest ajax call', function() {
$.get('/url', { 'im':'latest' }, function() {})
expect(smoax.calls.latest).to.exist
expect(smoax.calls.latest.type).to.equal('GET')
expect(smoax.calls.latest.url).to.equal('/url?im=latest')
})
it('can also play async', function(done) {
smoax.registerAsync('get', '/url', { data: "yeah" })
var pre = (new Date()).getTime()
$.get('/url', function(resp) {
var duration = (new Date()).getTime() - pre
expect(duration).to.be.below(20)
expect(resp.data).to.equal('yeah')
done()
}, 'json')
})
it('can delay when async', function(done) {
smoax.registerAsync('get', '/url', 'slowly', 50)
var pre = (new Date()).getTime()
$.get('/url', function(resp) {
var duration = (new Date()).getTime() - pre
expect(duration).to.be.above(50)
expect(resp).to.equal('slowly')
done()
})
})
it('can use async errors', function(done) {
smoax.registerAsyncError('get', '/teapot', 418, "I'm a teapot", {}, 50)
var pre = (new Date()).getTime()
$.get('/teapot').error(function(xhr, statusText, errorThrown) {
var duration = (new Date()).getTime() - pre
expect(duration).to.be.above(50)
expect(xhr.status).to.equal(418)
expect(statusText).to.equal('error')
expect(errorThrown).to.equal("I'm a teapot")
done()
})
})
it('exposes real ajax', function() {
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1 && document.location.protocol == 'file:') {
smoax.warn('This test fails with google-chrome if not started with --allow-file-access-from-files')
}
var response = smoax.ajax({type:'get', url:'../test/jasmine-test.js', async:false})
expect(response.responseText).to.match(/^describe\('smoax/)
})
it('can use a string response body', function() {
smoax.register('get', '/url', 'foo')
$.get('/url').success(function(data) { expect(data).to.equal('foo') })
expect(smoax.calls.count).to.equal(1)
})
it('can use a function response body', function() {
smoax.register('get', '/url', function() { return 'foo' })
$.get('/url').success(function(data) { expect(data).to.equal('foo') })
expect(smoax.calls.count).to.equal(1)
})
it('can assert all called ajaxes', function() {
$.get('/url')
$.get('/url', { key:'value' })
$.get('/url?foo=bar')
expect(smoax).to.have.beenInvokedWith('get', '/url')
expect(smoax).to.have.beenInvokedWith('get', '/url?key=value')
expect(smoax).to.have.beenInvokedWith('get', '/url?foo=bar')
})
it('cant handle get data as object', function() {
$.get('/url', { key:'value' })
expect(smoax).not.to.have.beenInvokedWith('get', '/url', { key:'value' })
})
it('can assert latest ajax', function() {
$.post('/first', { foo:'bar' })
$.post('/second', { data:'ok' })
expect(smoax).latestInvocationToHaveBeen('POST', '/second', { data:'ok'})
$.get('/third')
expect(smoax).latestInvocationToHaveBeen('GET', '/third')
})
it('uses readable error messages when method and url doesnt match expected', function() {
$.get('/first')
$.post('/second')
var assertion = undefined
var obj = { assert: function(ok, m1, m2) { assertion = [ok, m1, m2] }}
chai.Assertion.prototype.beenInvokedWith.call(obj, 'get', '/second')
expect(assertion[0]).to.be.false
expect(assertion[1]).to.equal('Expected GET /second to have been invoked. There have been calls to: GET /first, POST /second')
expect(assertion[2]).to.equal('Expected GET /second not to have been invoked. There have been calls to: GET /first, POST /second')
})
it('uses readable error messages when data doesnt match expected', function() {
$.post('/second', { data:'ok' })
var assertion = undefined
var obj = { assert: function(ok, m1, m2) { assertion = [ok, m1, m2] }}
chai.Assertion.prototype.beenInvokedWith.call(obj, 'post', '/second', { data:'fail' })
expect(assertion[0]).to.be.false
expect(assertion[1]).to.equal("Expected POST /second to have been invoked with 'data=fail' but was invoked with 'data=ok'")
expect(assertion[2]).to.equal("Expected POST /second not to have been invoked with 'data=fail'")
})
it('releases $.ajax', function() {
expect($.ajax.toString()).to.have.length.below(200)
smoax.release()
expect($.ajax.toString()).to.have.length.above(3000)
})
})