-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sync): only clear queue after successful send
Also add a unit test for the websocket polyfill Signed-off-by: Max <[email protected]>
- Loading branch information
1 parent
d9faf1c
commit 1911a14
Showing
2 changed files
with
132 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import initWebSocketPolyfill from '../../services/WebSocketPolyfill.js' | ||
|
||
describe('Init function', () => { | ||
|
||
it('returns a websocket polyfill class', () => { | ||
const syncService = { on: jest.fn(), open: jest.fn() } | ||
const Polyfill = initWebSocketPolyfill(syncService) | ||
const websocket = new Polyfill('url') | ||
expect(websocket).toBeInstanceOf(Polyfill) | ||
}) | ||
|
||
it('registers handlers', () => { | ||
const syncService = { on: jest.fn(), open: jest.fn() } | ||
const Polyfill = initWebSocketPolyfill(syncService) | ||
const websocket = new Polyfill('url') | ||
expect(syncService.on).toHaveBeenCalled() | ||
}) | ||
|
||
it('opens sync service', () => { | ||
const syncService = { on: jest.fn(), open: jest.fn() } | ||
const fileId = 123 | ||
const initialSession = { } | ||
const Polyfill = initWebSocketPolyfill(syncService, fileId, initialSession) | ||
const websocket = new Polyfill('url') | ||
expect(syncService.open).toHaveBeenCalledWith({ fileId, initialSession }) | ||
}) | ||
|
||
it('sends steps to sync service', async () => { | ||
const syncService = { | ||
on: jest.fn(), | ||
open: jest.fn(), | ||
sendSteps: async getData => getData(), | ||
} | ||
const queue = [ 'initial' ] | ||
const data = { dummy: 'data' } | ||
const Polyfill = initWebSocketPolyfill(syncService, null, null, queue) | ||
const websocket = new Polyfill('url') | ||
const result = websocket.send(data) | ||
expect(result).toBeInstanceOf(Promise) | ||
expect(queue).toEqual([ 'initial' , data ]) | ||
const dataSendOut = await result | ||
expect(queue).toEqual([]) | ||
expect(dataSendOut).toHaveProperty('awareness') | ||
expect(dataSendOut).toHaveProperty('steps') | ||
expect(dataSendOut).toHaveProperty('version') | ||
}) | ||
|
||
it('handles early reject', async () => { | ||
const syncService = { | ||
on: jest.fn(), | ||
open: jest.fn(), | ||
sendSteps: jest.fn().mockRejectedValue('error before reading steps in sync service'), | ||
} | ||
const queue = [ 'initial' ] | ||
const data = { dummy: 'data' } | ||
const Polyfill = initWebSocketPolyfill(syncService, null, null, queue) | ||
const websocket = new Polyfill('url') | ||
const result = websocket.send(data) | ||
expect(queue).toEqual([ 'initial' , data ]) | ||
expect(result).toBeInstanceOf(Promise) | ||
const returned = await result | ||
expect(returned).toBeUndefined() | ||
expect(queue).toEqual([ 'initial' , data ]) | ||
}) | ||
|
||
it('handles reject after reading data', async () => { | ||
const syncService = { | ||
on: jest.fn(), | ||
open: jest.fn(), | ||
sendSteps: jest.fn().mockImplementation( async getData => { | ||
getData() | ||
throw 'error when sending in sync service' | ||
}), | ||
} | ||
const queue = [ 'initial' ] | ||
const data = { dummy: 'data' } | ||
const Polyfill = initWebSocketPolyfill(syncService, null, null, queue) | ||
const websocket = new Polyfill('url') | ||
const result = websocket.send(data) | ||
expect(queue).toEqual([ 'initial' , data ]) | ||
expect(result).toBeInstanceOf(Promise) | ||
const returned = await result | ||
expect(returned).toBeUndefined() | ||
expect(queue).toEqual([ 'initial' , data ]) | ||
}) | ||
|
||
it('queue survives a close', async () => { | ||
const syncService = { | ||
on: jest.fn(), | ||
open: jest.fn(), | ||
sendSteps: jest.fn().mockImplementation( async getData => { | ||
getData() | ||
throw 'error when sending in sync service' | ||
}), | ||
sendStepsNow: jest.fn().mockImplementation( async getData => { | ||
getData() | ||
throw 'sendStepsNow error when sending' | ||
}), | ||
off: jest.fn(), | ||
close: jest.fn( async data => data ), | ||
} | ||
const queue = [ 'initial' ] | ||
const data = { dummy: 'data' } | ||
const Polyfill = initWebSocketPolyfill(syncService, null, null, queue) | ||
const websocket = new Polyfill('url') | ||
websocket.onclose = jest.fn() | ||
await websocket.send(data) | ||
const promise = websocket.close() | ||
expect(queue).toEqual([ 'initial' , data ]) | ||
await promise | ||
expect(queue).toEqual([ 'initial' , data ]) | ||
}) | ||
|
||
}) |