Reputation: 2129
I have a test case which goes something like that:
const css = require('../css');
it('should include css', function(){
var document = jsdom('<html><head></head></html>');
return css('/bootstrap.min.css');
});
The actual css module looks like:
module.exports = function(url){
return new Promise(function(resolve){
const link = document.createElement('link');
link.href = url;
link.type = 'text/css';
link.rel = 'stylesheet';
document.getElementsByTagName('head')[0].appendChild(link);
});
}
The code will be executed in the browser, so the document will be available, but tests are run locally, and I want to use a fake document which I generate with JSDOM module. Is it possible to make node look for document from the test case's scope? ATM I'm explicitly passing doc, but I want to find to proxy the scope.
Upvotes: 0
Views: 42
Reputation: 55962
One way to resolve this by exporting a function that takes a url and a document
object. Then your production code could pass in it's real document object, and your test could configure a test object to assert on:
module.exports = function(document, url){
return new Promise(function(resolve){
const link = document.createElement('link');
link.href = url;
link.type = 'text/css';
link.rel = 'stylesheet';
document.getElementsByTagName('head')[0].appendChild(link);
resolve(document);
});
}
it('should append link', function() {
var document = jsdom('<html><head></head></html>');
css(document, someUrl).then(function(mutatedDocument) {
assert(mutatedDocument).containsURL(someUrl);
});
});
Upvotes: 1