From 7a896f69fdc9b26eae3d275832266c59506b472e Mon Sep 17 00:00:00 2001 From: Half-Shot Date: Tue, 17 Dec 2024 15:02:47 +0000 Subject: [PATCH] fixup tests with fake timers --- src/reactions/useReactionsReader.test.tsx | 17 +++++++++++------ src/reactions/useReactionsReader.ts | 3 +++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/reactions/useReactionsReader.test.tsx b/src/reactions/useReactionsReader.test.tsx index 4838b257..c5e224d2 100644 --- a/src/reactions/useReactionsReader.test.tsx +++ b/src/reactions/useReactionsReader.test.tsx @@ -259,14 +259,15 @@ test("handles a reaction", () => { const reactionEventId = "$my_event_id:example.org"; const reaction = ReactionSet[1]; + vitest.useFakeTimers(); vitest.setSystemTime(0); - withTestScheduler(({ schedule, expectObservable }) => { + withTestScheduler(({ schedule, time, expectObservable }) => { renderHook(() => { const { reactions$ } = useReactionsReader( rtcSession as unknown as MatrixRTCSession, ); - schedule("ab", { + schedule(`abc`, { a: () => {}, b: () => { rtcSession.room.emit( @@ -291,7 +292,7 @@ test("handles a reaction", () => { ); }, c: () => { - vitest.advanceTimersByTime(5000); + vitest.advanceTimersByTime(REACTION_ACTIVE_TIME_MS); }, }); expectObservable(reactions$).toBe( @@ -440,6 +441,7 @@ test("that reactions cannot be spammed", () => { const reactionA = ReactionSet[1]; const reactionB = ReactionSet[2]; + vitest.useFakeTimers(); vitest.setSystemTime(0); withTestScheduler(({ schedule, expectObservable }) => { @@ -447,7 +449,7 @@ test("that reactions cannot be spammed", () => { const { reactions$ } = useReactionsReader( rtcSession as unknown as MatrixRTCSession, ); - schedule("abc", { + schedule("abcd", { a: () => {}, b: () => { rtcSession.room.emit( @@ -493,9 +495,12 @@ test("that reactions cannot be spammed", () => { {} as IRoomTimelineData, ); }, + d: () => { + vitest.advanceTimersByTime(REACTION_ACTIVE_TIME_MS); + }, }); expectObservable(reactions$).toBe( - `ab ${REACTION_ACTIVE_TIME_MS - 1}ms c`, + `ab- ${REACTION_ACTIVE_TIME_MS - 2}ms d`, { a: {}, b: { @@ -504,7 +509,7 @@ test("that reactions cannot be spammed", () => { expireAfter: new Date(REACTION_ACTIVE_TIME_MS), }, }, - c: {}, + d: {}, }, ); }); diff --git a/src/reactions/useReactionsReader.ts b/src/reactions/useReactionsReader.ts index 265b3113..be3f865d 100644 --- a/src/reactions/useReactionsReader.ts +++ b/src/reactions/useReactionsReader.ts @@ -208,6 +208,9 @@ export default function useReactionsReader(rtcSession: MatrixRTCSession): { const currentReactions = reactionsSubject$.current.value; if (currentReactions[identifier]) { // We've still got a reaction from this user, ignore it to prevent spamming + logger.warn( + `Got reaction from ${identifier} but one is still playing`, + ); return; } reactionsSubject$.current.next({