Skip to content

Instantly share code, notes, and snippets.

@Papipo
Last active October 28, 2015 23:38
Show Gist options
  • Save Papipo/1e5ebb3e6260e8c1fb9c to your computer and use it in GitHub Desktop.
Save Papipo/1e5ebb3e6260e8c1fb9c to your computer and use it in GitHub Desktop.
Testing cached m.request
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;
}
}
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