Last active
October 28, 2015 23:38
-
-
Save Papipo/1e5ebb3e6260e8c1fb9c to your computer and use it in GitHub Desktop.
Testing cached m.request
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cache = []; | |
model.latest = function() { | |
makeRequest().then(replace); // If I put this on the function below, the tests still pass | |
return function() { // but when using mithril, I would get infinite requests | |
return cache; | |
}; | |
}; | |
function makeRequest() { | |
return request({method: "GET", url: url, type: model}); | |
} | |
function replace(list) { | |
if (list.length) { | |
cache = list; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("latest()", function() { | |
it("returns an empty dataset, then populates and caches it", function() { | |
var model = modelFactory("/someurl", []); | |
var expected = [{id: 123}]; | |
var firstResponse = mithril.deferred(); | |
var furtherResponses = mithril.deferred(); | |
requestStub | |
.withArgs({method: "GET", url: "/someurl", type: model}) | |
.onFirstCall() | |
.returns(firstResponse.promise); | |
requestStub | |
.withArgs({method: "GET", url: "/someurl", type: model}) | |
.returns(furtherResponses.promise); | |
var latest = model.latest(); | |
assert.deepEqual(latest(), []); | |
firstResponse.resolve(expected); | |
assert.deepEqual(latest(), expected); | |
furtherResponses.resolve([]); | |
assert.deepEqual(latest(), expected); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment