change m.request return value from stream to promise

remove m.prop
add m.Promise
update tests and examples
This commit is contained in:
Leo Horie 2016-11-13 22:44:22 -05:00
parent 8f1a69cfcb
commit bc8cf4ed76
19 changed files with 650 additions and 852 deletions

View file

@ -3,14 +3,13 @@
var o = require("../../ospec/ospec")
var xhrMock = require("../../test-utils/xhrMock")
var Request = require("../../request/request")
var StreamFactory = require("../../util/stream")
var Promise = require("../../promise/promise")
o.spec("xhr", function() {
var mock, xhr, spy
var mock, xhr
o.beforeEach(function() {
mock = xhrMock()
spy = o.spy()
xhr = new Request(mock, StreamFactory(spy)).request
xhr = new Request(mock, Promise).request
})
o.spec("success", function() {
@ -21,9 +20,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr({method: "GET", url: "/item"}).run(function(data) {
xhr({method: "GET", url: "/item"}).then(function(data) {
o(data).deepEquals({a: 1})
}).run(function() {
}).then(function() {
done()
})
})
@ -34,9 +33,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr({url: "/item"}).run(function(data) {
xhr({url: "/item"}).then(function(data) {
o(data).deepEquals({a: 1})
}).run(function() {
}).then(function() {
done()
})
})
@ -47,9 +46,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr("/item").run(function(data) {
xhr("/item").then(function(data) {
o(data).deepEquals({a: 1})
}).run(function() {
}).then(function() {
done()
})
})
@ -59,9 +58,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr({method: "POST", url: "/item"}).run(function(data) {
xhr({method: "POST", url: "/item"}).then(function(data) {
o(data).deepEquals({a: 1})
}).run(done)
}).then(done)
})
o("first argument can act as URI with second argument providing options", function(done) {
mock.$defineRoutes({
@ -69,9 +68,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: 1})}
}
})
xhr("/item", {method: "POST"}).run(function(data) {
xhr("/item", {method: "POST"}).then(function(data) {
o(data).deepEquals({a: 1})
}).run(done)
}).then(done)
})
o("works w/ parameterized data via GET", function(done) {
mock.$defineRoutes({
@ -79,9 +78,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.query})}
}
})
xhr({method: "GET", url: "/item", data: {x: "y"}}).run(function(data) {
xhr({method: "GET", url: "/item", data: {x: "y"}}).then(function(data) {
o(data).deepEquals({a: "?x=y"})
}).run(done)
}).then(done)
})
o("works w/ parameterized data via POST", function(done) {
mock.$defineRoutes({
@ -89,9 +88,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: JSON.parse(request.body)})}
}
})
xhr({method: "POST", url: "/item", data: {x: "y"}}).run(function(data) {
xhr({method: "POST", url: "/item", data: {x: "y"}}).then(function(data) {
o(data).deepEquals({a: {x: "y"}})
}).run(done)
}).then(done)
})
o("works w/ parameterized data containing colon via GET", function(done) {
mock.$defineRoutes({
@ -99,9 +98,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.query})}
}
})
xhr({method: "GET", url: "/item", data: {x: ":y"}}).run(function(data) {
xhr({method: "GET", url: "/item", data: {x: ":y"}}).then(function(data) {
o(data).deepEquals({a: "?x=%3Ay"})
}).run(done)
}).then(done)
})
o("works w/ parameterized data containing colon via POST", function(done) {
mock.$defineRoutes({
@ -109,9 +108,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: JSON.parse(request.body)})}
}
})
xhr({method: "POST", url: "/item", data: {x: ":y"}}).run(function(data) {
xhr({method: "POST", url: "/item", data: {x: ":y"}}).then(function(data) {
o(data).deepEquals({a: {x: ":y"}})
}).run(done)
}).then(done)
})
o("works w/ parameterized url via GET", function(done) {
mock.$defineRoutes({
@ -119,9 +118,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.url, b: request.query})}
}
})
xhr({method: "GET", url: "/item/:x", data: {x: "y"}}).run(function(data) {
xhr({method: "GET", url: "/item/:x", data: {x: "y"}}).then(function(data) {
o(data).deepEquals({a: "/item/y", b: {}})
}).run(done)
}).then(done)
})
o("works w/ parameterized url via POST", function(done) {
mock.$defineRoutes({
@ -129,9 +128,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.url, b: JSON.parse(request.body)})}
}
})
xhr({method: "POST", url: "/item/:x", data: {x: "y"}}).run(function(data) {
xhr({method: "POST", url: "/item/:x", data: {x: "y"}}).then(function(data) {
o(data).deepEquals({a: "/item/y", b: {}})
}).run(done)
}).then(done)
})
o("ignores unresolved parameter via GET", function(done) {
mock.$defineRoutes({
@ -139,9 +138,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.url})}
}
})
xhr({method: "GET", url: "/item/:x"}).run(function(data) {
xhr({method: "GET", url: "/item/:x"}).then(function(data) {
o(data).deepEquals({a: "/item/:x"})
}).run(done)
}).then(done)
})
o("ignores unresolved parameter via POST", function(done) {
mock.$defineRoutes({
@ -149,9 +148,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({a: request.url})}
}
})
xhr({method: "GET", url: "/item/:x"}).run(function(data) {
xhr({method: "GET", url: "/item/:x"}).then(function(data) {
o(data).deepEquals({a: "/item/:x"})
}).run(done)
}).then(done)
})
o("type parameter works for Array responses", function(done) {
var Entity = function(args) {
@ -163,9 +162,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify([{id: 1}, {id: 2}, {id: 3}])}
}
})
xhr({method: "GET", url: "/item", type: Entity}).run(function(data) {
xhr({method: "GET", url: "/item", type: Entity}).then(function(data) {
o(data).deepEquals([{_id: 1}, {_id: 2}, {_id: 3}])
}).run(done)
}).then(done)
})
o("type parameter works for Object responses", function(done) {
var Entity = function(args) {
@ -177,9 +176,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({id: 1})}
}
})
xhr({method: "GET", url: "/item", type: Entity}).run(function(data) {
xhr({method: "GET", url: "/item", type: Entity}).then(function(data) {
o(data).deepEquals({_id: 1})
}).run(done)
}).then(done)
})
o("serialize parameter works in GET", function(done) {
var serialize = function(data) {
@ -191,9 +190,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({body: request.query})}
}
})
xhr({method: "GET", url: "/item", serialize: serialize, data: {id: 1}}).run(function(data) {
xhr({method: "GET", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) {
o(data.body).equals("?id=1")
}).run(done)
}).then(done)
})
o("serialize parameter works in POST", function(done) {
var serialize = function(data) {
@ -205,9 +204,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({body: request.body})}
}
})
xhr({method: "POST", url: "/item", serialize: serialize, data: {id: 1}}).run(function(data) {
xhr({method: "POST", url: "/item", serialize: serialize, data: {id: 1}}).then(function(data) {
o(data.body).equals("id=1")
}).run(done)
}).then(done)
})
o("deserialize parameter works in GET", function(done) {
var deserialize = function(data) {
@ -219,9 +218,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({test: 123})}
}
})
xhr({method: "GET", url: "/item", deserialize: deserialize}).run(function(data) {
xhr({method: "GET", url: "/item", deserialize: deserialize}).then(function(data) {
o(data).equals("{\"test\":123}")
}).run(done)
}).then(done)
})
o("deserialize parameter works in POST", function(done) {
var deserialize = function(data) {
@ -233,9 +232,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: JSON.stringify({test: 123})}
}
})
xhr({method: "POST", url: "/item", deserialize: deserialize}).run(function(data) {
xhr({method: "POST", url: "/item", deserialize: deserialize}).then(function(data) {
o(data).equals("{\"test\":123}")
}).run(done)
}).then(done)
})
o("extract parameter works in GET", function(done) {
var extract = function(data) {
@ -247,9 +246,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: ""}
}
})
xhr({method: "GET", url: "/item", extract: extract}).run(function(data) {
xhr({method: "GET", url: "/item", extract: extract}).then(function(data) {
o(data).equals("{\"test\":123}")
}).run(done)
}).then(done)
})
o("extract parameter works in POST", function(done) {
var extract = function(data) {
@ -261,9 +260,9 @@ o.spec("xhr", function() {
return {status: 200, responseText: ""}
}
})
xhr({method: "POST", url: "/item", extract: extract}).run(function(data) {
xhr({method: "POST", url: "/item", extract: extract}).then(function(data) {
o(data).equals("{\"test\":123}")
}).run(done)
}).then(done)
})
o("ignores deserialize if extract is defined", function(done) {
var extract = function(data) {
@ -276,11 +275,11 @@ o.spec("xhr", function() {
return {status: 200, responseText: ""}
}
})
xhr({method: "GET", url: "/item", extract: extract, deserialize: deserialize}).run(function(data) {
xhr({method: "GET", url: "/item", extract: extract, deserialize: deserialize}).then(function(data) {
o(data).equals(200)
}).run(function() {
}).then(function() {
o(deserialize.callCount).equals(0)
}).run(done)
}).then(done)
})
o("config parameter works", function(done) {
mock.$defineRoutes({
@ -288,7 +287,7 @@ o.spec("xhr", function() {
return {status: 200, responseText: ""}
}
})
xhr({method: "POST", url: "/item", config: config}).run(done)
xhr({method: "POST", url: "/item", config: config}).then(done)
function config(xhr) {
o(typeof xhr.setRequestHeader).equals("function")
@ -296,16 +295,6 @@ o.spec("xhr", function() {
o(typeof xhr.send).equals("function")
}
})
o("initialValue parameter works", function() {
mock.$defineRoutes({
"GET /items": function() {
return {status: 200, responseText: JSON.stringify([{a: 1}])}
}
})
var items = xhr({method: "GET", url: "/items", initialValue: []})
o(items()).deepEquals([])
})
})
o.spec("failure", function() {
o("rejects on server error", function(done) {
@ -317,7 +306,7 @@ o.spec("xhr", function() {
xhr({method: "GET", url: "/item"}).catch(function(e) {
o(e instanceof Error).equals(true)
o(e.message).equals(JSON.stringify({error: "error"}))
}).run(done)
}).then(done)
})
o("extends Error with JSON response", function(done) {
mock.$defineRoutes({
@ -329,7 +318,7 @@ o.spec("xhr", function() {
o(e instanceof Error).equals(true)
o(e.message).equals("error")
o(e.stack).equals("error on line 1")
}).run(done)
}).then(done)
})
o("rejects on non-JSON server error", function(done) {
mock.$defineRoutes({
@ -339,7 +328,7 @@ o.spec("xhr", function() {
})
xhr({method: "GET", url: "/item"}).catch(function(e) {
o(e.message).equals("error")
}).run(done)
}).then(done)
})
})
})