Skip to content

Commit

Permalink
feat: Ability to send realtime event
Browse files Browse the repository at this point in the history
  • Loading branch information
ptbrowne committed Jan 9, 2020
1 parent c98e84a commit dc95cb9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/cozy-realtime/src/CozyRealtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,16 @@ class CozyRealtime {
this._resetSocket()
}

async send(doctype, id, data) {
return this._cozyClient.stackClient.fetchJSON(
'POST',
`/realtime/${doctype}/${id}`,
{
data
}
)
}

_getEventKeys() {
if (!this._events) return []

Expand Down
21 changes: 20 additions & 1 deletion packages/cozy-realtime/src/CozyRealtime.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,17 @@ import MicroEE from 'microee'
const COZY_URL = 'http://cozy.tools:8888'
const WS_URL = 'ws://cozy.tools:8888/realtime/'
const COZY_TOKEN = 'zvNpzsHILcXpnDBlUfmAqVuEEuyWvPYn'

class CozyClient {
stackClient = {
uri: COZY_URL,
token: {
token: COZY_TOKEN
}
},
fetchJSON: jest.fn()
}
}

MicroEE.mixin(CozyClient)
const cozyClient = new CozyClient()
const pause = time => new Promise(resolve => setTimeout(resolve, time))
Expand Down Expand Up @@ -291,3 +294,19 @@ describe('getWebSocketToken', () => {
expect(getWebSocketToken(fakeCozyClient)).toBe('token2')
})
})

describe('send', () => {
beforeEach(() => {
cozyClient.stackClient.fetchJSON.mockReset()
})

it('should send a message', async () => {
const realtime = new CozyRealtime({ client: cozyClient })
await realtime.send('io.cozy.doctype', 'my-id', { message: 'hello' })
expect(cozyClient.stackClient.fetchJSON).toHaveBeenCalledWith(
'POST',
'/realtime/io.cozy.doctype/my-id',
{ data: { message: 'hello' } }
)
})
})

0 comments on commit dc95cb9

Please sign in to comment.