Skip to content

Commit

Permalink
fix: add testcase of unsub
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzhang1030 committed May 13, 2024
1 parent ff68cdd commit 5b75e7a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 12 additions & 1 deletion src/__test__/send.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ describe('send', () => {
throw new Error('a')
})

wshe.subscribeRaw(ev => (event = ev))
const unsubscribe = wshe.subscribeRaw(ev => (event = ev))
wshe.sendRaw(eventData)
await vi.waitFor(() => {
vi.setSystemTime(date)
Expand All @@ -127,5 +127,16 @@ describe('send', () => {

expect(event).instanceof(Blob)
expect(event).toStrictEqual(new Blob([eventData]))

event = undefined
unsubscribe()
wshe.sendRaw(eventData)
await expect(new Promise<void>((_, reject) => {
vi.waitFor(() => {
vi.setSystemTime(date)
if (event === undefined)
reject(new Error('Binary Data received back'))
})
})).rejects.toThrow()
})
})
2 changes: 0 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ export function createWSHE<
* @returns unsubscribe function
*/
subscribeRaw<T extends DataTypes>(callback: (data: T) => void) {
if (!ws)
return
emitter.on(RAW_EVENT, callback)
return () => {
emitter.off(RAW_EVENT, callback)
Expand Down

0 comments on commit 5b75e7a

Please sign in to comment.