How can I unit test the m.request model in Mithril?
I am trying to unit test Mithril's model using m.request
DOM-less framework .
For me this test works as an integration test in a browser environment using the browser XMLHttpRequest, but he would like this option to run in isolation.
I'm considering mixing up the XMLHttpRequest response to get it properly initialized m.request
, but I'm not sure where to start. I have a naive XMLHttpRequest implementation banished from a test and looked into the source m.request
, but as a relative JS newbie, it's hard to follow.
Does this mean it makes sense to completely disable m.request
it just to test the transformation, since I trust Mithril is working (and is technically a dependency of the device under test)? This scares me a little as it m.request
has chaining behavior that can be tricky to stub.
I would happily agree with an answer, which usually outlines the steps I need to take to make some progress on this issue and / or some advice on what makes sense to test.
require('chai').should();
require('mithril');
m.deps({ XMLHttpRequest: function() {
this.open = function() {
}
this.setRequestHeader = function() {
}
this.send = function() {
}
}});
var Curriculum = require('../../../app/modules/practice/practice.curriculum');
describe('Curriculum', function() {
it('can retrieve a list of modules', function(done) {
Curriculum.modules().then(function(modules) {
modules.should.deep.equal([
{ name: 'Module 1' },
{ name: 'Module 2' },
{ name: 'Module 3' }
]);
done();
});
});
});
Currently running this test s is being mocha
played uselessly with no output or errors.
Source of the device under test, if useful:
module.exports = {
modules: function() {
// URL obscured to protect the innocent.
return m.request({
method: 'GET',
url: 'http://working.url'
}).then(function(objects) {
var transformed = objects.map(function(object) {
return { name: object.name };
});
return transformed;
});
}
};
You can refer to Mithril's own test suite to see how it tests itselfm.request
The mock object that is used in these tests can be found here