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', () => {
let mockedNote: Note;
beforeAll(() => {
jest.useFakeTimers();
});
beforeEach(() => {
mockedNote = Mock.of<Note>({ id: 4711 });
});
afterAll(() => {
jest.useRealTimers();
jest.resetAllMocks();
jest.resetModules();
});

View file

@ -4,7 +4,6 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import {
MessageTransporter,
MockedBackendTransportAdapter,
YDocSyncServerAdapter,
} from '@hedgedoc/commons';
@ -15,6 +14,7 @@ import { Username } from '../../../utils/username';
import { RealtimeConnection } from '../realtime-connection';
import { RealtimeNote } from '../realtime-note';
import { RealtimeUserStatusAdapter } from '../realtime-user-status-adapter';
import { MockMessageTransporter } from './mock-message-transporter';
enum RealtimeUserState {
WITHOUT,
@ -83,7 +83,7 @@ export class MockConnectionBuilder {
public build(): RealtimeConnection {
const displayName = this.deriveDisplayName();
const transporter = new MessageTransporter();
const transporter = new MockMessageTransporter();
transporter.setAdapter(new MockedBackendTransportAdapter(''));
const realtimeUserStateAdapter: RealtimeUserStatusAdapter =
this.includeRealtimeUserStatus === RealtimeUserState.WITHOUT
@ -125,6 +125,9 @@ export class MockConnectionBuilder {
this.realtimeNote.addClient(connection);
transporter.markAsReady();
jest.advanceTimersByTime(0);
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')
}
private stopSendingOfReadyRequests() {
protected stopSendingOfReadyRequests() {
if (this.readyInterval !== undefined) {
clearInterval(this.readyInterval)
this.readyInterval = undefined
@ -242,7 +242,7 @@ export class MessageTransporter extends EventEmitter2<MessageEventPayloadMap> {
this.startSendingOfReadyRequests()
}
private startSendingOfReadyRequests(): void {
protected startSendingOfReadyRequests(): void {
this.readyInterval = setInterval(() => {
this.sendMessage({
type: MessageType.READY_REQUEST