Skip to content

Commit

Permalink
Merge pull request #48 from ameerul-deriv/fix-test-cases-4
Browse files Browse the repository at this point in the history
Ameerul / Fix remaining test cases
  • Loading branch information
farrah-deriv authored May 6, 2024
2 parents 63577ab + 28dbbdc commit 06fa663
Show file tree
Hide file tree
Showing 27 changed files with 200 additions and 105 deletions.
3 changes: 2 additions & 1 deletion jest.config.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ module.exports = {
moduleNameMapper: {
'@deriv-com/(.*)': '<rootDir>/node_modules/@deriv-com/$1',
'\\.(css|less|sass|scss)$': 'identity-obj-proxy',
'\\.svg': '<rootDir>/__mocks__/svgMock.js',
'^@/(.*)$': '<rootDir>/src/$1',
'^@/assets/(.*)$': '<rootDir>/src/assets/$1',
'\\.svg': '<rootDir>/__mocks__/svgMock.js',
},
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
testEnvironment: 'jsdom',
testPathIgnorePatterns: ['useModalManager.spec.tsx'],
transform: {
'^.+\\.(js|jsx)$': 'babel-jest',
'^.+\\.tsx?$': 'ts-jest',
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@
"preview": "vite preview",
"test:lint": "prettier --log-level silent --write . && eslint \"./src/**/*.?(js|jsx|ts|tsx)\"",
"test": "jest --passWithNoTests",
"test:coverage": "jest --coverage --passWithNoTests",
"test:coverage": "jest --coverage --passWithNoTests --runInBand",
"prepare": "husky install"
},
"dependencies": {
"@babel/preset-env": "^7.24.5",
"@deriv-com/api-hooks": "^0.1.16",
"@deriv-com/api-hooks": "^0.1.18",
"@deriv-com/ui": "latest",
"@deriv-com/utils": "latest",
"@deriv/deriv-api": "^1.0.15",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ const mockProps = {
},
};

jest.mock('@deriv-com/api-hooks', () => ({
useGetSettings: jest.fn(() => ({ email: '[email protected]' })),
}));

jest.mock('@deriv-com/ui', () => ({
...jest.requireActual('@deriv-com/ui'),
useDevice: jest.fn(() => ({ isMobile: false })),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ const mockUseAdvertiserStats = {
isLoading: false,
};

jest.mock('../../../hooks', () => ({
...jest.requireActual('../../../hooks'),
jest.mock('@/hooks/custom-hooks', () => ({
...jest.requireActual('@/hooks/custom-hooks'),
useAdvertiserStats: jest.fn(() => mockUseAdvertiserStats),
}));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@ const mockProps = {
};
const mockUseAdvertiserUpdateMutate = jest.fn();

jest.mock('@deriv-com/api-hooks', () => ({
useP2pAdvertiserUpdate: jest.fn(() => ({
mutate: mockUseAdvertiserUpdateMutate,
})),
jest.mock('@/hooks', () => ({
api: {
advertiser: {
useUpdate: jest.fn(() => ({
mutate: mockUseAdvertiserUpdateMutate,
})),
},
},
}));

describe('AdvertiserNameToggle', () => {
Expand Down
26 changes: 14 additions & 12 deletions src/components/FloatingRate/__tests__/FloatingRate.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ jest.mock('@deriv-com/ui', () => ({
}));

jest.mock('@deriv-com/api-hooks', () => ({
useExchangeRateSubscription: () => ({
data: {
rates: {
USD: 1,
},
},
subscribe: jest.fn(),
}),
useP2PSettings: () => ({
data: {
override_exchange_rate: 1,
...jest.requireActual('@deriv-com/api-hooks'),
useExchangeRates: jest.fn(() => ({ subscribeRates: jest.fn() })),
}));

jest.mock('@/hooks', () => ({
...jest.requireActual('@/hooks'),
api: {
settings: {
useSettings: jest.fn(() => ({
data: {
override_exchange_rate: 1,
},
})),
},
}),
},
}));

const mockProps = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ jest.mock('@/hooks', () => ({
},
}));

jest.mock('@deriv-com/ui', () => ({
...jest.requireActual('@deriv-com/ui'),
useDevice: () => ({ isMobile: false }),
}));

describe('DailyLimitModal', () => {
it('should render loader when data is not ready', async () => {
render(<DailyLimitModal currency='USD' isModalOpen onRequestClose={mockOnRequestClose} />);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { api } from '@/hooks';
import { useAdvertiserInfoState } from '@/providers/AdvertiserInfoStateProvider';
import { render, screen, waitFor } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import NicknameModal from '../NicknameModal';

Expand Down Expand Up @@ -55,6 +55,8 @@ const mockProps = {
onRequestClose: jest.fn(),
};

const user = userEvent.setup({ delay: null });

describe('NicknameModal', () => {
it('should render title and description correctly', () => {
render(<NicknameModal {...mockProps} />);
Expand All @@ -66,14 +68,12 @@ describe('NicknameModal', () => {

const nicknameInput = screen.getByTestId('dt_nickname_modal_input');

await userEvent.type(nicknameInput, 'Nahida');
await user.type(nicknameInput, 'Nahida');

await waitFor(async () => {
const confirmBtn = screen.getByRole('button', {
name: 'Confirm',
});
await userEvent.click(confirmBtn);
const confirmBtn = screen.getByRole('button', {
name: 'Confirm',
});
await user.click(confirmBtn);

expect(mockedMutate).toHaveBeenCalledWith({
name: 'Nahida',
Expand Down Expand Up @@ -107,7 +107,7 @@ describe('NicknameModal', () => {
const cancelBtn = screen.getByRole('button', {
name: 'Cancel',
});
await userEvent.click(cancelBtn);
await user.click(cancelBtn);

expect(mockPush).toHaveBeenCalledWith('/buy-sell');
expect(mockProps.onRequestClose).toHaveBeenCalled();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { fireEvent, render, screen } from '@testing-library/react';
import OrderDetailsCancelModal from '../OrderDetailsCancelModal';

jest.mock('@deriv-com/ui', () => ({
Expand Down Expand Up @@ -47,12 +46,12 @@ describe('<OrderDetailsCancelModal />', () => {
expect(screen.getByRole('button', { name: 'Do not cancel' })).toBeInTheDocument();
});

it('should call the cancel function when the cancel button is clicked', async () => {
it('should call the cancel function when the cancel button is clicked', () => {
render(<OrderDetailsCancelModal {...mockProps} />);

const cancelButton = screen.getByRole('button', { name: 'Cancel this order' });

await userEvent.click(cancelButton);
fireEvent.click(cancelButton);

expect(mockProps.onRequestClose).toHaveBeenCalledTimes(1);
expect(mockMutate).toHaveBeenCalledTimes(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { render, screen, waitFor } from '@testing-library/react';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import OrderDetailsConfirmModal from '../OrderDetailsConfirmModal';

Expand Down Expand Up @@ -57,7 +57,20 @@ describe('<OrderDetailsConfirmModal />', () => {
const file = new File(['test'], 'test.mp4', { type: 'video/mp4' });
const fileInput = screen.getByTestId('dt_file_upload_input');

await userEvent.upload(fileInput, file);
const fileList = {
0: file,
item: () => file,
length: 1,
};

Object.defineProperty(fileInput, 'files', {
value: fileList,
});

// eslint-disable-next-line testing-library/no-unnecessary-act
await act(async () => {
fireEvent.change(fileInput);
});

await waitFor(() => {
expect(screen.getByText('The file you uploaded is not supported. Upload another.')).toBeInTheDocument();
Expand All @@ -71,11 +84,22 @@ describe('<OrderDetailsConfirmModal />', () => {
const file = new File([blob], 'test.png');
const fileInput = screen.getByTestId('dt_file_upload_input');

await userEvent.upload(fileInput, file);
const fileList = {
0: file,
item: () => file,
length: 1,
};

await waitFor(() => {
expect(screen.getByText('Cannot upload a file over 5MB')).toBeInTheDocument();
Object.defineProperty(fileInput, 'files', {
value: fileList,
});

// eslint-disable-next-line testing-library/no-unnecessary-act
await act(async () => {
fireEvent.change(fileInput);
});

expect(screen.getByText('Cannot upload a file over 5MB')).toBeInTheDocument();
});

it('should remove file when close icon is clicked', async () => {
Expand All @@ -84,10 +108,19 @@ describe('<OrderDetailsConfirmModal />', () => {
const file = new File(['test'], 'test.png', { type: 'image/png' });
const fileInput = screen.getByTestId('dt_file_upload_input');

await userEvent.upload(fileInput, file);
const fileList = {
0: file,
item: () => file,
length: 1,
};

await waitFor(() => {
expect(screen.getByText('test.png')).toBeInTheDocument();
Object.defineProperty(fileInput, 'files', {
value: fileList,
});

// eslint-disable-next-line testing-library/no-unnecessary-act
await act(async () => {
fireEvent.change(fileInput);
});

const closeIcon = screen.getByTestId('dt_remove_file_icon');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ const mockPaymentMethods = [
},
] as const;

const mockModalManager = {
hideModal: jest.fn(),
isModalOpenFor: jest.fn().mockReturnValue(false),
showModal: jest.fn(),
};

jest.mock('@deriv-com/ui', () => ({
...jest.requireActual('@deriv-com/ui'),
useDevice: () => ({ isMobile: false }),
Expand All @@ -72,6 +78,11 @@ jest.mock('@/hooks', () => {
};
});

jest.mock('@/hooks/custom-hooks', () => ({
useModalManager: jest.fn(() => mockModalManager),
useQueryString: jest.fn().mockReturnValue({ queryString: { tab: '' } }),
}));

const mockUseCreate = api.advertiserPaymentMethods.useCreate as jest.MockedFunction<
typeof api.advertiserPaymentMethods.useCreate
>;
Expand Down Expand Up @@ -207,8 +218,10 @@ describe('PaymentMethodForm', () => {
onResetFormState={jest.fn()}
/>
);
const dropdown = screen.getByText('Payment method');
const dropdown = screen.getByRole('combobox');

await userEvent.click(dropdown);

const dropdownItem = screen.getByText('Bank Transfer');
await userEvent.click(dropdownItem);
const otherPaymentMethod = mockPaymentMethods.find(method => method.type === 'bank');
Expand Down Expand Up @@ -339,6 +352,7 @@ describe('PaymentMethodForm', () => {
});
it('should handle onclick when the back arrow is clicked and the form is dirty, and close the opened modal when go back button is clicked', async () => {
const otherPaymentMethod = mockPaymentMethods.find(method => method.type === 'other');
mockModalManager.isModalOpenFor.mockImplementation((modalName: string) => modalName === 'PaymentMethodModal');
render(
<PaymentMethodForm
formState={{
Expand Down Expand Up @@ -368,6 +382,7 @@ describe('PaymentMethodForm', () => {
});
it("should handle onclick when the back arrow is clicked and the form is dirty, and close the opened modal when don't cancel button is clicked", async () => {
const otherPaymentMethod = mockPaymentMethods.find(method => method.type === 'other');
mockModalManager.isModalOpenFor.mockImplementation((modalName: string) => modalName === 'PaymentMethodModal');
render(
<PaymentMethodForm
formState={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ let mockProps = {

describe('<ProfileStats />', () => {
it('should render no results if advertiserStats is empty', () => {
render(<ProfileStats advertiserStats={{}} />);
render(<ProfileStats advertiserStats={undefined} />);

expect(screen.queryByText('Buy completion 30d')).not.toBeInTheDocument();
expect(screen.queryByText('Sell completion 30d')).not.toBeInTheDocument();
Expand Down
11 changes: 11 additions & 0 deletions src/components/ProfileContent/__tests__/ProfileContent.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ jest.mock('@deriv-com/ui', () => ({
useDevice: jest.fn(() => ({ isMobile: false })),
}));

jest.mock('@/hooks/custom-hooks', () => ({
useAdvertiserStats: jest.fn(() => ({
data: {
isAddressVerified: false,
isIdentityVerified: false,
totalOrders: 10,
},
isLoading: false,
})),
}));

const mockUseDevice = useDevice as jest.Mock;

describe('ProfileContent', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/hooks/__tests__/useModalManager.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jest.mock('react-router-dom', () => ({
}));

const mockedUseQueryString = useQueryString as jest.MockedFunction<typeof useQueryString>;
jest.mock('@/hooks/useQueryString', () => ({
jest.mock('@/hooks/custom-hooks/useQueryString', () => ({
__esModule: true,
default: jest.fn(() => ({
deleteQueryString: jest.fn(),
Expand Down
1 change: 0 additions & 1 deletion src/hooks/api/advert/p2p-advert/useAdvertList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ type TPayload = NonNullable<Parameters<typeof useP2PAdvertList>[0]>['payload'];
const useAdvertList = (payload?: TPayload) => {
const { data, loadMoreAdverts, ...rest } = useP2PAdvertList({
payload: { ...payload, limit: payload?.limit, offset: payload?.offset },
queryKey: [{ ...payload }],
});

// Add additional information to the 'p2p_advert_list' data
Expand Down
Loading

0 comments on commit 06fa663

Please sign in to comment.