-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Dashlane/add-tests
Add tests
- Loading branch information
Showing
9 changed files
with
464 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import 'should' | ||
|
||
import { TestChannel } from './TestChannel' | ||
import * as sinon from 'sinon' | ||
|
||
describe('GenericChannel', () => { | ||
|
||
it('should call onConnect subscribers when its _connected method is called', () => { | ||
const testInstance = new TestChannel() | ||
const spy = sinon.spy() | ||
testInstance.onConnect(spy) | ||
testInstance.callConnected() | ||
spy.called.should.be.True() | ||
}) | ||
|
||
it('should call onDisconnect subscribers when its _disconnected method is called', () => { | ||
const testInstance = new TestChannel() | ||
const spy = sinon.spy() | ||
testInstance.onDisconnect(spy) | ||
testInstance.callDisconnected() | ||
spy.called.should.be.True() | ||
}) | ||
|
||
it('should call onData callbacks when its _messageReceived method is called', () => { | ||
const testInstance = new TestChannel() | ||
const spy = sinon.spy() | ||
testInstance.onData(spy) | ||
testInstance.callMessageReceived() | ||
spy.called.should.be.True() | ||
}) | ||
|
||
it('should call onError callbacks when its _error method is called', () => { | ||
const testInstance = new TestChannel() | ||
const spy = sinon.spy() | ||
testInstance.onError(spy) | ||
testInstance.callError() | ||
spy.called.should.be.True() | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import 'should' | ||
|
||
import {slot} from './../src/Slot' | ||
import {combineEvents, createEventBus} from './../src/Events' | ||
import {GenericChannel} from './../src/Channel' | ||
import {TransportMessage} from './../src/Transport' | ||
import {TestChannel} from './TestChannel' | ||
import * as sinon from 'sinon' | ||
|
||
describe('combineEvents()', () => { | ||
|
||
it('should correctly combine several EventDeclarations', () => { | ||
const combined = combineEvents( | ||
{ | ||
hello: slot<{ name: string }>() | ||
}, | ||
{ | ||
how: slot<{ mode: 'simple' | 'advanced'}>(), | ||
are: slot<{ tense: number }>() | ||
}, | ||
{ | ||
you: slot<{ reflective: boolean }>() | ||
} | ||
) | ||
Object.keys(combined).should.eql(['hello', 'how', 'are', 'you']) | ||
|
||
// Uncomment the following to check that combineEvents | ||
// does preserve each slot's typings: they contain type errors | ||
// combined.hello({ name: 5 }) | ||
// combined.how({ mode: 'smiple' }) | ||
// combined.are({ tense: true }) | ||
// combined.you({ reflective: 5 }) | ||
}) | ||
|
||
}) | ||
|
||
describe('createEventBus()', () => { | ||
|
||
const events = { | ||
numberToString: slot<number, string>() | ||
} | ||
|
||
it('should correctly create an event bus with no channels', async () => { | ||
|
||
// Attempting to use the events without having | ||
// created an event bus fails | ||
const bad = () => events.numberToString(5) | ||
bad.should.throw(/Slot not connected/) | ||
|
||
// After creating an event bus, events can be | ||
// subscribed to and triggered | ||
const eventBus = createEventBus({ events }) | ||
eventBus.numberToString.on(num => num.toString()) | ||
const res = await eventBus.numberToString(5) | ||
res.should.eql('5') | ||
}) | ||
|
||
it('should connect the channels passed as argument', async () => { | ||
|
||
const channel = new TestChannel() | ||
const eventBus = createEventBus({ | ||
events, | ||
channels: [ channel ] | ||
}) | ||
channel.callConnected() | ||
|
||
// When a handler is added locally, a message should be | ||
// sent through the Channel to signal the registration | ||
eventBus.numberToString.on(num => num.toString()) | ||
channel.sendSpy.calledWith({ | ||
type: 'handler_registered', | ||
slotName: 'numberToString' | ||
}).should.be.True() | ||
|
||
// When a request is sent from the Channel, it should | ||
// be treated and a message sent in response | ||
channel.fakeReceive({ | ||
type: 'request', | ||
slotName: 'numberToString', | ||
id: '0', | ||
data: 5 | ||
}) | ||
|
||
await Promise.resolve() // yied to ts-event-bus internals | ||
channel.sendSpy.calledWith({ | ||
type: 'response', | ||
slotName: 'numberToString', | ||
id: '0', | ||
data: '5' | ||
}).should.be.True() | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.