Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Wiaderek authored and Tomasz Wiaderek committed Jan 16, 2024
1 parent 75251c6 commit e8417cd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
29 changes: 9 additions & 20 deletions apps/server/src/modules/tldraw/repo/tldraw-board.repo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand Down Expand Up @@ -69,7 +64,7 @@ describe('TldrawBoardRepo', () => {
app.useWebSocketAdapter(new WsAdapter(app));
await app.init();

// jest.useFakeTimers();
jest.useFakeTimers();
});

afterEach(() => {
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand Down
6 changes: 3 additions & 3 deletions apps/server/src/modules/tldraw/repo/tldraw-board.repo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export class TldrawBoardRepo {
return yDoc;
}

public updateStoredDocWithDiff(docName: string, diff: Uint8Array): void {
public async updateStoredDocWithDiff(docName: string, diff: Uint8Array): Promise<void> {
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;
});
Expand All @@ -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));

Expand Down

0 comments on commit e8417cd

Please sign in to comment.