Skip to content

Commit

Permalink
Merge pull request #1086 from hypothesis/mockable-imports-part-3
Browse files Browse the repository at this point in the history
Convert mocking to mockable imports (3/4)
  • Loading branch information
Hannah Stepanek authored May 1, 2019
2 parents 67ecd18 + 6e2fc70 commit b2ad6fb
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 40 deletions.
7 changes: 5 additions & 2 deletions src/annotator/guest.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Delegator = require('./delegator')
$ = require('jquery')

adder = require('./adder')
htmlAnchoring = require('./anchoring/html')
highlighter = require('./highlighter')
rangeUtil = require('./range-util')
selections = require('./selections')
Expand Down Expand Up @@ -37,7 +38,7 @@ module.exports = class Guest extends Delegator
TextSelection: {}

# Anchoring module
anchoring: require('./anchoring/html')
anchoring: null

# Internal state
plugins: null
Expand All @@ -48,7 +49,7 @@ module.exports = class Guest extends Delegator
html:
adder: '<hypothesis-adder></hypothesis-adder>'

constructor: (element, config) ->
constructor: (element, config, anchoring = htmlAnchoring) ->
super

this.adder = $(this.html.adder).appendTo(@element).hide()
Expand Down Expand Up @@ -77,6 +78,8 @@ module.exports = class Guest extends Delegator
# The "top" guest instance will have this as null since it's in a top frame not a sub frame
this.frameIdentifier = config.subFrameIdentifier || null

this.anchoring = anchoring

cfOptions =
config: config
on: (event, handler) =>
Expand Down
8 changes: 3 additions & 5 deletions src/annotator/plugin/test/cross-frame-test.coffee
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
proxyquire = require('proxyquire')

Plugin = require('../../plugin')
CrossFrame = null
CrossFrame = require('../cross-frame')

describe 'CrossFrame', ->
fakeDiscovery = null
Expand Down Expand Up @@ -41,16 +39,16 @@ describe 'CrossFrame', ->
proxyDiscovery = sandbox.stub().returns(fakeDiscovery)
proxyBridge = sandbox.stub().returns(fakeBridge)

CrossFrame = proxyquire('../cross-frame', {
CrossFrame.$imports.$mock({
'../plugin': Plugin,
'../annotation-sync': proxyAnnotationSync,
'../../shared/bridge': proxyBridge,
'../../shared/discovery': proxyDiscovery
})


afterEach ->
sandbox.restore()
CrossFrame.$imports.$restore()

describe 'CrossFrame constructor', ->
it 'instantiates the Discovery component', ->
Expand Down
52 changes: 29 additions & 23 deletions src/annotator/test/guest-test.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
proxyquire = require('proxyquire')

adder = require('../adder')
Observable = require('../util/observable').Observable
Plugin = require('../plugin')
Expand All @@ -8,9 +6,7 @@ Delegator = require('../delegator')
$ = require('jquery')
Delegator['@noCallThru'] = true

Guest = null
anchoring = {}
highlighter = {}
Guest = require('../guest')
rangeUtil = null
selections = null

Expand Down Expand Up @@ -50,12 +46,15 @@ describe 'Guest', ->
sandbox = sinon.sandbox.create()
CrossFrame = null
fakeCrossFrame = null
highlighter = null
guestConfig = null
htmlAnchoring = null

createGuest = (config={}) ->
config = Object.assign({}, guestConfig, config)
element = document.createElement('div')
return new Guest(element, config)
guest = new Guest(element, config)
return guest

beforeEach ->
sinon.stub(console, 'warn')
Expand All @@ -67,10 +66,17 @@ describe 'Guest', ->
}
selections = null
guestConfig = {pluginClasses: {}}
highlighter = {
highlightRange: sinon.stub()
removeHighlights: sinon.stub()
}
htmlAnchoring = {
anchor: sinon.stub()
}

Guest = proxyquire('../guest', {
Guest.$imports.$mock({
'./adder': {Adder: FakeAdder},
'./anchoring/html': anchoring,
'./anchoring/html': htmlAnchoring,
'./highlighter': highlighter,
'./range-util': rangeUtil,
'./selections': (document) ->
Expand All @@ -97,6 +103,7 @@ describe 'Guest', ->
afterEach ->
sandbox.restore()
console.warn.restore()
Guest.$imports.$restore()

describe 'plugins', ->
fakePlugin = null
Expand Down Expand Up @@ -478,7 +485,7 @@ describe 'Guest', ->
{selector: [{type: 'TextQuoteSelector', exact: 'hello'}]},
]
}
sandbox.stub(anchoring, 'anchor').returns(Promise.resolve(range))
htmlAnchoring.anchor.returns(Promise.resolve(range))

guest.anchor(annotation).then ->
assert.isFalse(annotation.$orphan)
Expand All @@ -491,7 +498,7 @@ describe 'Guest', ->
{selector: [{type: 'TextQuoteSelector', exact: 'hello'}]},
]
}
sandbox.stub(anchoring, 'anchor')
htmlAnchoring.anchor
.onFirstCall().returns(Promise.reject())
.onSecondCall().returns(Promise.resolve(range))

Expand All @@ -505,7 +512,7 @@ describe 'Guest', ->
{selector: [{type: 'TextQuoteSelector', exact: 'notinhere'}]},
]
}
sandbox.stub(anchoring, 'anchor').returns(Promise.reject())
htmlAnchoring.anchor.returns(Promise.reject())

