test: add mocked message transporter

Signed-off-by: Tilman Vatteroth <git@tilmanvatteroth.de>
This commit is contained in:
Tilman Vatteroth 2023-07-06 10:03:35 +02:00
parent 233fb263c7
commit eeef0ea025
4 changed files with 33 additions and 4 deletions

View file

@ -13,11 +13,16 @@ import { MockConnectionBuilder } from './test-utils/mock-connection';
describe('realtime note', () => { describe('realtime note', () => {
let mockedNote: Note; let mockedNote: Note;
beforeAll(() => {
jest.useFakeTimers();
});
beforeEach(() => { beforeEach(() => {
mockedNote = Mock.of<Note>({ id: 4711 }); mockedNote = Mock.of<Note>({ id: 4711 });
}); });
afterAll(() => { afterAll(() => {
jest.useRealTimers();
jest.resetAllMocks(); jest.resetAllMocks();
jest.resetModules(); jest.resetModules();
}); });

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { import {
MessageTransporter,
MockedBackendTransportAdapter, MockedBackendTransportAdapter,
YDocSyncServerAdapter, YDocSyncServerAdapter,
} from '@hedgedoc/commons'; } from '@hedgedoc/commons';
@ -15,6 +14,7 @@ import { Username } from '../../../utils/username';
import { RealtimeConnection } from '../realtime-connection'; import { RealtimeConnection } from '../realtime-connection';
import { RealtimeNote } from '../realtime-note'; import { RealtimeNote } from '../realtime-note';
import { RealtimeUserStatusAdapter } from '../realtime-user-status-adapter'; import { RealtimeUserStatusAdapter } from '../realtime-user-status-adapter';
import { MockMessageTransporter } from './mock-message-transporter';
enum RealtimeUserState { enum RealtimeUserState {
WITHOUT, WITHOUT,
@ -83,7 +83,7 @@ export class MockConnectionBuilder {
public build(): RealtimeConnection { public build(): RealtimeConnection {
const displayName = this.deriveDisplayName(); const displayName = this.deriveDisplayName();
const transporter = new MessageTransporter(); const transporter = new MockMessageTransporter();
transporter.setAdapter(new MockedBackendTransportAdapter('')); transporter.setAdapter(new MockedBackendTransportAdapter(''));
const realtimeUserStateAdapter: RealtimeUserStatusAdapter = const realtimeUserStateAdapter: RealtimeUserStatusAdapter =
this.includeRealtimeUserStatus === RealtimeUserState.WITHOUT this.includeRealtimeUserStatus === RealtimeUserState.WITHOUT
@ -125,6 +125,9 @@ export class MockConnectionBuilder {
this.realtimeNote.addClient(connection); this.realtimeNote.addClient(connection);
transporter.markAsReady();
jest.advanceTimersByTime(0);
return connection; return connection;
} }

View file

@ -0,0 +1,21 @@
/*
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { MessageTransporter, MessageType } from '@hedgedoc/commons';
/**
* A message transporter that is only used in testing where certain conditions like resending of requests isn't needed.
*/
export class MockMessageTransporter extends MessageTransporter {
protected startSendingOfReadyRequests(): void {
this.sendMessage({
type: MessageType.READY_REQUEST,
});
}
protected stopSendingOfReadyRequests(): void {
//intentionally left blank
}
}

View file

@ -126,7 +126,7 @@ export class MessageTransporter extends EventEmitter2<MessageEventPayloadMap> {
this.emit('ready') this.emit('ready')
} }
private stopSendingOfReadyRequests() { protected stopSendingOfReadyRequests() {
if (this.readyInterval !== undefined) { if (this.readyInterval !== undefined) {
clearInterval(this.readyInterval) clearInterval(this.readyInterval)
this.readyInterval = undefined this.readyInterval = undefined
@ -242,7 +242,7 @@ export class MessageTransporter extends EventEmitter2<MessageEventPayloadMap> {
this.startSendingOfReadyRequests() this.startSendingOfReadyRequests()
} }
private startSendingOfReadyRequests(): void { protected startSendingOfReadyRequests(): void {
this.readyInterval = setInterval(() => { this.readyInterval = setInterval(() => {
this.sendMessage({ this.sendMessage({
type: MessageType.READY_REQUEST type: MessageType.READY_REQUEST