From 25193f467a0f8ecddecb41623186a23cf4b042b7 Mon Sep 17 00:00:00 2001 From: Robin Date: Wed, 18 Sep 2024 23:05:31 -0400 Subject: [PATCH] Factor out repeated event emitter mocks --- src/utils/test.ts | 90 +++++++++++++---------------------------------- 1 file changed, 25 insertions(+), 65 deletions(-) diff --git a/src/utils/test.ts b/src/utils/test.ts index d5ba1f61..b80afded 100644 --- a/src/utils/test.ts +++ b/src/utils/test.ts @@ -63,58 +63,40 @@ export function withTestScheduler( ); } -export function mockMember(member: Partial): RoomMember { +interface EmitterMock { + on: () => T; + off: () => T; + addListener: () => T; + removeListener: () => T; +} + +function mockEmitter(): EmitterMock { return { - on() { - return this; + on(): T { + return this as T; }, - off() { - return this; + off(): T { + return this as T; }, - addListener() { - return this; + addListener(): T { + return this as T; }, - removeListener() { - return this; + removeListener(): T { + return this as T; }, - ...member, - } as RoomMember; + }; +} + +export function mockMember(member: Partial): RoomMember { + return { ...mockEmitter(), ...member } as RoomMember; } export function mockMatrixRoom(room: Partial): MatrixRoom { - return { - on() { - return this as MatrixRoom; - }, - off() { - return this as MatrixRoom; - }, - addEventListener() { - return this as MatrixRoom; - }, - removeEventListener() { - return this as MatrixRoom; - }, - ...room, - } as Partial as MatrixRoom; + return { ...mockEmitter(), ...room } as Partial as MatrixRoom; } export function mockLivekitRoom(room: Partial): LivekitRoom { - return { - on() { - return this as LivekitRoom; - }, - off() { - return this as LivekitRoom; - }, - addEventListener() { - return this as LivekitRoom; - }, - removeEventListener() { - return this as LivekitRoom; - }, - ...room, - } as Partial as LivekitRoom; + return { ...mockEmitter(), ...room } as Partial as LivekitRoom; } export function mockLocalParticipant( @@ -124,18 +106,7 @@ export function mockLocalParticipant( isLocal: true, getTrackPublication: () => ({}) as Partial as LocalTrackPublication, - on() { - return this as LocalParticipant; - }, - off() { - return this as LocalParticipant; - }, - addListener() { - return this as LocalParticipant; - }, - removeListener() { - return this as LocalParticipant; - }, + ...mockEmitter(), ...participant, } as Partial as LocalParticipant; } @@ -165,18 +136,7 @@ export function mockRemoteParticipant( setVolume() {}, getTrackPublication: () => ({}) as Partial as RemoteTrackPublication, - on() { - return this; - }, - off() { - return this; - }, - addListener() { - return this; - }, - removeListener() { - return this; - }, + ...mockEmitter(), ...participant, } as RemoteParticipant; }