Skip to content

Commit

Permalink
fix: Fix stale values in toggle events
Browse files Browse the repository at this point in the history
  • Loading branch information
tjtanjin committed Nov 7, 2024
1 parent 643052b commit 9274c60
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 3 deletions.
14 changes: 14 additions & 0 deletions __tests__/hooks/internal/useAudioInternal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
14 changes: 14 additions & 0 deletions __tests__/hooks/internal/useNotificationsInternal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
14 changes: 14 additions & 0 deletions __tests__/hooks/internal/useVoiceInternal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/internal/useAudioInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const useAudioInternal = () => {
}
}
setAudioToggledOn(prev => !prev);
}, []);
}, [audioToggledOn]);

return {
audioToggledOn,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/internal/useNotificationsInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export const useNotificationInternal = () => {
}
}
setNotificationsToggledOn(prev => !prev);
}, []);
}, [notificationsToggledOn]);

return {
unreadCount,
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/internal/useVoiceInternal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 9274c60

Please sign in to comment.