guest.anchor(annotation).then ->
assert.isTrue(annotation.$orphan)
Expand All @@ -518,7 +525,7 @@ describe 'Guest', ->
{selector: [{type: 'TextQuoteSelector', exact: 'neitherami'}]},
]
}
sandbox.stub(anchoring, 'anchor').returns(Promise.reject())
htmlAnchoring.anchor.returns(Promise.reject())

guest.anchor(annotation).then ->
assert.isTrue(annotation.$orphan)
Expand All @@ -532,7 +539,7 @@ describe 'Guest', ->
}
# This shouldn't be called, but if it is, we successfully anchor so that
# this test is guaranteed to fail.
sandbox.stub(anchoring, 'anchor').returns(Promise.resolve(range))
htmlAnchoring.anchor.returns(Promise.resolve(range))

guest.anchor(annotation).then ->
assert.isTrue(annotation.$orphan)
Expand All @@ -544,10 +551,9 @@ describe 'Guest', ->
{selector: [{type: 'TextPositionSelector', start: 0, end: 5}]},
]
}
sandbox.spy(anchoring, 'anchor')

guest.anchor(annotation).then ->
assert.notCalled(anchoring.anchor)
assert.notCalled(htmlAnchoring.anchor)

it 'updates the cross frame and bucket bar plugins', () ->
guest = createGuest()
Expand All @@ -563,17 +569,17 @@ describe 'Guest', ->
it 'returns a promise of the anchors for the annotation', () ->
guest = createGuest()
highlights = [document.createElement('span')]
sandbox.stub(anchoring, 'anchor').returns(Promise.resolve(range))
sandbox.stub(highlighter, 'highlightRange').returns(highlights)
htmlAnchoring.anchor.returns(Promise.resolve(range))
highlighter.highlightRange.returns(highlights)
target = {selector: [{type: 'TextQuoteSelector', exact: 'hello'}]}
return guest.anchor({target: [target]}).then (anchors) ->
assert.equal(anchors.length, 1)

it 'adds the anchor to the "anchors" instance property"', () ->
guest = createGuest()
highlights = [document.createElement('span')]
sandbox.stub(anchoring, 'anchor').returns(Promise.resolve(range))
sandbox.stub(highlighter, 'highlightRange').returns(highlights)
htmlAnchoring.anchor.returns(Promise.resolve(range))
highlighter.highlightRange.returns(highlights)
target = {selector: [{type: 'TextQuoteSelector', exact: 'hello'}]}
annotation = {target: [target]}
return guest.anchor(annotation).then ->
Expand All @@ -589,7 +595,7 @@ describe 'Guest', ->
highlights = []
guest = createGuest()
guest.anchors = [{annotation, target, highlights}]
removeHighlights = sandbox.stub(highlighter, 'removeHighlights')
removeHighlights = highlighter.removeHighlights

