Fix inflexible/slightly broken m.request tests
This commit is contained in:
parent
7f7a0a3815
commit
c21566366e
3 changed files with 27 additions and 4 deletions
|
|
@ -227,6 +227,14 @@ this.mock = (function (global) {
|
||||||
function Request() {
|
function Request() {
|
||||||
this.$headers = {}
|
this.$headers = {}
|
||||||
|
|
||||||
|
this.$resolve = function (data, status) {
|
||||||
|
if (data === undefined) data = this // eslint-disable-line
|
||||||
|
this.responseText = JSON.stringify(data)
|
||||||
|
this.readyState = 4
|
||||||
|
this.status = status || 200
|
||||||
|
return this
|
||||||
|
}
|
||||||
|
|
||||||
this.setRequestHeader = function (key, value) {
|
this.setRequestHeader = function (key, value) {
|
||||||
this.$headers[key] = value
|
this.$headers[key] = value
|
||||||
}
|
}
|
||||||
|
|
@ -237,9 +245,6 @@ this.mock = (function (global) {
|
||||||
}
|
}
|
||||||
|
|
||||||
this.send = function () {
|
this.send = function () {
|
||||||
this.responseText = JSON.stringify(this)
|
|
||||||
this.readyState = 4
|
|
||||||
this.status = 200
|
|
||||||
Request.$instances.push(this)
|
Request.$instances.push(this)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -648,7 +648,7 @@ describe("m.mount()", function () {
|
||||||
})
|
})
|
||||||
|
|
||||||
function resolveXhr() {
|
function resolveXhr() {
|
||||||
mock.XMLHttpRequest.$instances.pop().onreadystatechange()
|
mock.XMLHttpRequest.$instances.pop().$resolve().onreadystatechange()
|
||||||
mock.requestAnimationFrame.$resolve()
|
mock.requestAnimationFrame.$resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ describe("m.request()", function () {
|
||||||
// Much easier to read
|
// Much easier to read
|
||||||
function resolve() {
|
function resolve() {
|
||||||
var xhr = mock.XMLHttpRequest.$instances.pop()
|
var xhr = mock.XMLHttpRequest.$instances.pop()
|
||||||
|
xhr.$resolve.apply(xhr, arguments)
|
||||||
xhr.onreadystatechange()
|
xhr.onreadystatechange()
|
||||||
return xhr
|
return xhr
|
||||||
}
|
}
|
||||||
|
|
@ -381,4 +382,21 @@ describe("m.request()", function () {
|
||||||
// For good measure
|
// For good measure
|
||||||
mock.requestAnimationFrame.$resolve()
|
mock.requestAnimationFrame.$resolve()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("can use a config correctly", function () {
|
||||||
|
var config = sinon.spy()
|
||||||
|
var result = m.prop()
|
||||||
|
var error = sinon.spy
|
||||||
|
var opts = {
|
||||||
|
method: "GET",
|
||||||
|
url: "/test",
|
||||||
|
config: config
|
||||||
|
}
|
||||||
|
m.request(opts).then(result, error)
|
||||||
|
var xhr = resolve({foo: "bar"})
|
||||||
|
|
||||||
|
expect(config).to.be.calledWithExactly(xhr, opts)
|
||||||
|
expect(result()).to.eql({foo: "bar"})
|
||||||
|
expect(error).to.not.be.called
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue