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() {
|
||||
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.$headers[key] = value
|
||||
}
|
||||
|
|
@ -237,9 +245,6 @@ this.mock = (function (global) {
|
|||
}
|
||||
|
||||
this.send = function () {
|
||||
this.responseText = JSON.stringify(this)
|
||||
this.readyState = 4
|
||||
this.status = 200
|
||||
Request.$instances.push(this)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -648,7 +648,7 @@ describe("m.mount()", function () {
|
|||
})
|
||||
|
||||
function resolveXhr() {
|
||||
mock.XMLHttpRequest.$instances.pop().onreadystatechange()
|
||||
mock.XMLHttpRequest.$instances.pop().$resolve().onreadystatechange()
|
||||
mock.requestAnimationFrame.$resolve()
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ describe("m.request()", function () {
|
|||
// Much easier to read
|
||||
function resolve() {
|
||||
var xhr = mock.XMLHttpRequest.$instances.pop()
|
||||
xhr.$resolve.apply(xhr, arguments)
|
||||
xhr.onreadystatechange()
|
||||
return xhr
|
||||
}
|
||||
|
|
@ -381,4 +382,21 @@ describe("m.request()", function () {
|
|||
// For good measure
|
||||
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