Skip to content

Commit 988fd37

Browse files
fix: test button not playing default sound in notification preferences (#40335)
1 parent 451df64 commit 988fd37

4 files changed

Lines changed: 92 additions & 3 deletions

File tree

.changeset/major-coats-smash.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@rocket.chat/meteor': patch
3+
---
4+
5+
Fixes test button not playing default sound in Notifications Preferences

apps/meteor/client/views/room/contextualBar/NotificationPreferences/NotificationPreferencesForm.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const NotificationPreferencesForm = ({ notificationOptions, handlePlaySound }: N
8484
optionValue={value}
8585
onChange={onChange}
8686
>
87-
<IconButton icon='play' mis={4} onClick={handlePlaySound} />
87+
<IconButton icon='play' mis={4} onClick={handlePlaySound} aria-label={t('Play')} />
8888
</NotificationPreference>
8989
)}
9090
/>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { mockAppRoot } from '@rocket.chat/mock-providers';
2+
import { render, screen } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
5+
import NotificationPreferencesWithData from './NotificationPreferencesWithData';
6+
7+
const mockPlay = jest.fn();
8+
const mockCloseTab = jest.fn();
9+
const mockUseRoomSubscription = jest.fn();
10+
11+
jest.mock('@rocket.chat/ui-contexts', () => ({
12+
...jest.requireActual('@rocket.chat/ui-contexts'),
13+
useCustomSound: () => ({ play: mockPlay, list: [] }),
14+
useRoomToolbox: () => ({ closeTab: mockCloseTab }),
15+
useToastMessageDispatch: () => jest.fn(),
16+
}));
17+
18+
jest.mock('../../contexts/RoomContext', () => ({
19+
useRoom: () => ({ _id: 'GENERAL' }),
20+
useRoomSubscription: () => mockUseRoomSubscription(),
21+
}));
22+
23+
jest.mock('../../../../hooks/useEndpointMutation', () => ({
24+
useEndpointMutation: () => ({ mutateAsync: jest.fn() }),
25+
}));
26+
27+
const appRoot = (userPreferences?: Record<string, unknown>) =>
28+
mockAppRoot()
29+
.withUserPreference('newMessageNotification', userPreferences?.newMessageNotification ?? 'chime')
30+
.build();
31+
32+
beforeEach(() => {
33+
mockPlay.mockClear();
34+
mockUseRoomSubscription.mockReturnValue({
35+
disableNotifications: false,
36+
muteGroupMentions: false,
37+
hideUnreadStatus: false,
38+
hideMentionStatus: false,
39+
audioNotificationValue: undefined, // desktopSound defaults to 'default'
40+
});
41+
});
42+
43+
describe('NotificationPreferencesWithData - handlePlaySound', () => {
44+
it('plays the user newMessageNotification preference when desktopSound is "default"', async () => {
45+
render(<NotificationPreferencesWithData />, {
46+
wrapper: appRoot({ newMessageNotification: 'chime' }),
47+
});
48+
49+
await userEvent.click(screen.getByRole('button', { name: 'Play' }));
50+
51+
expect(mockPlay).toHaveBeenCalledWith('chime');
52+
expect(mockPlay).not.toHaveBeenCalledWith('default');
53+
});
54+
55+
it('plays the user preference sound even when it is not the default chime', async () => {
56+
render(<NotificationPreferencesWithData />, {
57+
wrapper: appRoot({ newMessageNotification: 'ringtone' }),
58+
});
59+
60+
await userEvent.click(screen.getByRole('button', { name: 'Play' }));
61+
62+
expect(mockPlay).toHaveBeenCalledWith('ringtone');
63+
});
64+
65+
it('plays the specific sound directly when desktopSound is not "default"', async () => {
66+
mockUseRoomSubscription.mockReturnValue({
67+
disableNotifications: false,
68+
muteGroupMentions: false,
69+
hideUnreadStatus: false,
70+
hideMentionStatus: false,
71+
audioNotificationValue: 'door',
72+
});
73+
74+
render(<NotificationPreferencesWithData />, {
75+
wrapper: appRoot({ newMessageNotification: 'chime' }),
76+
});
77+
78+
await userEvent.click(screen.getByRole('button', { name: 'Play' }));
79+
80+
expect(mockPlay).toHaveBeenCalledWith('door');
81+
expect(mockPlay).not.toHaveBeenCalledWith('chime');
82+
});
83+
});

apps/meteor/client/views/room/contextualBar/NotificationPreferences/NotificationPreferencesWithData.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { SelectOption } from '@rocket.chat/fuselage';
2-
import { useCustomSound, useToastMessageDispatch, useRoomToolbox } from '@rocket.chat/ui-contexts';
2+
import { useCustomSound, useToastMessageDispatch, useRoomToolbox, useUserPreference } from '@rocket.chat/ui-contexts';
33
import type { ReactElement } from 'react';
44
import { memo } from 'react';
55
import { useForm, FormProvider } from 'react-hook-form';
@@ -16,6 +16,7 @@ const NotificationPreferencesWithData = (): ReactElement => {
1616
const { closeTab } = useRoomToolbox();
1717
const customSound = useCustomSound();
1818
const dispatchToastMessage = useToastMessageDispatch();
19+
const newMessageNotificationPreference = useUserPreference<string>('newMessageNotification', 'chime') as string;
1920

2021
const { mutateAsync: saveSettings } = useEndpointMutation('POST', '/v1/rooms.saveNotification', {
2122
onSuccess: () => {
@@ -58,7 +59,7 @@ const NotificationPreferencesWithData = (): ReactElement => {
5859
const { desktopSound } = methods.watch();
5960

6061
const handlePlaySound = (): void => {
61-
customSound.play(desktopSound);
62+
customSound.play(desktopSound === 'default' ? newMessageNotificationPreference : desktopSound);
6263
};
6364

6465
const handleSave = methods.handleSubmit(

0 commit comments

Comments
 (0)