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'); ...@@ -4,19 +4,7 @@ var angular = require('angular');
var proxyquire = require('proxyquire'); var proxyquire = require('proxyquire');
var util = require('./util'); var util = require('./util');
var noCallThru = require('../../test/util').noCallThru;
/**
* 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});
}
describe('markdown', function () { describe('markdown', function () {
function isHidden(element) { function isHidden(element) {
...@@ -49,8 +37,8 @@ describe('markdown', function () { ...@@ -49,8 +37,8 @@ describe('markdown', function () {
before(function () { before(function () {
angular.module('app', ['ngSanitize']) angular.module('app', ['ngSanitize'])
.directive('markdown', proxyquire('../markdown', { .directive('markdown', proxyquire('../markdown', noCallThru({
angular: noCallThru(angular), angular: angular,
katex: { katex: {
renderToString: function (input) { renderToString: function (input) {
return 'math:' + input.replace(/$$/g, ''); return 'math:' + input.replace(/$$/g, '');
...@@ -62,8 +50,7 @@ describe('markdown', function () { ...@@ -62,8 +50,7 @@ describe('markdown', function () {
toggleSpanStyle: mockFormattingCommand, toggleSpanStyle: mockFormattingCommand,
LinkType: require('../../markdown-commands').LinkType, LinkType: require('../../markdown-commands').LinkType,
}, },
'@noCallThru': true, })))
}))
.filter('converter', function () { .filter('converter', function () {
return function (input) { return function (input) {
return 'rendered:' + input; return 'rendered:' + input;
......
'use strict'; 'use strict';
var proxyquire = require('proxyquire'); var proxyquire = require('proxyquire');
var noCallThru = require('./util').noCallThru;
function noCallThru(stub) {
return Object.assign(stub, {'@noCallThru':true});
}
function fakeExceptionData(scriptURL) { function fakeExceptionData(scriptURL) {
return { return {
...@@ -51,10 +48,10 @@ describe('raven', function () { ...@@ -51,10 +48,10 @@ describe('raven', function () {
Raven.setDataCallback(fakeAngularTransformer); Raven.setDataCallback(fakeAngularTransformer);
}); });
raven = proxyquire('../raven', { raven = proxyquire('../raven', noCallThru({
'raven-js': noCallThru(fakeRavenJS), 'raven-js': fakeRavenJS,
'raven-js/plugins/angular': noCallThru(fakeAngularPlugin), 'raven-js/plugins/angular': fakeAngularPlugin,
}); }));
}); });
describe('.install()', function () { 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'); ...@@ -6,10 +6,7 @@ var proxyquire = require('proxyquire');
var EventEmitter = require('tiny-emitter'); var EventEmitter = require('tiny-emitter');
var events = require('../events'); var events = require('../events');
var noCallThru = require('./util').noCallThru;
function noCallThru(stub) {
return Object.assign(stub, {'@noCallThru':true});
}
var searchClients; var searchClients;
function FakeSearchClient(resource, opts) { function FakeSearchClient(resource, opts) {
...@@ -45,10 +42,12 @@ describe('WidgetController', function () { ...@@ -45,10 +42,12 @@ describe('WidgetController', function () {
before(function () { before(function () {
angular.module('h', []) angular.module('h', [])
.controller('WidgetController', proxyquire('../widget-controller', { .controller('WidgetController', proxyquire('../widget-controller',
angular: noCallThru(angular), noCallThru({
'./search-client': noCallThru(FakeSearchClient), angular: angular,
})); './search-client': FakeSearchClient,
})
));
}); });
beforeEach(angular.mock.module('h')); 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