Skip to content

Commit c807544

Browse files
add unit test
1 parent 4a6ece2 commit c807544

2 files changed

Lines changed: 84 additions & 1 deletion

File tree

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} data-testid='play-sound-button' />
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+
// 2. Conectamos la exportación al mock dinámico
21+
useRoomSubscription: () => mockUseRoomSubscription(),
22+
}));
23+
24+
jest.mock('../../../../hooks/useEndpointMutation', () => ({
25+
useEndpointMutation: () => ({ mutateAsync: jest.fn() }),
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.getByTestId('play-sound-button'));
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.getByTestId('play-sound-button'));
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.getByTestId('play-sound-button'));
79+
80+
expect(mockPlay).toHaveBeenCalledWith('door');
81+
expect(mockPlay).not.toHaveBeenCalledWith('chime');
82+
});
83+
});

0 commit comments

Comments
 (0)