diff --git a/apps/server/src/modules/tldraw/repo/tldraw-board.repo.spec.ts b/apps/server/src/modules/tldraw/repo/tldraw-board.repo.spec.ts index 93f4951157e..02ac7e1280a 100644 --- a/apps/server/src/modules/tldraw/repo/tldraw-board.repo.spec.ts +++ b/apps/server/src/modules/tldraw/repo/tldraw-board.repo.spec.ts @@ -30,11 +30,6 @@ describe('TldrawBoardRepo', () => { const gatewayPort = 3346; const wsUrl = TestConnection.getWsUrl(gatewayPort); - const delay = (ms: number) => - new Promise((resolve) => { - setTimeout(resolve, ms); - }); - beforeAll(async () => { const testingModule = await Test.createTestingModule({ imports: [ @@ -69,7 +64,7 @@ describe('TldrawBoardRepo', () => { app.useWebSocketAdapter(new WsAdapter(app)); await app.init(); - // jest.useFakeTimers(); + jest.useFakeTimers(); }); afterEach(() => { @@ -202,12 +197,12 @@ describe('TldrawBoardRepo', () => { }; }; - it('should call store update method', () => { + it('should call store update method', async () => { const { calculateDiffSpy } = setup(); const storeUpdateSpy = jest.spyOn(repo.mdb, 'storeUpdateTransactional').mockResolvedValueOnce(1); const diffArray = new Uint8Array(); - repo.updateStoredDocWithDiff('test', diffArray); + await repo.updateStoredDocWithDiff('test', diffArray); expect(storeUpdateSpy).toHaveBeenCalled(); calculateDiffSpy.mockRestore(); @@ -216,17 +211,11 @@ describe('TldrawBoardRepo', () => { it('should log error if update fails', async () => { const { calculateDiffSpy, errorLogSpy } = setup(); - const storeUpdateSpy = jest.spyOn(repo.mdb, 'storeUpdateTransactional').mockImplementationOnce(() => { - throw new Error('test error'); - }); + const storeUpdateSpy = jest + .spyOn(repo.mdb, 'storeUpdateTransactional') + .mockRejectedValueOnce(new Error('test error')); const diffArray = new Uint8Array(); - try { - repo.updateStoredDocWithDiff('test', diffArray); - } catch (e) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access - expect(e.message).toMatch('test error'); - } - await delay(100); + await expect(repo.updateStoredDocWithDiff('test', diffArray)).rejects.toThrow('test error'); expect(storeUpdateSpy).toHaveBeenCalled(); expect(errorLogSpy).toHaveBeenCalled(); @@ -246,11 +235,11 @@ describe('TldrawBoardRepo', () => { }; }; - it('should not call store update method', () => { + it('should not call store update method', async () => { const { storeUpdateSpy, calculateDiffSpy } = setup(); const diffArray = new Uint8Array(); - repo.updateStoredDocWithDiff('test', diffArray); + await repo.updateStoredDocWithDiff('test', diffArray); expect(storeUpdateSpy).not.toHaveBeenCalled(); calculateDiffSpy.mockRestore(); diff --git a/apps/server/src/modules/tldraw/repo/tldraw-board.repo.ts b/apps/server/src/modules/tldraw/repo/tldraw-board.repo.ts index 262863c94b1..a88417366f1 100644 --- a/apps/server/src/modules/tldraw/repo/tldraw-board.repo.ts +++ b/apps/server/src/modules/tldraw/repo/tldraw-board.repo.ts @@ -21,10 +21,10 @@ export class TldrawBoardRepo { return yDoc; } - public updateStoredDocWithDiff(docName: string, diff: Uint8Array): void { + public async updateStoredDocWithDiff(docName: string, diff: Uint8Array): Promise { const calc = calculateDiff(diff); if (calc > 0) { - this.mdb.storeUpdateTransactional(docName, diff).catch((err) => { + await this.mdb.storeUpdateTransactional(docName, diff).catch((err) => { this.logger.warning(new MongoTransactionErrorLoggable(err as Error)); throw err; }); @@ -35,7 +35,7 @@ export class TldrawBoardRepo { const persistedYdoc = await this.getYDocFromMdb(docName); const persistedStateVector = encodeStateVector(persistedYdoc); const diff = encodeStateAsUpdate(ydoc, persistedStateVector); - this.updateStoredDocWithDiff(docName, diff); + await this.updateStoredDocWithDiff(docName, diff); applyUpdate(ydoc, encodeStateAsUpdate(persistedYdoc));