Commit daa64411 authored by Aron Carroll's avatar Aron Carroll

Start writing tests for the AnnotationUISync module

parent 6fb310f9
......@@ -49,6 +49,8 @@ class AnnotationSync
this._syncCache(channel)
@bridge.onConnect(onConnect)
# Provide a public interface to the annotation cache so that other
# sync services can lookup annotations by tag.
getAnnotationForTag: (tag) ->
@cache[tag] or null
......
# Uses a channel between the sidebar and the attached providers to ensure
# the interface remains in sync.
class AnnotationUISync
constructor: ($rootScope, $window, bridge, annotationUI) ->
constructor: ($window, bridge, annotationSync, annotationUI) ->
getAnnotationsByTags = (tags) ->
tags.map(bridge.getAnnotationForTag, bridge)
tags.map(annotationSync.getAnnotationForTag, bridge)
notifyHost = (message) ->
for {channel, window} in bridge.links where window is $window.parent
......@@ -17,20 +17,20 @@ class AnnotationUISync
back: hide
open: show
showEditor: show
showAnnotations: (ctx, tags=[]) =>
showAnnotations: (ctx, tags=[]) ->
show()
annotations = getAnnotationsByTags(tags)
annotationUI.xorSelectedAnnotations(annotations)
focusAnnotations: (ctx, tags=[]) =>
focusAnnotations: (ctx, tags=[]) ->
annotations = getAnnotationsByTags(tags)
annotationUI.focusAnnotations(annotations)
toggleAnnotationSelection: (ctx, tags=[]) =>
toggleAnnotationSelection: (ctx, tags=[]) ->
annotations = getAnnotationsByTags(tags)
annotationUI.selectAnnotations(annotations)
setTool: (ctx, name) =>
setTool: (ctx, name) ->
annotationUI.tool = name
bridge.notify(method: 'setTool', params: name)
setVisibleHighlights: (ctx, state) =>
setVisibleHighlights: (ctx, state) ->
annotationUI.visibleHighlights = Boolean(state)
bridge.notify(method: 'setVisibleHighlights', params: state)
......@@ -49,3 +49,5 @@ class AnnotationUISync
params: annotationUI.visibleHighlights
bridge.onConnect(onConnect)
angular.module('h').value('AnnotationUISync', AnnotationUISync)
......@@ -38,8 +38,8 @@ class CrossFrameService
new AnnotationSync(options, bridge)
createAnnotationUISync = (bridge) ->
new AnnotationUISync($rootScope, $window, bridge, annotationUI)
createAnnotationUISync = (bridge, annotationSync) ->
new AnnotationUISync($window, bridge, annotationSync, annotationUI)
addProvider = (channel) =>
provider = {channel: channel, entities: []}
......@@ -57,7 +57,7 @@ class CrossFrameService
bridge.onConnect(addProvider)
annotationSync = createAnnotationSync(bridge)
annotationUISync = createAnnotationUISync(bridge)
annotationUISync = createAnnotationUISync(bridge, annotationSync)
onDiscoveryCallback = (source, origin, token) ->
bridge.createChannel(source, origin, token)
......
assert = chai.assert
sinon.assert.expose(assert, prefix: '')
describe 'AnnotationUISync', ->
sandbox = sinon.sandbox.create()
uiSync = null
fakeBridge = null
createAnnotationUISync = null
PARENT_WINDOW = {}
beforeEach module('h')
beforeEach inject (AnnotationUISync) ->
fakeWindow = parent: PARENT_WINDOW
fakeBridge =
on: sandbox.stub()
notify: sandbox.stub()
onConnect: sandbox.stub()
fakeAnnotationSync =
getAnnotationForTag: (x) -> x
fakeAnnotationUI =
focusAnnotations: sandbox.stub()
selectAnnotations: sandbox.stub()
xorSelectedAnnotations: sandbox.stub()
tool: 'comment'
visibleHighlights: false
createAnnotationUISync = ->
new AnnotationUISync(
fakeWindow, fakeBridge, fakeAnnotationSync, fakeAnnotationUI)
afterEach: -> sandbox.restore()
describe 'on bridge connection', ->
describe 'when the source is not the parent window', ->
it 'broadcasts the tool/visibility settings to the channel', ->
channel = notify: sandbox.stub()
fakeBridge.onConnect.callsArgWith(0, channel, {})
createAnnotationUISync()
assert.calledWith(channel.notify, {
method: 'setTool'
params: 'comment'
})
assert.calledWith(channel.notify, {
method: 'setVisibleHighlights'
params: false
})
describe 'when the source is the parent window', ->
it 'does nothing', ->
channel = notify: sandbox.stub()
fakeBridge.onConnect.callsArgWith(0, channel, PARENT_WINDOW)
createAnnotationUISync()
assert.notCalled(channel.notify)
describe 'on "back" event', ->
it 'sends the "hideFrame" message to the host only'
describe 'on "open" event', ->
it 'sends the "showFrame" message to the host only'
describe 'on "showEditor" event', ->
it 'sends the "showFrame" message to the host only'
describe 'on "showAnnotations" event', ->
it 'sends the "showFrame" message to the host only'
it 'updates the annotationUI to include the shown annotations'
describe 'on "focusAnnotations" event', ->
it 'updates the annotationUI to show the provided annotations'
describe 'on "toggleAnnotationSelection" event', ->
it 'updates the annotationUI to show the provided annotations'
describe 'on "setTool" event', ->
it 'updates the annotationUI with the new tool'
it 'notifies the other frames of the change'
describe 'on "setVisibleHighlights" event', ->
it 'updates the annotationUI with the new value'
it 'notifies the other frames of the change'
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