return guest.anchor(annotation).then ->
assert.equal(guest.anchors.length, 0)
Expand All @@ -599,11 +605,11 @@ describe 'Guest', ->
it 'does not reanchor targets that are already anchored', () ->
guest = createGuest()
annotation = target: [{selector: [{type: 'TextQuoteSelector', exact: 'hello'}]}]
stub = sandbox.stub(anchoring, 'anchor').returns(Promise.resolve(range))
htmlAnchoring.anchor.returns(Promise.resolve(range))
return guest.anchor(annotation).then ->
guest.anchor(annotation).then ->
assert.equal(guest.anchors.length, 1)
assert.calledOnce(stub)
assert.calledOnce(htmlAnchoring.anchor)

describe '#detach()', ->
it 'removes the anchors from the "anchors" instance variable', ->
Expand Down Expand Up @@ -636,7 +642,7 @@ describe 'Guest', ->
guest = createGuest()
annotation = {}
highlights = [document.createElement('span')]
removeHighlights = sandbox.stub(highlighter, 'removeHighlights')
removeHighlights = highlighter.removeHighlights

guest.anchors.push({annotation, highlights})
guest.deleteAnnotation(annotation)
Expand Down
3 changes: 1 addition & 2 deletions src/annotator/test/host-test.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
proxyquire = require('proxyquire')
Host = proxyquire('../host', {})
Host = require('../host')

describe 'Host', ->
sandbox = sinon.sandbox.create()
Expand Down
6 changes: 4 additions & 2 deletions src/annotator/test/integration/multi-frame-test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
'use strict';

const proxyquire = require('proxyquire');
const isLoaded = require('../../util/frame-util').isLoaded;

const FRAME_DEBOUNCE_WAIT = require('../../frame-observer').DEBOUNCE_WAIT + 10;
const CrossFrame = require('../../plugin/cross-frame');

describe('CrossFrame multi-frame scenario', function() {
let fakeAnnotationSync;
Expand All @@ -30,7 +30,7 @@ describe('CrossFrame multi-frame scenario', function() {
proxyAnnotationSync = sandbox.stub().returns(fakeAnnotationSync);
proxyBridge = sandbox.stub().returns(fakeBridge);

const CrossFrame = proxyquire('../../plugin/cross-frame', {
CrossFrame.$imports.$mock({
'../annotation-sync': proxyAnnotationSync,
'../../shared/bridge': proxyBridge,
});
Expand All @@ -53,6 +53,8 @@ describe('CrossFrame multi-frame scenario', function() {
sandbox.restore();
crossFrame.destroy();
container.parentNode.removeChild(container);

CrossFrame.$imports.$restore();
});

it('detects frames on page', function() {
Expand Down
15 changes: 9 additions & 6 deletions src/annotator/test/sidebar-test.coffee
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
events = require('../../shared/bridge-events')

proxyquire = require('proxyquire')

rafStub = (fn) ->
fn()

Sidebar = proxyquire('../sidebar', { raf: rafStub })
Sidebar = require('../sidebar')

DEFAULT_WIDTH = 350
DEFAULT_HEIGHT = 600
Expand All @@ -17,6 +12,14 @@ describe 'Sidebar', ->
fakeCrossFrame = null
sidebarConfig = {pluginClasses: {}}

before ->
rafStub = (fn) ->
fn()
Sidebar.$imports.$mock({ raf: rafStub })

after ->
Sidebar.$imports.$restore()

createSidebar = (config={}) ->
config = Object.assign({}, sidebarConfig, config)
element = document.createElement('div')
Expand Down

0 comments on commit b2ad6fb

Please sign in to comment.