Commit efc316dd authored by Robert Knight's avatar Robert Knight

Remove the need for some test-only code in Bridge

The need for this "testing only" code can be removed by improving the way
that we simulate the `connect` message being handled in tests.
parent 803648f3
...@@ -118,7 +118,6 @@ export default class Bridge { ...@@ -118,7 +118,6 @@ export default class Bridge {
const connect = (_token, cb) => { const connect = (_token, cb) => {
if (_token === token) { if (_token === token) {
cb(); cb();
ready(); // This is necessary for testing only.
} }
}; };
......
...@@ -234,14 +234,29 @@ describe('shared/bridge', () => { ...@@ -234,14 +234,29 @@ describe('shared/bridge', () => {
}); });
describe('#onConnect', () => { describe('#onConnect', () => {
// Simulate a Bridge attached to the other end of a channel receiving
// the `connect` RPC request and handling it using the `connect` handler
// registered by the Bridge.
const runConnectHandler = channel => {
const connectCall = channel.call
.getCalls()
.find(call => call.firstArg === 'connect');
// Invoke the `connect` handler. Here we're invoking it on `channel` but
// in the actual app this would be called on the counterpart channel in
// the other frame. This distinction doesn't matter because all the
// handler does is check a token (which is the same on both sides) and
// call the result callback.
channel.methods.connect(...connectCall.args.slice(1));
};
it('runs callbacks when a Window channel connects with correct token', () => { it('runs callbacks when a Window channel connects with correct token', () => {
const onConnectCallback = sinon.stub(); const onConnectCallback = sinon.stub();
bridge.onConnect(onConnectCallback); bridge.onConnect(onConnectCallback);
const channel = createChannel(); const channel = createChannel();
// Simulate "connect" RPC call by Bridge instance in channel's destination frame. runConnectHandler(channel);
channel.methods.connect('TOKEN', sinon.stub());
assert.calledWith(onConnectCallback, channel, fakeWindow); assert.calledWith(onConnectCallback, channel, fakeWindow);
}); });
...@@ -253,7 +268,10 @@ describe('shared/bridge', () => { ...@@ -253,7 +268,10 @@ describe('shared/bridge', () => {
const channel = createChannel(); const channel = createChannel();
// Simulate "connect" RPC call by Bridge instance in channel's destination frame. // Simulate "connect" RPC call by Bridge instance in channel's destination frame.
channel.methods.connect('WRONG-TOKEN', sinon.stub()); const connectCall = channel.call
.getCalls()
.find(call => call.firstArg === 'connect');
channel.methods.connect('WRONG-TOKEN', connectCall.lastArg);
assert.notCalled(onConnectCallback); assert.notCalled(onConnectCallback);
}); });
...@@ -265,13 +283,7 @@ describe('shared/bridge', () => { ...@@ -265,13 +283,7 @@ describe('shared/bridge', () => {
const messageChannel = new MessageChannel(); const messageChannel = new MessageChannel();
const channel = createChannel(messageChannel.port1); const channel = createChannel(messageChannel.port1);
// Simulate a Bridge attached to the other end of the channel receiving runConnectHandler(channel);
// the `connect` RPC request and handling it using the `connect` handler
// registered by the Bridge.
const connectCall = channel.call
.getCalls()
.find(call => call.firstArg === 'connect');
channel.methods.connect(...connectCall.args.slice(1));
assert.calledWith(onConnectCallback, channel); assert.calledWith(onConnectCallback, channel);
}); });
...@@ -284,8 +296,7 @@ describe('shared/bridge', () => { ...@@ -284,8 +296,7 @@ describe('shared/bridge', () => {
const channel = createChannel(); const channel = createChannel();
// Simulate "connect" RPC call by Bridge instance in channel's destination frame. runConnectHandler(channel);
channel.methods.connect('TOKEN', sinon.stub());
assert.calledWith(onConnectCallback1, channel, fakeWindow); assert.calledWith(onConnectCallback1, channel, fakeWindow);
assert.calledWith(onConnectCallback2, channel, fakeWindow); assert.calledWith(onConnectCallback2, channel, fakeWindow);
...@@ -297,9 +308,8 @@ describe('shared/bridge', () => { ...@@ -297,9 +308,8 @@ describe('shared/bridge', () => {
const channel = createChannel(); const channel = createChannel();
// Simulate "connect" RPC call by Bridge instance in channel's destination frame. runConnectHandler(channel);
channel.methods.connect('TOKEN', sinon.stub()); runConnectHandler(channel);
channel.methods.connect('TOKEN', sinon.stub());
assert.calledOnce(onConnectCallback); assert.calledOnce(onConnectCallback);
}); });
......
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