Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
coopwire-hypothesis
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙灵跃 Leon Sun
coopwire-hypothesis
Commits
40aa21bf
Commit
40aa21bf
authored
Feb 02, 2015
by
Aron Carroll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add unit tests for the CrossFrameBridge module
parent
2f25ec88
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
223 additions
and
22 deletions
+223
-22
cross-frame-bridge.coffee
h/static/scripts/cross-frame-bridge.coffee
+3
-0
cross-frame-bridge-test.coffee
tests/js/cross-frame-bridge-test.coffee
+220
-22
No files found.
h/static/scripts/cross-frame-bridge.coffee
View file @
40aa21bf
...
...
@@ -88,6 +88,9 @@ class CrossFrameBridge
return
on
:
(
method
,
callback
)
->
if
@
channelListeners
[
method
]
throw
new
Error
(
"Listener '
#{
method
}
' already bound in CrossFrameBridge"
)
@
channelListeners
[
method
]
=
callback
for
l
in
@
links
l
.
channel
.
bind
method
,
callback
...
...
tests/js/cross-frame-bridge-test.coffee
View file @
40aa21bf
...
...
@@ -7,47 +7,245 @@ describe 'CrossFrameBridge', ->
createChannel
=
null
beforeEach
module
(
'h'
)
beforeEach
module
(
CrossFrameBridge
)
->
beforeEach
inject
(
CrossFrameBridge
)
->
createBridge
=
(
options
)
->
new
CrossFrameBridge
(
options
)
createChannel
->
createChannel
=
->
call
:
sandbox
.
stub
()
bind
:
sandbox
.
stub
()
unbind
:
sandbox
.
stub
()
notify
:
sandbox
.
stub
()
destroy
:
sandbox
.
stub
()
sandbox
.
stub
(
Channel
,
'build'
)
afterEach
->
sandbox
.
restore
()
describe
'.createChannel'
,
->
it
'creates a new channel with the provided options'
it
'adds the channel to the .links property'
it
'registers any channelListeners on the channel'
it
'returns the newly created channel'
it
'creates a new channel with the provided options'
,
->
Channel
.
build
.
returns
(
createChannel
())
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
assert
.
called
(
Channel
.
build
)
assert
.
calledWith
(
Channel
.
build
,
{
window
:
'WINDOW'
origin
:
'ORIGIN'
scope
:
'crossFrameBridgeTOKEN'
onReady
:
sinon
.
match
.
func
})
it
'adds the channel to the .links property'
,
->
channel
=
createChannel
()
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
assert
.
include
(
bridge
.
links
,
{
channel
:
channel
,
window
:
'WINDOW'
})
it
'registers any existing listeners on the channel'
,
->
channel
=
createChannel
()
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
bridge
.
on
(
'message1'
,
sinon
.
spy
())
bridge
.
on
(
'message2'
,
sinon
.
spy
())
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
assert
.
called
(
channel
.
bind
)
assert
.
calledWith
(
channel
.
bind
,
'message1'
,
sinon
.
match
.
func
)
assert
.
calledWith
(
channel
.
bind
,
'message2'
,
sinon
.
match
.
func
)
it
'returns the newly created channel'
,
->
channel
=
createChannel
()
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
ret
=
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
assert
.
equal
(
ret
,
channel
)
describe
'.call'
,
->
it
'forwards the call to every created channel'
it
'provides a timeout of 1000ms'
it
'calls options.callback when all channels return successfully'
it
'calls options.callback with an error when one or more channels fail'
it
'destroys the channel when a call fails'
it
'removes the channel from the .links array when a call fails'
it
'treats a timeout as a success with no result'
it
'returns a promise object'
it
'forwards the call to every created channel'
,
->
channel
=
createChannel
()
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
call
({
method
:
'method1'
,
params
:
'params1'
})
assert
.
called
(
channel
.
call
)
message
=
channel
.
call
.
lastCall
.
args
[
0
]
assert
.
equal
(
message
.
method
,
'method1'
)
assert
.
equal
(
message
.
params
,
'params1'
)
it
'provides a timeout'
,
->
channel
=
createChannel
()
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
call
({
method
:
'method1'
,
params
:
'params1'
})
message
=
channel
.
call
.
lastCall
.
args
[
0
]
assert
.
isNumber
(
message
.
timeout
)
it
'calls options.callback when all channels return successfully'
,
->
channel
=
createChannel
()
channel
.
call
.
yieldsTo
(
'success'
,
'result'
)
Channel
.
build
.
returns
(
channel
)
callback
=
sandbox
.
stub
()
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
call
({
method
:
'method1'
,
params
:
'params1'
,
callback
:
callback
})
assert
.
called
(
callback
)
assert
.
calledWith
(
callback
,
null
,
[
'result'
])
it
'calls options.callback with an error when one or more channels fail'
,
->
err
=
new
Error
(
'Uh oh'
)
channel
=
createChannel
()
channel
.
call
.
yieldsTo
(
'error'
,
err
,
'A reason for the error'
)
Channel
.
build
.
returns
(
channel
)
callback
=
sandbox
.
stub
()
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
call
({
method
:
'method1'
,
params
:
'params1'
,
callback
:
callback
})
assert
.
called
(
callback
)
assert
.
calledWith
(
callback
,
err
)
it
'destroys the channel when a call fails'
,
->
channel
=
createChannel
()
channel
.
call
.
yieldsTo
(
'error'
,
new
Error
(
''
),
'A reason for the error'
)
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
call
({
method
:
'method1'
,
params
:
'params1'
,
callback
:
sandbox
.
stub
()})
assert
.
called
(
channel
.
destroy
)
it
'no longer publishes to a channel that has had an errored response'
,
->
channel
=
createChannel
()
channel
.
call
.
yieldsTo
(
'error'
,
new
Error
(
''
),
'A reason for the error'
)
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
call
({
method
:
'method1'
,
params
:
'params1'
,
callback
:
sandbox
.
stub
()})
bridge
.
call
({
method
:
'method1'
,
params
:
'params1'
,
callback
:
sandbox
.
stub
()})
assert
.
calledOnce
(
channel
.
call
)
it
'treats a timeout as a success with no result'
,
->
channel
=
createChannel
()
channel
.
call
.
yieldsTo
(
'error'
,
'timeout_error'
,
'timeout'
)
Channel
.
build
.
returns
(
channel
)
callback
=
sandbox
.
stub
()
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
call
({
method
:
'method1'
,
params
:
'params1'
,
callback
:
callback
})
assert
.
called
(
callback
)
assert
.
calledWith
(
callback
,
null
,
[
null
])
it
'returns a promise object'
,
->
channel
=
createChannel
()
channel
.
call
.
yieldsTo
(
'error'
,
'timeout_error'
,
'timeout'
)
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
ret
=
bridge
.
call
({
method
:
'method1'
,
params
:
'params1'
})
assert
.
isFunction
(
ret
.
then
)
describe
'.notify'
,
->
it
'publishes the message on every created channel'
it
'publishes the message on every created channel'
,
->
channel
=
createChannel
()
message
=
{
method
:
'message1'
,
params
:
'params'
}
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
notify
(
message
)
assert
.
called
(
channel
.
notify
)
assert
.
calledWith
(
channel
.
notify
,
message
)
describe
'.on'
,
->
it
'registers an event listener on all created channels'
it
'adds the message to the .channelListeners property'
it
'only allows one message to be registered per method'
it
'registers an event listener on all created channels'
,
->
channel
=
createChannel
()
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
on
(
'message1'
,
sandbox
.
spy
())
assert
.
called
(
channel
.
bind
)
assert
.
calledWith
(
channel
.
bind
,
'message1'
,
sinon
.
match
.
func
)
it
'only allows one message to be registered per method'
,
->
bridge
=
createBridge
()
bridge
.
on
(
'message1'
,
sandbox
.
spy
())
assert
.
throws
->
bridge
.
on
(
'message1'
,
sandbox
.
spy
())
describe
'.off'
,
->
it
'removes the event listener from the created channels'
it
'removes the method from th .channelListeners property'
it
'removes the event listener from the created channels'
,
->
channel
=
createChannel
()
Channel
.
build
.
returns
(
channel
)
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
off
(
'message1'
,
sandbox
.
spy
())
it
'ensures that the event is no longer bound when new channels are created'
,
->
channel1
=
createChannel
()
channel2
=
createChannel
()
Channel
.
build
.
returns
(
channel1
)
bridge
=
createBridge
()
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
bridge
.
off
(
'message1'
,
sandbox
.
spy
())
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
assert
.
notCalled
(
channel2
.
bind
)
describe
'.onConnect'
,
->
it
'adds a callback that is called when a new channel is connected'
it
'allows multiple callbacks to be registered'
it
'adds a callback that is called when a new channel is connected'
,
->
channel
=
createChannel
()
Channel
.
build
.
returns
(
channel
)
Channel
.
build
.
yieldsTo
(
'onReady'
,
channel
)
callback
=
sandbox
.
stub
()
bridge
=
createBridge
()
bridge
.
onConnect
(
callback
)
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
assert
.
called
(
callback
)
assert
.
calledWith
(
callback
,
channel
)
it
'allows multiple callbacks to be registered'
,
->
channel
=
createChannel
()
Channel
.
build
.
returns
(
channel
)
Channel
.
build
.
yieldsTo
(
'onReady'
,
channel
)
callback1
=
sandbox
.
stub
()
callback2
=
sandbox
.
stub
()
bridge
=
createBridge
()
bridge
.
onConnect
(
callback1
)
bridge
.
onConnect
(
callback2
)
bridge
.
createChannel
(
'WINDOW'
,
'ORIGIN'
,
'TOKEN'
)
assert
.
called
(
callback1
)
assert
.
called
(
callback2
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment