Factor out repeated event emitter mocks

This commit is contained in:
Robin 2024-09-18 23:05:31 -04:00
parent 156f1e3a10
commit 25193f467a

View File

@ -63,58 +63,40 @@ export function withTestScheduler(
); );
} }
export function mockMember(member: Partial<RoomMember>): RoomMember { interface EmitterMock<T> {
on: () => T;
off: () => T;
addListener: () => T;
removeListener: () => T;
}
function mockEmitter<T>(): EmitterMock<T> {
return { return {
on() { on(): T {
return this; return this as T;
}, },
off() { off(): T {
return this; return this as T;
}, },
addListener() { addListener(): T {
return this; return this as T;
}, },
removeListener() { removeListener(): T {
return this; return this as T;
}, },
...member, };
} as RoomMember; }
export function mockMember(member: Partial<RoomMember>): RoomMember {
return { ...mockEmitter(), ...member } as RoomMember;
} }
export function mockMatrixRoom(room: Partial<MatrixRoom>): MatrixRoom { export function mockMatrixRoom(room: Partial<MatrixRoom>): MatrixRoom {
return { return { ...mockEmitter(), ...room } as Partial<MatrixRoom> as MatrixRoom;
on() {
return this as MatrixRoom;
},
off() {
return this as MatrixRoom;
},
addEventListener() {
return this as MatrixRoom;
},
removeEventListener() {
return this as MatrixRoom;
},
...room,
} as Partial<MatrixRoom> as MatrixRoom;
} }
export function mockLivekitRoom(room: Partial<LivekitRoom>): LivekitRoom { export function mockLivekitRoom(room: Partial<LivekitRoom>): LivekitRoom {
return { return { ...mockEmitter(), ...room } as Partial<LivekitRoom> as LivekitRoom;
on() {
return this as LivekitRoom;
},
off() {
return this as LivekitRoom;
},
addEventListener() {
return this as LivekitRoom;
},
removeEventListener() {
return this as LivekitRoom;
},
...room,
} as Partial<LivekitRoom> as LivekitRoom;
} }
export function mockLocalParticipant( export function mockLocalParticipant(
@ -124,18 +106,7 @@ export function mockLocalParticipant(
isLocal: true, isLocal: true,
getTrackPublication: () => getTrackPublication: () =>
({}) as Partial<LocalTrackPublication> as LocalTrackPublication, ({}) as Partial<LocalTrackPublication> as LocalTrackPublication,
on() { ...mockEmitter(),
return this as LocalParticipant;
},
off() {
return this as LocalParticipant;
},
addListener() {
return this as LocalParticipant;
},
removeListener() {
return this as LocalParticipant;
},
...participant, ...participant,
} as Partial<LocalParticipant> as LocalParticipant; } as Partial<LocalParticipant> as LocalParticipant;
} }
@ -165,18 +136,7 @@ export function mockRemoteParticipant(
setVolume() {}, setVolume() {},
getTrackPublication: () => getTrackPublication: () =>
({}) as Partial<RemoteTrackPublication> as RemoteTrackPublication, ({}) as Partial<RemoteTrackPublication> as RemoteTrackPublication,
on() { ...mockEmitter(),
return this;
},
off() {
return this;
},
addListener() {
return this;
},
removeListener() {
return this;
},
...participant, ...participant,
} as RemoteParticipant; } as RemoteParticipant;
} }