Commit 75e1309f authored by Robert Knight's avatar Robert Knight

Extract 'noCallThru' helper into a separate module.

This was duplicated with varying levels of explanation in several
places.

Although the function is trivial, having it in its own module is useful
for documentation on why it should be used by default.
parent f3f3fce4
......@@ -4,19 +4,7 @@ var angular = require('angular');
var proxyquire = require('proxyquire');
var util = require('./util');
/**
* Disable calling through to the original module for a stub.
*
* By default proxyquire will call through to the original module
* for any methods not provided by a stub. This function disables
* this behavior for a stub and returns the input stub.
*
* This prevents unintended usage of the original dependency.
*/
function noCallThru(stub) {
return Object.assign(stub, {'@noCallThru':true});
}
var noCallThru = require('../../test/util').noCallThru;
describe('markdown', function () {
function isHidden(element) {
......@@ -49,8 +37,8 @@ describe('markdown', function () {
before(function () {
angular.module('app', ['ngSanitize'])
.directive('markdown', proxyquire('../markdown', {
angular: noCallThru(angular),
.directive('markdown', proxyquire('../markdown', noCallThru({
angular: angular,
katex: {
renderToString: function (input) {
return 'math:' + input.replace(/$$/g, '');
......@@ -62,8 +50,7 @@ describe('markdown', function () {
toggleSpanStyle: mockFormattingCommand,
LinkType: require('../../markdown-commands').LinkType,
},
'@noCallThru': true,
}))
})))
.filter('converter', function () {
return function (input) {
return 'rendered:' + input;
......
'use strict';
var proxyquire = require('proxyquire');
function noCallThru(stub) {
return Object.assign(stub, {'@noCallThru':true});
}
var noCallThru = require('./util').noCallThru;
function fakeExceptionData(scriptURL) {
return {
......@@ -51,10 +48,10 @@ describe('raven', function () {
Raven.setDataCallback(fakeAngularTransformer);
});
raven = proxyquire('../raven', {
'raven-js': noCallThru(fakeRavenJS),
'raven-js/plugins/angular': noCallThru(fakeAngularPlugin),
});
raven = proxyquire('../raven', noCallThru({
'raven-js': fakeRavenJS,
'raven-js/plugins/angular': fakeAngularPlugin,
}));
});
describe('.install()', function () {
......
'use strict';
/**
* Utility function for use with 'proxyquire' that prevents calls to
* stubs 'calling through' to the _original_ dependency if a particular
* function or property is not set on a stub, which is proxyquire's default
* but usually undesired behavior.
*
* See https://github.com/thlorenz/proxyquireify#nocallthru
*
* Usage:
* var moduleUnderTest = proxyquire('./module-under-test', noCallThru({
* './dependency-foo': fakeFoo,
* }));
*
* @param {Object} stubs - A map of dependency paths to stubs, or a single
* stub.
*/
function noCallThru(stubs) {
// This function is trivial but serves as documentation for why
// '@noCallThru' is used.
return Object.assign(stubs, {'@noCallThru':true});
}
module.exports = {
noCallThru: noCallThru,
};
......@@ -6,10 +6,7 @@ var proxyquire = require('proxyquire');
var EventEmitter = require('tiny-emitter');
var events = require('../events');
function noCallThru(stub) {
return Object.assign(stub, {'@noCallThru':true});
}
var noCallThru = require('./util').noCallThru;
var searchClients;
function FakeSearchClient(resource, opts) {
......@@ -45,10 +42,12 @@ describe('WidgetController', function () {
before(function () {
angular.module('h', [])
.controller('WidgetController', proxyquire('../widget-controller', {
angular: noCallThru(angular),
'./search-client': noCallThru(FakeSearchClient),
}));
.controller('WidgetController', proxyquire('../widget-controller',
noCallThru({
angular: angular,
'./search-client': FakeSearchClient,
})
));
});
beforeEach(angular.mock.module('h'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment