From 9274c603e534609021ce806d5a7adbd8f1448ed8 Mon Sep 17 00:00:00 2001 From: tjtanjin Date: Thu, 7 Nov 2024 08:44:05 +0800 Subject: [PATCH] fix: Fix stale values in toggle events --- __tests__/hooks/internal/useAudioInternal.test.ts | 14 ++++++++++++++ .../internal/useNotificationsInternal.test.ts | 14 ++++++++++++++ __tests__/hooks/internal/useVoiceInternal.test.ts | 14 ++++++++++++++ src/hooks/internal/useAudioInternal.ts | 2 +- src/hooks/internal/useNotificationsInternal.ts | 2 +- src/hooks/internal/useVoiceInternal.ts | 2 +- 6 files changed, 45 insertions(+), 3 deletions(-) diff --git a/__tests__/hooks/internal/useAudioInternal.test.ts b/__tests__/hooks/internal/useAudioInternal.test.ts index 65c0600..1725436 100644 --- a/__tests__/hooks/internal/useAudioInternal.test.ts +++ b/__tests__/hooks/internal/useAudioInternal.test.ts @@ -51,6 +51,20 @@ describe("useAudioInternal Hook", () => { // checks if audio state was updated expect(result.current.audioToggledOn).toBe(!initialAudioToggledOn); + + // simulates clicking the toggle action + await act(async () => { + await result.current.toggleAudio(); + }); + + // checks if callRcbEvent was called with rcb-toggle-audio and correct arguments + expect(callRcbEventMock).toHaveBeenCalledWith(RcbEvent.TOGGLE_AUDIO, { + currState: !initialAudioToggledOn, + newState: initialAudioToggledOn, + }); + + // check if voice state was updated + expect(result.current.audioToggledOn).toBe(initialAudioToggledOn); }); it("should prevent toggling when event is defaultPrevented", async () => { diff --git a/__tests__/hooks/internal/useNotificationsInternal.test.ts b/__tests__/hooks/internal/useNotificationsInternal.test.ts index 5c4ac0c..fca225e 100644 --- a/__tests__/hooks/internal/useNotificationsInternal.test.ts +++ b/__tests__/hooks/internal/useNotificationsInternal.test.ts @@ -51,6 +51,20 @@ describe("useNotificationsInternal Hook", () => { // checks if notifications state was updated expect(result.current.notificationsToggledOn).toBe(!initialNotificationsToggledOn); + + // simulates clicking the toggle action + await act(async () => { + await result.current.toggleNotifications(); + }); + + // checks if callRcbEvent was called with rcb-toggle-audio and correct arguments + expect(callRcbEventMock).toHaveBeenCalledWith(RcbEvent.TOGGLE_NOTIFICATIONS, { + currState: !initialNotificationsToggledOn, + newState: initialNotificationsToggledOn, + }); + + // check if voice state was updated + expect(result.current.notificationsToggledOn).toBe(initialNotificationsToggledOn); }); it("should prevent toggling when event is defaultPrevented", async () => { diff --git a/__tests__/hooks/internal/useVoiceInternal.test.ts b/__tests__/hooks/internal/useVoiceInternal.test.ts index 8247e68..1d831e5 100644 --- a/__tests__/hooks/internal/useVoiceInternal.test.ts +++ b/__tests__/hooks/internal/useVoiceInternal.test.ts @@ -54,6 +54,20 @@ describe("useVoiceInternal Hook", () => { // checks if voice state was updated expect(result.current.voiceToggledOn).toBe(!initialVoiceToggledOn); + + // simulates clicking the toggle action + await act(async () => { + await result.current.toggleVoice(); + }); + + // checks if callRcbEvent was called with rcb-toggle-audio and correct arguments + expect(callRcbEventMock).toHaveBeenCalledWith(RcbEvent.TOGGLE_VOICE, { + currState: !initialVoiceToggledOn, + newState: initialVoiceToggledOn, + }); + + // check if voice state was updated + expect(result.current.voiceToggledOn).toBe(initialVoiceToggledOn); }); it("should prevent toggling when event is defaultPrevented", () => { diff --git a/src/hooks/internal/useAudioInternal.ts b/src/hooks/internal/useAudioInternal.ts index f3371c6..980e1a4 100644 --- a/src/hooks/internal/useAudioInternal.ts +++ b/src/hooks/internal/useAudioInternal.ts @@ -31,7 +31,7 @@ export const useAudioInternal = () => { } } setAudioToggledOn(prev => !prev); - }, []); + }, [audioToggledOn]); return { audioToggledOn, diff --git a/src/hooks/internal/useNotificationsInternal.ts b/src/hooks/internal/useNotificationsInternal.ts index 51d0e5e..0da2ac4 100644 --- a/src/hooks/internal/useNotificationsInternal.ts +++ b/src/hooks/internal/useNotificationsInternal.ts @@ -89,7 +89,7 @@ export const useNotificationInternal = () => { } } setNotificationsToggledOn(prev => !prev); - }, []); + }, [notificationsToggledOn]); return { unreadCount, diff --git a/src/hooks/internal/useVoiceInternal.ts b/src/hooks/internal/useVoiceInternal.ts index a51ecba..6896921 100644 --- a/src/hooks/internal/useVoiceInternal.ts +++ b/src/hooks/internal/useVoiceInternal.ts @@ -33,7 +33,7 @@ export const useVoiceInternal = () => { } } setVoiceToggledOn(prev => !prev); - }, []); + }, [voiceToggledOn]); /** * Sync voice with chat input based on whether voice should be kept toggled on.