Skip to content

Instantly share code, notes, and snippets.

@julianlconnor
Last active December 24, 2015 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save julianlconnor/6827935 to your computer and use it in GitHub Desktop.
Save julianlconnor/6827935 to your computer and use it in GitHub Desktop.
How we mock and stub modules in stitched files. We ran into a roadblock when trying to mock and stub dependencies on the client. The stitched file wouldn't allow us to inject any of our own stuff. We had to write a wrapper around require inside of stitch to allow us to insert tests. I've made my own modifications to stitch and rendr_stitch in or…
var BaseView = require('../base'),
FeedView = require('../feed');
module.exports = BaseView.extend({
className: 'home_index_view',
postRender: function() {
this.feed = new FeedView({ collection: this.options.feed,
app: this.app});
this.$('.feed-wrapper').html(this.feed.render().el);
this.$('.user-wrapper .name').html(this.options.user.get('name'));
this.$('.balance').html(this.options.user.get('balance'));
}
});
module.exports.id = 'home/index';
var spy = sinon.spy(function() { return {
render: function() {
return { el: 'I AM A BANANA' }
}
}});
var HomeIndexView = requireAndInject('./index', { '../feed': spy });
describe('Home Index View', function() {
it('can instantiate', function() {
var home = new HomeIndexView();
expect(home).to.be.ok();
});
it('instantiates a FeedView with a a user\'s name and balance', function() {
var name = 'Julian';
var balance = '200.13';
var home = new HomeIndexView({
feed: [1,2,3],
app: window.App,
user: {
get: function(key) {
switch(key) {
case 'name' : return name;
case 'balance' : return balance;
}
}
}});
home.render();
expect(spy.called).to.be(true);
expect(home.$('.feed-wrapper').html()).to.be('I AM A BANANA');
expect(home.$('.user-wrapper .name').html()).to.be(name);
expect(home.$('.user-wrapper .balance').html()).to.be(balance);
});
});
(function(/*! Stitch !*/) {
if (!this.require) {
var modules = {}, cache = {}, require = function(name, root) {
var path = expand(root, name), module = cache[path], fn;
if (module) {
return module.exports;
} else if (fn = modules[path] || modules[path = expand(path, './index')]) {
module = {id: path, exports: {}};
try {
cache[path] = module;
fn(module.exports, function(name) {
return require(name, dirname(path));
}, module);
return module.exports;
} catch (err) {
delete cache[path];
throw err;
}
} else {
throw 'module \'' + name + '\' not found';
}
}, expand = function(root, name) {
var results = [], parts, part;
if (/^\.\.?(\/|$)/.test(name)) {
parts = [root, name].join('/').split('/');
} else {
parts = name.split('/');
}
for (var i = 0, length = parts.length; i < length; i++) {
part = parts[i];
if (part == '..') {
results.pop();
} else if (part != '.' && part != '') {
results.push(part);
}
}
return results.join('/');
}, dirname = function(path) {
return path.split('/').slice(0, -1).join('/');
};
this.requireAndInject = function(name, root, stubs) {
/*
* This is the method we added, pretty much just wraps require.
* If the file is attempting to access a module we've stubbed/mocked, return the stub/mock.
* Otherwise, normally require it.
*/
if ( !stubs ) {
stubs = root;
root = null;
}
var path = expand(root, name), module = cache[path], ogRequire = require, fn;
if (module) {
return module.exports;
} else if (fn = modules[path] || modules[path = expand(path, './index')]) {
module = {id: path, exports: {}};
fn(module.exports, function(name) {
if ( stubs[name] )
return stubs[name];
else
return require(name, dirname(path));
}, module);
return module.exports;
} else {
throw 'module \'' + name + '\' not found';
}
};
this.require = function(name) {
return require(name, '');
}
this.require.define = function(bundle) {
for (var key in bundle)
modules[key] = bundle[key];
};
}
return this.require.define;
}).call(this)({"app/app": function(exports, require, module) {var BaseApp = require('rendr/shared/app'),
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment