Commit f2a6fe75 authored by Robert Knight's avatar Robert Knight

Improve tests for `Guest#destroy`

Add test to check that anchors not associated with the removed
annotation are preserved.
parent 7bb6b70a
...@@ -923,7 +923,11 @@ describe('Guest', () => { ...@@ -923,7 +923,11 @@ describe('Guest', () => {
}); });
describe('#detach', () => { describe('#detach', () => {
it('removes the anchors from the "anchors" instance variable', () => { function createAnchor() {
return { annotation: {}, highlights: [document.createElement('span')] };
}
it('removes anchors associated with the removed annotation', () => {
const guest = createGuest(); const guest = createGuest();
const annotation = {}; const annotation = {};
guest.anchors.push({ annotation }); guest.anchors.push({ annotation });
...@@ -933,29 +937,41 @@ describe('Guest', () => { ...@@ -933,29 +937,41 @@ describe('Guest', () => {
assert.equal(guest.anchors.length, 0); assert.equal(guest.anchors.length, 0);
}); });
it('emits an `anchorsChanged` event', () => { it('removes any highlights associated with the annotation', () => {
const guest = createGuest(); const guest = createGuest();
const annotation = {}; const anchor = createAnchor();
guest.anchors.push({ annotation }); const { removeHighlights } = highlighter;
const anchorsChanged = sandbox.stub(); guest.anchors.push(anchor);
guest._emitter.subscribe('anchorsChanged', anchorsChanged);
guest.detach(annotation); guest.detach(anchor.annotation);
assert.calledWith(anchorsChanged, guest.anchors); assert.calledOnce(removeHighlights);
assert.calledWith(removeHighlights, anchor.highlights);
}); });
it('removes any highlights associated with the annotation', () => { it('keeps anchors and highlights associated with other annotations', () => {
const guest = createGuest(); const guest = createGuest();
const annotation = {}; const anchorA = createAnchor();
const highlights = [document.createElement('span')]; const anchorB = createAnchor();
const { removeHighlights } = highlighter; guest.anchors.push(anchorA, anchorB);
guest.anchors.push({ annotation, highlights });
guest.detach(annotation); guest.detach(anchorA.annotation);
assert.calledOnce(removeHighlights); assert.include(guest.anchors, anchorB);
assert.calledWith(removeHighlights, highlights); assert.isFalse(
highlighter.removeHighlights.calledWith(anchorB.highlights)
);
});
it('emits an `anchorsChanged` event with updated anchors', () => {
const guest = createGuest();
const anchor = createAnchor();
const anchorsChanged = sandbox.stub();
guest._emitter.subscribe('anchorsChanged', anchorsChanged);
guest.detach(anchor.annotation);
assert.calledWith(anchorsChanged, guest.anchors);
}); });
}); });
......
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