Skip to content
This repository has been archived by the owner on May 13, 2024. It is now read-only.

Commit

Permalink
test: finalize all tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shafin-deriv committed Mar 22, 2024
1 parent 5c15552 commit 69d3707
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from 'react';
import { cleanup, render, screen } from '@site/src/test-utils';
import AppDashboardContainer from '..';

describe('AppDashboardContainer', () => {
afterEach(() => {
cleanup();
jest.clearAllMocks();
});

it('Should render the page heading', () => {
render(<AppDashboardContainer />);

const label = screen.getByText(/App dashboard/i);
expect(label).toBeInTheDocument();
});

it('Should render children component in the screen', () => {
render(
<AppDashboardContainer>
<div>Test Component</div>
</AppDashboardContainer>,
);
const label = screen.getByText(/Test Component/i);
expect(label).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import React from 'react';
import { cleanup, render, screen } from '@site/src/test-utils';
import AppRegister from '..';

const mock_submit = jest.fn();

describe('AppRegister', () => {
afterEach(() => {
cleanup();
jest.clearAllMocks();
});

it('Should render the register form with register button', () => {
render(<AppRegister submit={mock_submit} />);

const button = screen.getByText(/Register now/i);
expect(button).toBeInTheDocument();
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import React from 'react';
import { cleanup, render, screen } from '@site/src/test-utils';
import { AppRegisterSuccessModal } from '..';
import useAppManager from '@site/src/hooks/useAppManager';

const mock_cancel = jest.fn();
const mock_configure = jest.fn();

jest.mock('@site/src/hooks/useAppManager');
const mockUseAppManager = useAppManager as jest.MockedFunction<
() => Partial<ReturnType<typeof useAppManager>>
>;
mockUseAppManager.mockImplementation(() => ({
app_register_modal_open: true,
}));

describe('AppRegisterSuccessModal', () => {
afterEach(() => {
cleanup();
jest.clearAllMocks();
});

it('Should render the success modal in desktop', () => {
render(
<AppRegisterSuccessModal
is_desktop={true}
onCancel={mock_cancel}
onConfigure={mock_configure}
/>,
);

const label = screen.getByText(/Application registered successfully!/i);
expect(label).toBeInTheDocument();
const imgElement = screen.getByAltText('check icon');
expect(imgElement).toBeInTheDocument();
});

it('Should render the success modal in mobile', () => {
render(
<AppRegisterSuccessModal
is_desktop={false}
onCancel={mock_cancel}
onConfigure={mock_configure}
/>,
);

const label = screen.getByText(/Application registered successfully!/i);
expect(label).toBeInTheDocument();
const imgElement = screen.queryByAltText('check icon');
expect(imgElement).not.toBeInTheDocument();
});

it('Should handle click events properly', () => {
render(
<AppRegisterSuccessModal
is_desktop={false}
onCancel={mock_cancel}
onConfigure={mock_configure}
/>,
);
const configure_btn = screen.getByText(/Configure now/i);
const maybe_later_btn = screen.getByText(/Maybe later/i);
configure_btn.click();
expect(mock_configure).toBeCalled();
maybe_later_btn.click();
expect(mock_cancel).toBeCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ export const AppRegisterSuccessModal = ({
action_sheet_open={app_register_modal_open}
primary_action={{
label: 'Configure now',
onClick: onCancel,
onClick: onConfigure,
}}
secondary_action={{
label: 'Maybe later',
onClick: onConfigure,
onClick: onCancel,
}}
is_desktop={is_desktop}
disable_drag
Expand All @@ -36,7 +36,7 @@ export const AppRegisterSuccessModal = ({
<div>
{is_desktop && (
<div className='app_register_success_modal__icon'>
<img src='/img/circle_check_regular_icon.svg' />
<img src='/img/circle_check_regular_icon.svg' alt='check icon' />
</div>
)}
<Heading.H3 className='app_register_success_modal__header'>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import React from 'react';
import { cleanup, render, screen } from '@site/src/test-utils';
import MemoizedManageDashboard from '..';
import useAppManager from '@site/src/hooks/useAppManager';
import useDeviceType from '@site/src/hooks/useDeviceType';
import userEvent from '@testing-library/user-event';
import apiManager from '@site/src/configs/websocket';

jest.mock('@site/src/hooks/useAppManager');
const mockUseAppManager = useAppManager as jest.MockedFunction<
() => Partial<ReturnType<typeof useAppManager>>
>;
mockUseAppManager.mockImplementation(() => ({
getApps: jest.fn(),
apps: undefined,
tokens: undefined,
}));

jest.mock('@site/src/hooks/useDeviceType');
const mockDeviceType = useDeviceType as jest.MockedFunction<
() => Partial<ReturnType<typeof useDeviceType>>
>;
mockDeviceType.mockImplementation(() => ({
deviceType: 'desktop',
}));

jest.mock('@site/src/configs/websocket');
const mockApiManager = apiManager as jest.Mocked<typeof apiManager>;

describe('ManageDashboard', () => {
afterEach(() => {
cleanup();
jest.clearAllMocks();
});

it('Should render the initial compoent with loader', () => {
const { container } = render(<MemoizedManageDashboard />);
expect(container).toBeInTheDocument();
const loader = screen.getByTestId('dt_spinner');
expect(loader).toBeInTheDocument();
});

it('Should render the content App Register page in mobile device - if no token or app is available', () => {
mockUseAppManager.mockImplementation(() => ({
apps: [],
tokens: [],
getApps: jest.fn(),
}));
mockDeviceType.mockImplementation(() => ({
deviceType: 'mobile',
}));
render(<MemoizedManageDashboard />);
const register_button = screen.getByText(/Register now/i);
expect(register_button).toBeInTheDocument();
});

it('Should call getApps on submit button press if all the fields are filled up', async () => {
const mockGetApps = jest.fn();
mockUseAppManager.mockImplementation(() => ({
apps: [],
tokens: [],
getApps: mockGetApps,
}));
render(<MemoizedManageDashboard />);

const name_input = screen.getByRole('textbox');
await userEvent.type(name_input, 'test create token');
const tnc_input = screen.getByRole('checkbox');
await userEvent.click(tnc_input);
const register_button = screen.getByText(/Register now/i);
await userEvent.click(register_button);

expect(mockGetApps).toHaveBeenCalled();
});

it('Should trigger the success modal in desktop', async () => {
const mockModalOpenSetter = jest.fn();
mockApiManager.augmentedSend.mockResolvedValue({
app_register: {
active: 1,
app_id: 1234,
app_markup_percentage: 0,
appstore: '',
github: '',
googleplay: '',
homepage: '',
name: 'TestApp1',
redirect_uri: '',
scopes: [],
verification_uri: '',
},
echo_req: {
app_markup_percentage: 0,
app_register: 1,
name: 'TestApp1',
req_id: 4,
scopes: [],
},
msg_type: 'app_register',
req_id: 4,
});

mockUseAppManager.mockImplementation(() => ({
getApps: jest.fn(),
apps: [],
tokens: [],
setAppRegisterModalOpen: mockModalOpenSetter,
}));

render(<MemoizedManageDashboard />);

const name_input = screen.getByRole('textbox');
await userEvent.type(name_input, 'test create token');
const tnc_input = screen.getByRole('checkbox');
await userEvent.click(tnc_input);
const register_button = screen.getByText(/Register now/i);
await userEvent.click(register_button);

expect(mockModalOpenSetter).toBeCalledWith(true);
});

it('Should close the modal on config button click', async () => {
const mockModalOpenSetter = jest.fn();
mockUseAppManager.mockImplementation(() => ({
getApps: jest.fn(),
apps: [],
tokens: [],
setAppRegisterModalOpen: mockModalOpenSetter,
app_register_modal_open: true,
}));

render(<MemoizedManageDashboard />);

const config_button = screen.getByText(/Config/i);
await userEvent.click(config_button);

expect(mockModalOpenSetter).toBeCalledWith(false);
});

it('Should close the modal on cancel button click', async () => {
const mockModalOpenSetter = jest.fn();
mockUseAppManager.mockImplementation(() => ({
getApps: jest.fn(),
apps: [],
tokens: [],
setAppRegisterModalOpen: mockModalOpenSetter,
app_register_modal_open: true,
}));

render(<MemoizedManageDashboard />);

const cancel_button = screen.getByText(/Maybe Later/i);
await userEvent.click(cancel_button);

expect(mockModalOpenSetter).toBeCalledWith(false);
});
});

0 comments on commit 69d3707

Please sign in to comment.