Skip to content

Commit

Permalink
fix: test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ameerul-deriv committed May 1, 2024
1 parent 4360928 commit e11cf73
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const BlockUnblockUserModal = ({

return (
<Modal
ariaHideApp={false}
className='block-unblock-user-modal'
isOpen={isModalOpen}
onRequestClose={onRequestClose}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const mockOnRequestClose = jest.fn();
const mockUseBlockMutate = jest.fn();
const mockUseUnblockMutate = jest.fn();

jest.mock('@deriv/api-v2', () => ({
...jest.requireActual('@deriv/api-v2'),
p2p: {
jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
api: {
counterparty: {
useBlock: jest.fn(() => ({
mutate: mockUseBlockMutate,
Expand Down
1 change: 1 addition & 0 deletions src/components/Modals/DailyLimitModal/DailyLimitModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const DailyLimitModal = ({ currency, isModalOpen, onRequestClose }: TDailyLimitM
return (
// TODO: below modal will be rewritten to use @deriv/ui modal
<Modal
ariaHideApp={false}
className='daily-limit-modal'
isOpen={isModalOpen}
onRequestClose={onRequestClose}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ let mockUseAdvertiserUpdate = {
daily_sell_limit: 200,
},
error: undefined,
isLoading: true,
isPending: true,
isSuccess: false,
mutate: mockUseAdvertiserUpdateMutate,
};

jest.mock('@deriv/api-v2', () => ({
...jest.requireActual('@deriv/api-v2'),
p2p: {
jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
api: {
advertiser: {
useUpdate: jest.fn(() => mockUseAdvertiserUpdate),
},
},
}));

describe('DailyLimitModal', () => {
it('should render loader when data is not ready', () => {
it('should render loader when data is not ready', async () => {
render(<DailyLimitModal currency='USD' isModalOpen onRequestClose={mockOnRequestClose} />);

expect(screen.getByTestId('dt_derivs-loader')).toBeVisible();
});
it('should render the correct title and behaviour', async () => {
mockUseAdvertiserUpdate = {
...mockUseAdvertiserUpdate,
isLoading: false,
isPending: false,
isSuccess: false,
};
render(<DailyLimitModal currency='USD' isModalOpen onRequestClose={mockOnRequestClose} />);
Expand All @@ -55,7 +55,7 @@ describe('DailyLimitModal', () => {
it('should render the successful limits increase', async () => {
mockUseAdvertiserUpdate = {
...mockUseAdvertiserUpdate,
isLoading: false,
isPending: false,
isSuccess: true,
};
render(<DailyLimitModal currency='USD' isModalOpen onRequestClose={mockOnRequestClose} />);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const mockUseGet = {
isLoading: false,
};

jest.mock('@deriv/api-v2', () => ({
p2p: {
jest.mock('@/hooks', () => ({
api: {
advert: {
useGet: jest.fn(() => mockUseGet),
},
Expand Down
4 changes: 2 additions & 2 deletions src/components/Modals/NicknameModal/NicknameModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const NicknameModal = ({ isModalOpen, onRequestClose }: TNicknameModalProps) =>

useEffect(() => {
if (isSuccess) {
onRequestClose;
onRequestClose();
setHasCreatedAdvertiser(true);
} else if (isError) {
debouncedReset();
Expand Down Expand Up @@ -90,7 +90,7 @@ const NicknameModal = ({ isModalOpen, onRequestClose }: TNicknameModalProps) =>
color='black'
onClick={() => {
history.push(BUY_SELL_URL);
onRequestClose;
onRequestClose();
}}
size='lg'
textSize={textSize}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ jest.mock('@deriv-com/ui', () => ({
}),
}));

jest.mock('@deriv/api-v2', () => ({
...jest.requireActual('@deriv/api-v2'),
p2p: {
jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
api: {
advertiser: {
useCreate: jest.fn(() => ({
error: undefined,
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('NicknameModal', () => {
expect(mockedMutate).toHaveBeenCalledWith({
name: 'Nahida',
});
expect(mockUseAdvertiserInfoState().setHasCreatedAdvertiser).toBeCalledWith(true);
expect(mockUseAdvertiserInfoState().setHasCreatedAdvertiser).toHaveBeenCalledWith(true);
});
it('should invoke reset when there is an error from creating advertiser', async () => {
(mockedUseAdvertiserCreate as jest.Mock).mockImplementationOnce(() => ({
Expand All @@ -89,11 +89,9 @@ describe('NicknameModal', () => {
reset: mockedReset,
}));

await (() => {
render(<NicknameModal {...mockProps} />);
});
render(<NicknameModal {...mockProps} />);

expect(mockedReset).toBeCalled();
expect(mockedReset).toHaveBeenCalled();
});
it('should close the modal when Cancel button is clicked', async () => {
(mockedUseAdvertiserCreate as jest.Mock).mockImplementationOnce(() => ({
Expand All @@ -111,7 +109,7 @@ describe('NicknameModal', () => {
});
await userEvent.click(cancelBtn);

expect(mockPush).toBeCalledWith('/buy-sell');
expect(mockProps.onRequestClose).toBeCalledWith(false);
expect(mockPush).toHaveBeenCalledWith('/buy-sell');
expect(mockProps.onRequestClose).toHaveBeenCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const mockUseDispute = {
mutate: jest.fn(),
};

jest.mock('@deriv/api-v2', () => ({
...jest.requireActual('@deriv/api-v2'),
p2p: {
jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
api: {
orderDispute: {
useDispute: () => mockUseDispute,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import html2canvas from 'html2canvas';
import { useDevice } from '@deriv-com/ui';
import { act, render, screen, waitFor } from '@testing-library/react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import ShareAdsModal from '../ShareAdsModal';

Expand All @@ -24,8 +24,8 @@ const mockUseGet = {
isLoading: false,
};

jest.mock('@deriv/api-v2', () => ({
p2p: {
jest.mock('@/hooks', () => ({
api: {
advert: {
useGet: jest.fn(() => mockUseGet),
},
Expand All @@ -50,8 +50,8 @@ jest.mock('@deriv-com/ui', () => ({
}));

const mockCopyFn = jest.fn();
jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
jest.mock('@/hooks/custom-hooks', () => ({
...jest.requireActual('@/hooks/custom-hooks'),
useCopyToClipboard: jest.fn(() => [true, mockCopyFn, jest.fn()]),
}));

Expand All @@ -77,21 +77,26 @@ describe('ShareAdsModal', () => {
});

it('should call onCopy function when clicking on copy icon', async () => {
jest.useFakeTimers();
mockUseDevice.mockReturnValue({
isDesktop: false,
isMobile: true,
});

render(<ShareAdsModal {...mockProps} />);
screen.debug(undefined, 1000000);

Check warning on line 86 in src/components/Modals/ShareAdsModal/__tests__/ShareAdsModal.spec.tsx

View workflow job for this annotation

GitHub Actions / build_to_cloudflare_pages

Unexpected debug statement
const copyButton = screen.getByRole('button', { name: 'Copy link' });
await userEvent.click(copyButton);
await act(async () => {
jest.runAllTimers();
await Promise.resolve();
});

expect(mockCopyFn).toHaveBeenCalledWith(
`${window.location.href}advertiser/${mockUseGet.data.advertiser_details.id}?advert_id=${mockProps.id}`
);
});
it('should call html2canvas function when clicking on Download this QR code button', async () => {
mockUseDevice.mockReturnValue({
isDesktop: false,
isMobile: true,
});

render(<ShareAdsModal {...mockProps} />);

const downloadButton = screen.getByRole('button', { name: 'Download this QR code' });
Expand Down

0 comments on commit e11cf73

Please sign in to comment.