-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:deriv-com/p2p
- Loading branch information
Showing
3 changed files
with
132 additions
and
4 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/components/AppHeader/MobileMenu/__tests__/MobileMenu.spec.tsx
This file contains 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,74 @@ | ||
import { BrowserRouter } from 'react-router-dom'; | ||
import { QueryParamProvider } from 'use-query-params'; | ||
import { ReactRouter5Adapter } from 'use-query-params/adapters/react-router-5'; | ||
import { useModalManager } from '@/hooks'; | ||
import { useDevice } from '@deriv-com/ui'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import MobileMenu from '../MobileMenu'; | ||
|
||
jest.mock('@/hooks', () => ({ | ||
useModalManager: jest.fn().mockReturnValue({ | ||
hideModal: jest.fn(), | ||
isModalOpenFor: jest.fn().mockReturnValue(false), | ||
showModal: jest.fn(), | ||
}), | ||
useNetworkStatus: jest.fn().mockReturnValue('online'), | ||
useSyncedTime: jest.fn(), | ||
})); | ||
|
||
jest.mock('@deriv-com/ui', () => ({ | ||
...jest.requireActual('@deriv-com/ui'), | ||
useDevice: jest.fn(), | ||
})); | ||
|
||
jest.mock('@deriv-com/api-hooks', () => ({ | ||
useAuthData: jest.fn().mockReturnValue({ | ||
isAuthorized: true, | ||
}), | ||
})); | ||
|
||
jest.mock('@deriv-com/translations', () => ({ | ||
useTranslations: jest.fn().mockReturnValue({ | ||
currentLang: 'EN', | ||
localize: jest.fn(text => text), | ||
}), | ||
})); | ||
|
||
const MobileMenuComponent = () => ( | ||
<BrowserRouter> | ||
<QueryParamProvider adapter={ReactRouter5Adapter}> | ||
<MobileMenu /> | ||
</QueryParamProvider> | ||
</BrowserRouter> | ||
); | ||
|
||
describe('MobileMenu component', () => { | ||
it('should not render when isDesktop is true', () => { | ||
(useDevice as jest.Mock).mockReturnValue({ isDesktop: true }); | ||
render(<MobileMenuComponent />); | ||
expect(screen.queryByText('Menu')).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should render toggle button and handle click', async () => { | ||
(useDevice as jest.Mock).mockReturnValue({ isDesktop: false }); | ||
render(<MobileMenuComponent />); | ||
expect(screen.queryByText('Menu')).not.toBeInTheDocument(); | ||
await userEvent.click(screen.getByRole('button')); | ||
expect(screen.getByText('Menu')).toBeInTheDocument(); | ||
}); | ||
|
||
it('should open the language settings', async () => { | ||
(useDevice as jest.Mock).mockReturnValue({ isDesktop: false }); | ||
const { isModalOpenFor, showModal } = useModalManager(); | ||
|
||
render(<MobileMenuComponent />); | ||
await userEvent.click(screen.getByRole('button')); | ||
expect(screen.getByText('EN')).toBeInTheDocument(); | ||
|
||
await userEvent.click(screen.getByText('EN')); | ||
|
||
expect(showModal).toHaveBeenCalledWith('MobileLanguagesDrawer'); | ||
expect(isModalOpenFor).toHaveBeenCalledWith('MobileLanguagesDrawer'); | ||
}); | ||
}); |
This file contains 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,48 @@ | ||
import { act, renderHook } from '@testing-library/react'; | ||
import { useFullScreen } from '../custom-hooks'; | ||
|
||
describe('useFullScreen', () => { | ||
afterEach(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
it('should add and remove fullscreen event listeners', () => { | ||
const addEventListenerSpy = jest.spyOn(document, 'addEventListener'); | ||
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener'); | ||
|
||
const { unmount } = renderHook(() => useFullScreen()); | ||
|
||
expect(addEventListenerSpy).toHaveBeenCalledWith('fullscreenchange', expect.any(Function), false); | ||
expect(addEventListenerSpy).toHaveBeenCalledWith('webkitfullscreenchange', expect.any(Function), false); | ||
expect(addEventListenerSpy).toHaveBeenCalledWith('mozfullscreenchange', expect.any(Function), false); | ||
expect(addEventListenerSpy).toHaveBeenCalledWith('MSFullscreenChange', expect.any(Function), false); | ||
|
||
unmount(); | ||
|
||
expect(removeEventListenerSpy).toHaveBeenCalledWith('fullscreenchange', expect.any(Function), false); | ||
expect(removeEventListenerSpy).toHaveBeenCalledWith('webkitfullscreenchange', expect.any(Function), false); | ||
expect(removeEventListenerSpy).toHaveBeenCalledWith('mozfullscreenchange', expect.any(Function), false); | ||
expect(removeEventListenerSpy).toHaveBeenCalledWith('MSFullscreenChange', expect.any(Function), false); | ||
}); | ||
|
||
it('should call requestFullscreen when trying to enter fullscreen', () => { | ||
const requestFullscreenMock = jest.fn(); | ||
document.documentElement.requestFullscreen = requestFullscreenMock; | ||
const { result } = renderHook(() => useFullScreen()); | ||
|
||
act(() => { | ||
result.current.toggleFullScreenMode(); | ||
}); | ||
|
||
expect(requestFullscreenMock).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should clean up event listeners on unmount', () => { | ||
const removeEventListenerSpy = jest.spyOn(document, 'removeEventListener'); | ||
const { unmount } = renderHook(() => useFullScreen()); | ||
|
||
unmount(); | ||
|
||
expect(removeEventListenerSpy).toHaveBeenCalledTimes(4); | ||
}); | ||
}); |
This file contains 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