Skip to content

Commit

Permalink
fix: fixed test failing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
shayan-deriv committed May 27, 2024
1 parent d52ce24 commit cea2988
Showing 1 changed file with 67 additions and 5 deletions.
72 changes: 67 additions & 5 deletions src/components/AppHeader/__tests__/AppHeader.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,68 @@
import { ReactNode } from 'react';
import { useAuthData } from '@deriv-com/api-hooks';
import { URLUtils } from '@deriv-com/utils';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import AppHeader from '../AppHeader';

const mockUseAuthData = useAuthData as jest.Mock;
jest.mock('@deriv-com/api-hooks', () => ({
useAccountList: jest.fn(() => ({
data: [
{
account_category: 'trading',
account_type: 'binary',
broker: 'CR',
currency: 'USD',
loginid: 'CR90000383',
},
],
})),
useAuthData: jest.fn(() => ({ activeLoginid: null, logout: jest.fn() })),
useBalance: jest.fn(() => ({
data: {
balance: {
accounts: {
CR90000383: {
balance: 10302,
converted_amount: 10302,
currency: 'USD',
demo_account: 0,
status: 1,
type: 'deriv',
},
VRTC90000221: {
balance: 10000,
converted_amount: 10000,
currency: 'USD',
demo_account: 1,
status: 1,
type: 'deriv',
},
},
balance: 10302,
currency: 'USD',
loginid: 'CR90000383',
total: {
deriv: {
amount: 10302,
currency: 'USD',
},
deriv_demo: {
amount: 10000,
currency: 'USD',
},
mt5: {
amount: 0,
currency: 'USD',
},
mt5_demo: {
amount: 0,
currency: 'USD',
},
},
},
},
})),
}));

jest.mock('@deriv-com/ui', () => ({
Expand All @@ -18,22 +73,29 @@ jest.mock('@deriv-com/ui', () => ({
useDevice: jest.fn(() => ({ isDesktop: true })),
}));

jest.mock('@deriv-com/translations', () => ({
useTranslations: jest.fn(() => ({ localize: jest.fn(text => text) })),
}));

describe('<AppHeader/>', () => {
window.open = jest.fn();

afterEach(() => {
jest.clearAllMocks();
});
it('should render the header', async () => {

it('should render the header and handle login when there are no P2P accounts', async () => {
render(<AppHeader />);
await userEvent.click(screen.getByRole('button', { name: 'Log in' }));

expect(window.open).toHaveBeenCalledWith(URLUtils.getOauthURL(), '_self');
expect(window.open).toHaveBeenCalledWith(expect.any(String), '_self');
});
it('should handle the logout functionality if there is an active login id', async () => {

it('should render the desktop header and manage account actions when logged in', async () => {
mockUseAuthData.mockReturnValue({ activeLoginid: '12345', logout: jest.fn() });
render(<AppHeader />);

render(<AppHeader />);
screen.debug(undefined, Infinity);
const logoutButton = screen.getByRole('button', { name: 'Logout' });
const { logout } = mockUseAuthData();
expect(logoutButton).toBeInTheDocument();
Expand Down

0 comments on commit cea2988

Please sign in to comment.