Skip to content
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

Fix test cases + issues with svg?react not being recognised in test cases #41

Merged
merged 4 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion __mocks__/svgReactTransformer.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module.exports = {
process() {
ameerul-deriv marked this conversation as resolved.
Show resolved Hide resolved
return `module.exports = ${JSON.stringify(React.Component)}`;
},
};
};
19 changes: 9 additions & 10 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
module.exports = {
testEnvironment: 'jsdom',
transform: {
'^.+\\.tsx?$': 'ts-jest',
'^.+\\.(js|jsx)$': 'babel-jest',
'^.+\\.svg$': '<rootDir>/__mocks__/svgReactTransformer.cjs',
},
moduleNameMapper: {
'\\.(css|less|sass|scss)$': 'identity-obj-proxy',
'@deriv-com/(.*)': '<rootDir>/node_modules/@deriv-com/$1',
'^@/assets/(.*)\\.svg\\?react$': '<rootDir>/__mocks__/svgReactTransformer.cjs',
'^@/assets/(.*)$': '<rootDir>/src/assets/$1',
'\\.(css|less|sass|scss)$': 'identity-obj-proxy',
'^@/(.*)$': '<rootDir>/src/$1',
'^@/assets/(.*)$': '<rootDir>/src/assets/$1',
},
transformIgnorePatterns: ['/node_modules/(?!(@deriv-com/ui|@sendbird/chat)).+\\.js$'],
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
testEnvironment: 'jsdom',
transform: {
'^.+\\.(js|jsx)$': 'babel-jest',
'^.+\\.svg$': 'jest-transform-stub',
'^.+\\.tsx?$': 'ts-jest',
},
transformIgnorePatterns: ['/node_modules/(?!(@deriv-com/ui|@sendbird/chat)).+\\.js$'],
};
434 changes: 413 additions & 21 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"@deriv/quill-design": "^1.2.24",
"@deriv/quill-icons": "^1.18.10",
"@sendbird/chat": "^4.11.3",
"@svgr/rollup": "^8.1.0",
"@tanstack/react-query": "^5.28.14",
"@tanstack/react-table": "^8.15.0",
"@vitejs/plugin-react": "^4.2.1",
Expand All @@ -30,6 +31,7 @@
"downshift": "^9.0.0",
"history": "^4.10.1",
"html2canvas": "^1.4.1",
"jest-transform-stub": "^2.0.0",
"lodash": "^4.17.21",
"moment": "^2.30.1",
"qrcode.react": "^3.1.0",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,6 @@ const mockProps = {
visibilityStatus: [],
};

jest.mock('@deriv/api-v2', () => ({
useAuthorize: () => ({
data: {
local_currencies: ['USD'],
},
}),
}));

jest.mock('@deriv-com/ui', () => ({
...jest.requireActual('@deriv-com/ui'),
useDevice: () => ({ isMobile: false }),
Expand Down
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
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,12 +40,12 @@

useEffect(() => {
if (isSuccess) {
onRequestClose;
onRequestClose();
setHasCreatedAdvertiser(true);
} else if (isError) {
debouncedReset();
}
}, [isError, isSuccess, setHasCreatedAdvertiser]);

Check warning on line 48 in src/components/Modals/NicknameModal/NicknameModal.tsx

View workflow job for this annotation

GitHub Actions / build_to_cloudflare_pages

React Hook useEffect has missing dependencies: 'debouncedReset' and 'onRequestClose'. Either include them or remove the dependency array. If 'onRequestClose' changes too often, find the parent component that defines it and wrap that definition in useCallback

return (
<Modal ariaHideApp={false} className='nickname-modal' isOpen={!!isModalOpen}>
Expand Down Expand Up @@ -90,7 +90,7 @@
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 @@
isLoading: false,
};

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

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 @@
});

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
ameerul-deriv marked this conversation as resolved.
Show resolved Hide resolved
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { THooks } from 'types';
import { FlyoutMenu } from '@/components';
import { LabelPairedEllipsisVerticalXlRegularIcon } from '@deriv/quill-icons';
import { Button, Checkbox } from '@deriv-com/ui';
import IcCashierBankTransfer from '../../../public/ic-cashier-bank-transfer.svg?react';
import IcCashierEwallet from '../../../public/ic-cashier-ewallet.svg?react';
import IcCashierOther from '../../../public/ic-cashier-other.svg?react';
import { ReactComponent as IcCashierBankTransfer } from '../../../public/ic-cashier-bank-transfer.svg';
import { ReactComponent as IcCashierEwallet } from '../../../public/ic-cashier-ewallet.svg';
import { ReactComponent as IcCashierOther } from '../../../public/ic-cashier-other.svg';
import './PaymentMethodCardHeader.scss';

type TPaymentMethodCardHeaderProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { render, screen } from '@testing-library/react';
import { PaymentMethodCardHeader } from '../PaymentMethodCardHeader';

jest.mock('../../../public/ic-cashier-ewallet.svg?react', () => 'span');

describe('PaymentMethodCardHeader', () => {
it('should render the component correctly', () => {
render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import clsx from 'clsx';
import { THooks } from 'types';
import { TGenericSizes } from '@/utils';
import { Text } from '@deriv-com/ui';
import IcCashierBankTransfer from '../../public/ic-cashier-bank-transfer.svg?react';
import IcCashierEwallet from '../../public/ic-cashier-ewallet.svg?react';
import IcCashierOther from '../../public/ic-cashier-other.svg?react';
import { ReactComponent as IcCashierBankTransfer } from '../../public/ic-cashier-bank-transfer.svg';
import { ReactComponent as IcCashierEwallet } from '../../public/ic-cashier-ewallet.svg';
import { ReactComponent as IcCashierOther } from '../../public/ic-cashier-other.svg';

type TPaymentMethodWithIconProps = {
className?: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { MouseEventHandler } from 'react';
import { Button } from '@deriv-com/ui';
import AttachmentIcon from '../../../../public/ic-attachment.svg?react';
import SendMessageIcon from '../../../../public/ic-send-message.svg?react';
import { ReactComponent as AttachmentIcon } from '../../../../public/ic-attachment.svg';
import { ReactComponent as SendMessageIcon } from '../../../../public/ic-send-message.svg';

type TChatFooterIconProps = {
length: number;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CHAT_FILE_TYPE, CHAT_MESSAGE_TYPE } from '@/constants';
import { useSendbird } from '@/hooks/custom-hooks';
import { convertToMB, formatMilliseconds } from '@/utils';
import { Text, useDevice } from '@deriv-com/ui';
import PDFIcon from '../../../../public/ic-pdf.svg?react';
import PDFIcon from '../../../../public/ic-pdf.svg';
ameerul-deriv marked this conversation as resolved.
Show resolved Hide resolved
import { ChatMessageReceipt } from '../ChatMessageReceipt';
import { ChatMessageText } from '../ChatMessageText';
import './ChatMessages.scss';
Expand Down
49 changes: 24 additions & 25 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
{
"compilerOptions": {
"baseUrl": "./",
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"compilerOptions": {
"baseUrl": "./",
"target": "ES2020",
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",

"esModuleInterop": true,
"esModuleInterop": true,

"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"types": ["@testing-library/jest-dom"],
"paths": {
"@/*": ["src/*"]
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"types": ["@testing-library/jest-dom"],
"paths": {
"@/*": ["src/*"]
}
},
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }],
"include": ["src", "vite-env-override.d.ts"],
"references": [{ "path": "./tsconfig.node.json" }]
}

Loading
Loading