Skip to content

Commit

Permalink
Improve tests after merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
blazejpass committed Oct 18, 2023
1 parent 015c300 commit f11d39f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
8 changes: 4 additions & 4 deletions apps/server/src/modules/tldraw/repo/tldraw-board.repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ describe('TldrawBoardRepo', () => {
doc.conns.set(ws, wsSet);
const storeGetYDocSpy = jest
.spyOn(repo.mdb, 'getYDoc')
.mockImplementation(() => new WsSharedDocDo('TEST', service));
const storeUpdateSpy = jest.spyOn(repo.mdb, 'storeUpdate').mockImplementation(() => {});
.mockImplementation(() => Promise.resolve(new WsSharedDocDo('TEST', service)));
const storeUpdateSpy = jest.spyOn(repo.mdb, 'storeUpdate').mockImplementation(() => Promise.resolve(1));

return {
doc,
Expand Down Expand Up @@ -111,10 +111,10 @@ describe('TldrawBoardRepo', () => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
doc.conns.set(ws, wsSet);
const storeUpdateSpy = jest.spyOn(repo.mdb, 'storeUpdate').mockImplementation(() => {});
const storeUpdateSpy = jest.spyOn(repo.mdb, 'storeUpdate').mockImplementation(() => Promise.resolve(1));
const storeGetYDocSpy = jest
.spyOn(repo.mdb, 'getYDoc')
.mockImplementation(() => new WsSharedDocDo('TEST', service));
.mockImplementation(() => Promise.resolve(new WsSharedDocDo('TEST', service)));
const byteArray = new TextEncoder().encode(testMessage);

return {
Expand Down
10 changes: 4 additions & 6 deletions apps/server/src/modules/tldraw/repo/tldraw-board.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export class TldrawBoardRepo {
updateStoredDocWithDiff(docName: string, diff: Uint8Array) {
const clac = calculateDiff(diff);
if (clac > 0) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
this.mdb.storeUpdate(docName, diff);
void this.mdb.storeUpdate(docName, diff);
}
}

Expand All @@ -59,15 +58,14 @@ export class TldrawBoardRepo {

applyUpdate(ydoc, encodeStateAsUpdate(persistedYdoc));

ydoc.on('update', (update) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
this.mdb.storeUpdate(docName, update);
ydoc.on('update', (update: Uint8Array) => {
void this.mdb.storeUpdate(docName, update);
});

persistedYdoc.destroy();
}

async flushDocument(docName) {
async flushDocument(docName: string) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-call,@typescript-eslint/no-unsafe-member-access
await this.mdb.flushDocument(docName);
}
Expand Down
42 changes: 32 additions & 10 deletions apps/server/src/modules/tldraw/service/tldraw.ws.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,22 @@ import { TldrawWs } from '@src/modules/tldraw/controller';
import { TldrawWsService } from '.';
import { TestHelper } from '../helper/test-helper';

describe('TldrawWS', () => {
jest.mock('y-protocols/awareness', () => {
const moduleMock: unknown = {
__esModule: true,
...jest.requireActual('y-protocols/awareness'),
};
return moduleMock;
});
jest.mock('y-protocols/sync', () => {
const moduleMock: unknown = {
__esModule: true,
...jest.requireActual('y-protocols/sync'),
};
return moduleMock;
});

describe('TldrawWSService', () => {
let app: INestApplication;
let ws: WebSocket;
let service: TldrawWsService;
Expand Down Expand Up @@ -172,7 +187,7 @@ describe('TldrawWS', () => {

const sendSpy = jest.spyOn(service, 'send');
const applyAwarenessUpdateSpy = jest.spyOn(AwarenessProtocol, 'applyAwarenessUpdate');
jest.spyOn(SyncProtocols, 'readSyncMessage').mockImplementationOnce((dec, enc) => {
const syncProtocolUpdateSpy = jest.spyOn(SyncProtocols, 'readSyncMessage').mockImplementationOnce((dec, enc) => {
enc.bufs = [new Uint8Array(2), new Uint8Array(2)];
return 1;
});
Expand All @@ -182,24 +197,27 @@ describe('TldrawWS', () => {
return {
sendSpy,
applyAwarenessUpdateSpy,
syncProtocolUpdateSpy,
doc,
msg,
};
};

it('should call send method when received message of type SYNC', async () => {
const { sendSpy, doc, msg } = await setup([0, 1]);
const { sendSpy, applyAwarenessUpdateSpy, syncProtocolUpdateSpy, doc, msg } = await setup([0, 1]);

service.messageHandler(ws, doc, msg);

expect(sendSpy).toHaveBeenCalledTimes(1);

ws.close();
sendSpy.mockRestore();
applyAwarenessUpdateSpy.mockRestore();
syncProtocolUpdateSpy.mockRestore();
});

it('should not call send method when received message of type AWARENESS', async () => {
const { sendSpy, applyAwarenessUpdateSpy, doc, msg } = await setup([1, 1, 0]);
const { sendSpy, applyAwarenessUpdateSpy, syncProtocolUpdateSpy, doc, msg } = await setup([1, 1, 0]);
service.messageHandler(ws, doc, msg);

expect(sendSpy).toHaveBeenCalledTimes(0);
Expand All @@ -208,10 +226,11 @@ describe('TldrawWS', () => {
ws.close();
sendSpy.mockRestore();
applyAwarenessUpdateSpy.mockRestore();
syncProtocolUpdateSpy.mockRestore();
});

it('should do nothing when received message unknown type', async () => {
const { sendSpy, applyAwarenessUpdateSpy, doc, msg } = await setup([2]);
const { sendSpy, applyAwarenessUpdateSpy, syncProtocolUpdateSpy, doc, msg } = await setup([2]);
service.messageHandler(ws, doc, msg);

expect(sendSpy).toHaveBeenCalledTimes(0);
Expand All @@ -220,6 +239,7 @@ describe('TldrawWS', () => {
ws.close();
sendSpy.mockRestore();
applyAwarenessUpdateSpy.mockRestore();
syncProtocolUpdateSpy.mockRestore();
});
});

Expand All @@ -228,7 +248,7 @@ describe('TldrawWS', () => {
ws = await TestHelper.setupWs(wsUrl, 'TEST');

const messageHandlerSpy = jest.spyOn(service, 'messageHandler');
jest.spyOn(SyncProtocols, 'readSyncMessage').mockImplementationOnce((dec, enc) => {
const readSyncMessageSpy = jest.spyOn(SyncProtocols, 'readSyncMessage').mockImplementationOnce((dec, enc) => {
enc.bufs = [new Uint8Array(2), new Uint8Array(2)];
return 1;
});
Expand All @@ -237,11 +257,12 @@ describe('TldrawWS', () => {
return {
messageHandlerSpy,
msg,
readSyncMessageSpy,
};
};

it('should handle message', async () => {
const { messageHandlerSpy, msg } = await setup([0, 1]);
const { messageHandlerSpy, msg, readSyncMessageSpy } = await setup([0, 1]);

service.setupWSConnection(ws);
ws.emit('message', msg);
Expand All @@ -250,6 +271,7 @@ describe('TldrawWS', () => {

ws.close();
messageHandlerSpy.mockRestore();
readSyncMessageSpy.mockRestore();
});
});

Expand All @@ -262,7 +284,7 @@ describe('TldrawWS', () => {
throw new Error('error');
});
const doc = new WsSharedDocDo('TEST', service);
const { msg } = createMessage([0, 1]);
const { msg } = createMessage([0]);

return {
sendSpy,
Expand Down Expand Up @@ -348,7 +370,7 @@ describe('TldrawWS', () => {
const messageHandlerSpy = jest.spyOn(service, 'messageHandler').mockImplementationOnce(() => {});
const sendSpy = jest.spyOn(service, 'send');
const getYDocSpy = jest.spyOn(service, 'getYDoc').mockImplementationOnce(() => doc);
const { msg } = createMessage([0, 1]);
const { msg } = createMessage([0]);
jest.spyOn(AwarenessProtocol, 'encodeAwarenessUpdate').mockImplementationOnce(() => msg);

return {
Expand All @@ -365,7 +387,7 @@ describe('TldrawWS', () => {

await delay(200);

expect(sendSpy).toHaveBeenCalledTimes(3);
expect(sendSpy).toHaveBeenCalledTimes(2);

ws.close();
messageHandlerSpy.mockRestore();
Expand Down

0 comments on commit f11d39f

Please sign in to comment.