Repository navigation
fix: test button not playing default sound in notification preferences #40335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
8dcf318
fix test button not playing default sound
nazabucciarelli fe52294
change audio._id assignment add one more test case
nazabucciarelli 4a6ece2
move solution downstream to NotificationPreferencesWithData file
nazabucciarelli dd9f430
add unit test
nazabucciarelli e7bc5a1
use aria-label instead of data-testid
nazabucciarelli File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fixes test button not playing default sound in Notifications Preferences |
116 changes: 116 additions & 0 deletions
116
apps/meteor/client/providers/CustomSoundProvider/CustomSoundProvider.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| import { mockAppRoot } from '@rocket.chat/mock-providers'; | ||
| import { useCustomSound } from '@rocket.chat/ui-contexts'; | ||
| import { renderHook, act } from '@testing-library/react'; | ||
| import React from 'react'; | ||
|
|
||
| import CustomSoundProvider from './CustomSoundProvider'; | ||
|
|
||
| jest.mock('./lib', () => ({ | ||
| defaultSounds: [ | ||
| { _id: 'chime', name: 'Sound_Chime', extension: 'mp3', src: 'sounds/chime.mp3' }, | ||
| { _id: 'door', name: 'Sound_Door', extension: 'mp3', src: 'sounds/door.mp3' }, | ||
| { _id: 'ringtone', name: 'Sound_Ringtone', extension: 'mp3', src: 'sounds/ringtone.mp3' }, | ||
| ], | ||
| getCustomSoundURL: (sound: { _id: string; extension: string }) => `sounds/${sound._id}.${sound.extension}`, | ||
| })); | ||
|
|
||
| jest.mock('../../../app/utils/client/lib/SDKClient', () => ({ | ||
| sdk: { | ||
| call: jest.fn().mockResolvedValue([]), | ||
| }, | ||
| })); | ||
|
|
||
| jest.mock('@rocket.chat/ui-contexts', () => ({ | ||
| ...jest.requireActual('@rocket.chat/ui-contexts'), | ||
| useStream: () => () => () => undefined, | ||
| })); | ||
|
|
||
| jest.mock('../../hooks/useUserSoundPreferences', () => ({ | ||
| useUserSoundPreferences: () => ({ | ||
| notificationsSoundVolume: 100, | ||
| voipRingerVolume: 100, | ||
| }), | ||
| })); | ||
|
|
||
| const mockPlay = jest.fn().mockResolvedValue(undefined); | ||
| const mockLoad = jest.fn(); | ||
| const audioInstances: { src: string }[] = []; | ||
|
|
||
| class MockAudio { | ||
| src: string; | ||
|
|
||
| id = ''; | ||
|
|
||
| volume = 1; | ||
|
|
||
| loop = false; | ||
|
|
||
| constructor(src: string) { | ||
| this.src = src; | ||
| audioInstances.push(this); | ||
| } | ||
|
|
||
| play = mockPlay; | ||
|
|
||
| load = mockLoad; | ||
| } | ||
|
|
||
| const buildWrapper = (appRootBuilder: ReturnType<typeof mockAppRoot>) => { | ||
| return appRootBuilder.wrap((children) => React.createElement(CustomSoundProvider, null, children)).build(); | ||
| }; | ||
|
|
||
| beforeAll(() => { | ||
| Object.defineProperty(global, 'Audio', { value: MockAudio, writable: true }); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| audioInstances.length = 0; | ||
| }); | ||
|
|
||
| describe('CustomSoundProvider - play()', () => { | ||
| it('should play the default newMessageNotification sound (ringtone) when soundId is "default"', async () => { | ||
| const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined); | ||
| const wrapper = buildWrapper(mockAppRoot().withUserPreference('newMessageNotification', 'ringtone')); | ||
|
|
||
| const { result } = renderHook(() => useCustomSound(), { wrapper }); | ||
|
|
||
| await act(async () => { | ||
| result.current.play('default'); | ||
| }); | ||
|
|
||
| expect(mockPlay).toHaveBeenCalledTimes(1); | ||
| expect(audioInstances[0].src).toContain('ringtone'); | ||
| expect(consoleSpy).not.toHaveBeenCalled(); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
|
|
||
| it('should fall back to "chime" when newMessageNotification preference is not set and soundId is "default"', async () => { | ||
| const wrapper = buildWrapper(mockAppRoot()); | ||
|
|
||
| const { result } = renderHook(() => useCustomSound(), { wrapper }); | ||
|
|
||
| await act(async () => { | ||
| result.current.play('default'); | ||
| }); | ||
|
|
||
| expect(mockPlay).toHaveBeenCalledTimes(1); | ||
| expect(audioInstances[0].src).toContain('chime'); | ||
| }); | ||
|
|
||
| it('should play the sound directly when a valid soundId is provided', async () => { | ||
| const consoleSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined); | ||
| const wrapper = buildWrapper(mockAppRoot().withUserPreference('newMessageNotification', 'chime')); | ||
|
|
||
| const { result } = renderHook(() => useCustomSound(), { wrapper }); | ||
|
|
||
| await act(async () => { | ||
| result.current.play('door'); | ||
| }); | ||
|
|
||
| expect(mockPlay).toHaveBeenCalledTimes(1); | ||
| expect(audioInstances[0].src).toContain('door'); | ||
| expect(consoleSpy).not.toHaveBeenCalled(); | ||
| consoleSpy.mockRestore(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.