Commit 478c173c authored by Robert Knight's avatar Robert Knight

Improve some test descriptions and do some small cleanups

 - Remove an `Object.assign` in favor of object spread

 - Remove an unused `ctx` argument to `emitGuestEvent`

 - Rewrite several test descriptions for readability

 - Re-order stub initializations in `beforeEach` to be more alphabetical
parent bf94d530
...@@ -46,22 +46,12 @@ describe('Guest', () => { ...@@ -46,22 +46,12 @@ describe('Guest', () => {
let selections; let selections;
const createGuest = (config = {}) => { const createGuest = (config = {}) => {
config = Object.assign({}, guestConfig, config);
const element = document.createElement('div'); const element = document.createElement('div');
const guest = new Guest(element, config); return new Guest(element, { ...guestConfig, ...config });
return guest;
}; };
beforeEach(() => { beforeEach(() => {
sinon.stub(console, 'warn');
FakeAdder.instance = null; FakeAdder.instance = null;
rangeUtil = {
itemsForRange: sinon.stub().returns([]),
isSelectionBackwards: sinon.stub(),
selectionFocusRect: sinon.stub(),
};
selections = null;
guestConfig = { pluginClasses: {} }; guestConfig = { pluginClasses: {} };
highlighter = { highlighter = {
highlightRange: sinon.stub(), highlightRange: sinon.stub(),
...@@ -70,9 +60,26 @@ describe('Guest', () => { ...@@ -70,9 +60,26 @@ describe('Guest', () => {
htmlAnchoring = { htmlAnchoring = {
anchor: sinon.stub(), anchor: sinon.stub(),
}; };
rangeUtil = {
itemsForRange: sinon.stub().returns([]),
isSelectionBackwards: sinon.stub(),
selectionFocusRect: sinon.stub(),
};
selections = null;
sinon.stub(window, 'requestAnimationFrame').yields(); sinon.stub(window, 'requestAnimationFrame').yields();
fakeCrossFrame = {
onConnect: sinon.stub(),
on: sinon.stub(),
call: sinon.stub(),
sync: sinon.stub(),
destroy: sinon.stub(),
};
CrossFrame = sandbox.stub().returns(fakeCrossFrame);
guestConfig.pluginClasses.CrossFrame = CrossFrame;
$imports.$mock({ $imports.$mock({
'./adder': { Adder: FakeAdder }, './adder': { Adder: FakeAdder },
'./anchoring/html': htmlAnchoring, './anchoring/html': htmlAnchoring,
...@@ -87,23 +94,11 @@ describe('Guest', () => { ...@@ -87,23 +94,11 @@ describe('Guest', () => {
'./delegator': Delegator, './delegator': Delegator,
'scroll-into-view': scrollIntoView, 'scroll-into-view': scrollIntoView,
}); });
fakeCrossFrame = {
onConnect: sinon.stub(),
on: sinon.stub(),
call: sinon.stub(),
sync: sinon.stub(),
destroy: sinon.stub(),
};
CrossFrame = sandbox.stub().returns(fakeCrossFrame);
guestConfig.pluginClasses.CrossFrame = CrossFrame;
}); });
afterEach(() => { afterEach(() => {
window.requestAnimationFrame.restore(); window.requestAnimationFrame.restore();
sandbox.restore(); sandbox.restore();
console.warn.restore();
$imports.$restore(); $imports.$restore();
}); });
...@@ -118,15 +113,15 @@ describe('Guest', () => { ...@@ -118,15 +113,15 @@ describe('Guest', () => {
fakePlugin = FakePlugin.instance; fakePlugin = FakePlugin.instance;
}); });
it('load and "pluginInit" gets called', () => { it('calls `pluginInit`', () => {
assert.calledOnce(fakePlugin.pluginInit); assert.calledOnce(fakePlugin.pluginInit);
}); });
it('hold reference to instance', () => { it('sets `annotator` property of plugin', () => {
assert.equal(fakePlugin.annotator, guest); assert.equal(fakePlugin.annotator, guest);
}); });
it('destroy when instance is destroyed', () => { it('calls `destroy` method of plugins when guest is destroyed', () => {
sandbox.spy(fakePlugin, 'destroy'); sandbox.spy(fakePlugin, 'destroy');
guest.destroy(); guest.destroy();
assert.called(fakePlugin.destroy); assert.called(fakePlugin.destroy);
...@@ -250,7 +245,7 @@ describe('Guest', () => { ...@@ -250,7 +245,7 @@ describe('Guest', () => {
{ annotation: { $tag: 'tag1' }, highlights: highlight0.toArray() }, { annotation: { $tag: 'tag1' }, highlights: highlight0.toArray() },
{ annotation: { $tag: 'tag2' }, highlights: highlight1.toArray() }, { annotation: { $tag: 'tag2' }, highlights: highlight1.toArray() },
]; ];
emitGuestEvent('focusAnnotations', 'ctx', ['tag1']); emitGuestEvent('focusAnnotations', ['tag1']);
assert.isFalse(highlight1.hasClass('hypothesis-highlight-focused')); assert.isFalse(highlight1.hasClass('hypothesis-highlight-focused'));
}); });
}); });
...@@ -537,7 +532,7 @@ describe('Guest', () => { ...@@ -537,7 +532,7 @@ describe('Guest', () => {
}); });
it('removes the fragment identifier from URIs', () => { it('removes the fragment identifier from URIs', () => {
guest.plugins.PDF.uri = () => 'urn:x-pdf:aabbcc#'; guest.plugins.PDF.uri = () => 'urn:x-pdf:aabbcc#ignoreme';
return guest return guest
.getDocumentInfo() .getDocumentInfo()
.then(({ uri }) => assert.equal(uri, 'urn:x-pdf:aabbcc')); .then(({ uri }) => assert.equal(uri, 'urn:x-pdf:aabbcc'));
...@@ -563,14 +558,14 @@ describe('Guest', () => { ...@@ -563,14 +558,14 @@ describe('Guest', () => {
}); });
}); });
it('treats an argument as the annotation object', () => { it('merges properties of input object into returned annotation', () => {
const guest = createGuest(); const guest = createGuest();
let annotation = { foo: 'bar' }; let annotation = { foo: 'bar' };
annotation = guest.createAnnotation(annotation); annotation = guest.createAnnotation(annotation);
assert.equal(annotation.foo, 'bar'); assert.equal(annotation.foo, 'bar');
}); });
it('triggers a beforeAnnotationCreated event', done => { it('triggers a "beforeAnnotationCreated" event', done => {
const guest = createGuest(); const guest = createGuest();
guest.subscribe('beforeAnnotationCreated', () => done()); guest.subscribe('beforeAnnotationCreated', () => done());
guest.createAnnotation(); guest.createAnnotation();
......
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