Unverified Commit 67ecd180 authored by Robert Knight's avatar Robert Knight Committed by GitHub

Merge pull request #1085 from hypothesis/mockable-imports-part-2

Convert mocking to mockable-imports (2/4)
parents 3cdeb195 12e0ddc5
'use strict';
const proxyquire = require('proxyquire');
const util = require('../../../shared/test/util');
const fakeSettingsFrom = sinon.stub();
const configFrom = proxyquire(
'../index',
util.noCallThru({
'./settings': fakeSettingsFrom,
})
);
const configFrom = require('../index');
describe('annotator.config.index', function() {
beforeEach('reset fakeSettingsFrom', function() {
fakeSettingsFrom.reset();
fakeSettingsFrom.returns({
let fakeSettingsFrom;
beforeEach(() => {
fakeSettingsFrom = sinon.stub().returns({
hostPageSetting: sinon.stub(),
});
configFrom.$imports.$mock({
'./settings': fakeSettingsFrom,
});
});
afterEach(() => {
configFrom.$imports.$restore();
});
it('gets the configuration settings', function() {
......
'use strict';
const proxyquire = require('proxyquire');
const util = require('../../../shared/test/util');
const settingsFrom = require('../settings');
const fakeConfigFuncSettingsFrom = sinon.stub();
const fakeIsBrowserExtension = sinon.stub();
const fakeSharedSettings = {};
describe('annotator.config.settingsFrom', function() {
let fakeConfigFuncSettingsFrom;
let fakeIsBrowserExtension;
let fakeSharedSettings;
beforeEach(() => {
fakeConfigFuncSettingsFrom = sinon.stub().returns({});
fakeIsBrowserExtension = sinon.stub().returns(false);
fakeSharedSettings = {
jsonConfigsFrom: sinon.stub().returns({}),
};
const settingsFrom = proxyquire(
'../settings',
util.noCallThru({
settingsFrom.$imports.$mock({
'./config-func-settings-from': fakeConfigFuncSettingsFrom,
'./is-browser-extension': fakeIsBrowserExtension,
'../../shared/settings': fakeSharedSettings,
})
);
describe('annotator.config.settingsFrom', function() {
beforeEach('reset fakeConfigFuncSettingsFrom', function() {
fakeConfigFuncSettingsFrom.reset();
fakeConfigFuncSettingsFrom.returns({});
});
beforeEach('reset fakeIsBrowserExtension', function() {
fakeIsBrowserExtension.reset();
fakeIsBrowserExtension.returns(false);
});
beforeEach('reset fakeSharedSettings', function() {
fakeSharedSettings.jsonConfigsFrom = sinon.stub().returns({});
afterEach(() => {
settingsFrom.$imports.$restore();
});
describe('#sidebarAppUrl', function() {
......
'use strict';
const proxyquire = require('proxyquire');
const boot = require('../boot');
function assetUrl(url) {
return `https://marginal.ly/client/build/${url}`;
}
describe('bootstrap', function() {
let boot;
let fakePolyfills;
let iframe;
......@@ -19,12 +18,13 @@ describe('bootstrap', function() {
requiredPolyfillSets: sinon.stub().returns([]),
};
boot = proxyquire('../boot', {
boot.$imports.$mock({
'../shared/polyfills': fakePolyfills,
});
});
afterEach(function() {
boot.$imports.$restore();
iframe.remove();
});
......
'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 });
}
/**
* Helper for writing parameterized tests.
*
......@@ -79,6 +57,5 @@ function unroll(description, testFn, fixtures) {
}
module.exports = {
noCallThru: noCallThru,
unroll: unroll,
};
'use strict';
const angular = require('angular');
const proxyquire = require('proxyquire');
const fixtures = require('../../test/annotation-fixtures');
const annotationHeader = require('../annotation-header');
const fakeDocumentMeta = {
domain: 'docs.io',
......@@ -27,7 +27,7 @@ describe('sidebar.components.annotation-header', function() {
});
beforeEach('Import and register the annotationHeader component', function() {
const annotationHeader = proxyquire('../annotation-header', {
annotationHeader.$imports.$mock({
'../annotation-metadata': {
// eslint-disable-next-line no-unused-vars
domainAndTitle: function(ann) {
......@@ -35,12 +35,14 @@ describe('sidebar.components.annotation-header', function() {
},
},
'../util/account-id': fakeAccountID,
'@noCallThru': true,
});
angular.module('app', []).component('annotationHeader', annotationHeader);
});
afterEach(() => {
annotationHeader.$imports.$restore();
});
beforeEach('Initialize and register fake AngularJS dependencies', function() {
fakeFeatures = {
flagEnabled: sinon.stub().returns(false),
......
'use strict';
const angular = require('angular');
const proxyquire = require('proxyquire');
const events = require('../../events');
const fixtures = require('../../test/annotation-fixtures');
const testUtil = require('../../../shared/test/util');
const util = require('../../directive/test/util');
const annotationComponent = require('../annotation');
const inject = angular.mock.inject;
const unroll = testUtil.unroll;
......@@ -100,9 +101,6 @@ describe('annotation', function() {
let $scope;
let $timeout;
let $window;
// Unfortunately fakeAccountID needs to be initialised here because it
// gets passed into proxyquire() _before_ the beforeEach() that initializes
// the rest of the fakes runs.
const fakeAccountID = {
isThirdPartyUser: sinon.stub(),
};
......@@ -120,16 +118,15 @@ describe('annotation', function() {
let fakeStreamer;
let sandbox;
/**
* Returns the annotation directive with helpers stubbed out.
*/
function annotationComponent() {
return proxyquire('../annotation', {
angular: testUtil.noCallThru(angular),
beforeEach(() => {
annotationComponent.$imports.$mock({
'../util/account-id': fakeAccountID,
'@noCallThru': true,
});
}
});
afterEach(() => {
annotationComponent.$imports.$restore();
});
function createDirective(annotation) {
annotation = annotation || fixtures.defaultAnnotation();
......@@ -153,7 +150,7 @@ describe('annotation', function() {
before(function() {
angular
.module('h', [])
.component('annotation', annotationComponent())
.component('annotation', annotationComponent)
.component('annotationActionButton', {
bindings: {
icon: '<',
......
'use strict';
const angular = require('angular');
const proxyquire = require('proxyquire');
const EventEmitter = require('tiny-emitter');
const events = require('../../events');
const noCallThru = require('../../../shared/test/util').noCallThru;
const sidebarContent = require('../sidebar-content');
const uiConstants = require('../../ui-constants');
let searchClients;
......@@ -64,21 +63,12 @@ describe('sidebar.components.sidebar-content', function() {
angular
.module('h', [])
.service('store', require('../../store'))
.component(
'sidebarContent',
proxyquire(
'../sidebar-content',
noCallThru({
angular: angular,
'../search-client': FakeSearchClient,
})
)
);
.component('sidebarContent', sidebarContent);
});
beforeEach(angular.mock.module('h'));
beforeEach(
beforeEach(() => {
angular.mock.module(function($provide) {
searchClients = [];
sandbox = sinon.sandbox.create();
......@@ -142,8 +132,16 @@ describe('sidebar.components.sidebar-content', function() {
$provide.value('streamFilter', fakeStreamFilter);
$provide.value('groups', fakeGroups);
$provide.value('settings', fakeSettings);
})
);
});
sidebarContent.$imports.$mock({
'../search-client': FakeSearchClient,
});
});
afterEach(() => {
sidebarContent.$imports.$restore();
});
function setFrames(frames) {
frames.forEach(function(frame) {
......
'use strict';
const angular = require('angular');
const proxyquire = require('proxyquire');
const immutable = require('seamless-immutable');
const annotationFixtures = require('../../test/annotation-fixtures');
......@@ -9,6 +8,8 @@ const events = require('../../events');
const uiConstants = require('../../ui-constants');
const util = require('../../../shared/test/util');
const rootThreadFactory = require('../root-thread');
const unroll = util.unroll;
const fixtures = immutable({
......@@ -84,12 +85,7 @@ describe('rootThread', function() {
.value('drafts', fakeDrafts)
.value('searchFilter', fakeSearchFilter)
.value('viewFilter', fakeViewFilter)
.service(
'rootThread',
proxyquire('../root-thread', {
'../build-thread': util.noCallThru(fakeBuildThread),
})
);
.service('rootThread', rootThreadFactory);
angular.mock.module('app');
......@@ -99,6 +95,16 @@ describe('rootThread', function() {
});
});
beforeEach(() => {
rootThreadFactory.$imports.$mock({
'../build-thread': fakeBuildThread,
});
});
afterEach(() => {
rootThreadFactory.$imports.$restore();
});
describe('#thread', function() {
it('returns the result of buildThread()', function() {
assert.equal(rootThread.thread(fakeStore.state), fixtures.emptyThread);
......
'use strict';
const EventEmitter = require('tiny-emitter');
const proxyquire = require('proxyquire');
const events = require('../../events');
const unroll = require('../../../shared/test/util').unroll;
const Streamer = require('../streamer');
const fixtures = {
createNotification: {
type: 'annotation-notification',
......@@ -82,7 +83,6 @@ describe('Streamer', function() {
let fakeSession;
let fakeSettings;
let activeStreamer;
let Streamer;
function createDefaultStreamer() {
activeStreamer = new Streamer(
......@@ -143,12 +143,13 @@ describe('Streamer', function() {
websocketUrl: 'ws://example.com/ws',
};
Streamer = proxyquire('../streamer', {
Streamer.$imports.$mock({
'../websocket': FakeSocket,
});
});
afterEach(function() {
Streamer.$imports.$restore();
activeStreamer = null;
});
......
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