Commit c0a05f89 authored by Nick Stenning's avatar Nick Stenning

Merge pull request #1858 from hypothesis/thread-prune-fix

Threading events fix and tests
parents 889c7533 2605ac16
......@@ -75,17 +75,23 @@ class Annotator.Plugin.Threading extends Annotator.Plugin
break
annotationDeleted: (annotation) =>
if id of this.idTable
container = this.idTable[id]
if this.idTable[annotation.id]
container = this.idTable[annotation.id]
container.message = null
delete this.idTable[id]
delete this.idTable[annotation.id]
this.pruneEmpties(@root)
else
for id, container of this.idTable
for child in container.children when child.message is annotation
child.message = null
this.pruneEmpties(@root)
return
if annotation.references
refs = annotation.references
unless angular.isArray(refs) then refs = [refs]
parentRef = refs[refs.length-1]
parent = this.idTable[parentRef]
else
parent = @root
for child in parent.children when child.message is annotation
child.message = null
this.pruneEmpties(@root)
break
annotationsLoaded: (annotations) =>
messages = (@root.flattenChildren() or []).concat(annotations)
......
assert = chai.assert
sinon.assert.expose(assert, prefix: '')
sinon.assert.expose(assert, prefix: null)
sandbox = sinon.sandbox.create()
describe 'Annotator.Threading', ->
createThreadingInstance = (options) ->
......@@ -84,3 +85,60 @@ describe 'Annotator.Threading', ->
instance.pruneEmpties(root)
assert.equal(root.children.length, 0)
describe 'handles events', ->
annotator = null
instance = null
beforeEach ->
instance = createThreadingInstance()
instance.pluginInit()
annotator =
publish: (event, args) ->
unless angular.isArray(args) then args = [args]
meth = instance.events[event]
instance[meth].apply(instance, args)
afterEach ->
sandbox.restore()
it 'calls the thread method on beforeAnnotationCreated', ->
annotation = {id: 'foo'}
sandbox.spy(instance, 'thread')
annotator.publish 'beforeAnnotationCreated', annotation
assert.calledWithMatch instance.thread, [annotation]
it 'calls the thread method on annotationsLoaded', ->
annotation = {id: 'foo'}
sandbox.spy(instance, 'thread')
annotator.publish 'annotationsLoaded', [annotation]
assert.calledWithMatch instance.thread, [annotation]
it 'removes matching top level threads when annotationDeleted is called', ->
annotation = {id: 'foo'}
instance.thread([annotation])
assert.equal(instance.root.children.length, 1)
assert.equal(instance.idTable['foo'].message, annotation)
sandbox.spy(instance, 'pruneEmpties')
annotator.publish 'annotationDeleted', annotation
assert.called(instance.pruneEmpties)
assert.equal(instance.root.children.length, 0)
assert.isUndefined(instance.idTable['foo'])
it 'removes matching reply threads when annotationDeleted is called', ->
parent = {id: 'foo'}
reply = {id: 'bar', references: ['foo']}
instance.thread([parent, reply])
assert.equal(instance.idTable['foo'].children.length, 1)
assert.equal(instance.idTable['bar'].message, reply)
sandbox.spy(instance, 'pruneEmpties')
annotator.publish 'annotationDeleted', reply
assert.called(instance.pruneEmpties)
assert.equal(instance.idTable['foo'].children.length, 0)
assert.isUndefined(instance.idTable['bar'])
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