Skip to content

Commit

Permalink
feat(Realtime): Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kosssi committed May 7, 2019
1 parent f74fe5a commit d5a7573
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/cozy-realtime/src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ describe('CozyRealtime', () => {
cozyStack.emitMessage(type, fakeDoc, 'UPDATED', fakeDoc._id)
})

it('should launch all handler when receive updated document', async () => {
const handler = jest.fn()
await realtime.subscribe('updated', type, handler)
await realtime.subscribe('updated', type, fakeDoc._id, handler)

cozyStack.emitMessage(type, fakeDoc, 'UPDATED', fakeDoc._id)
await pause(10)
expect(handler.mock.calls.length).toBe(2)
})

it('should launch handler when document is deleted', async done => {
await realtime.subscribe('deleted', type, doc => {
expect(doc).toEqual(fakeDoc)
Expand Down Expand Up @@ -160,6 +170,13 @@ describe('CozyRealtime', () => {
expect(realtime._socket.isOpen()).toBe(false)
})

it('should stop receiving created documents with given doctype', () => {
expect(handlerCreate.mock.calls.length).toBe(0)
realtime.unsubscribe('created', type, handlerCreate)
cozyStack.emitMessage(type, fakeDoc, 'CREATED')
expect(handlerCreate.mock.calls.length).toBe(0)
})

it('should unsubscribe all events', async () => {
expect(realtime._socket.isOpen()).toBe(true)
realtime.unsubscribeAll()
Expand Down

0 comments on commit d5a7573

Please sign in to comment.