Skip to content

Commit

Permalink
chore: transfer message unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
lubega-deriv committed May 17, 2024
1 parent 92c487e commit 3882059
Showing 1 changed file with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import React from 'react';
import { useFormikContext } from 'formik';
import { BrowserRouter as Router } from 'react-router-dom';
import { render, screen } from '@testing-library/react';
import { useTransferMessages } from '../../../hooks';
import { useTransfer } from '../../../provider';
import TransferMessages from '../TransferMessages';

jest.mock('formik', () => ({
useFormikContext: jest.fn(),
}));

jest.mock('../../../hooks', () => ({
useTransferMessages: jest.fn(),
}));

jest.mock('../../../provider', () => ({
useTransfer: jest.fn(),
}));

describe('TransferMessages', () => {
const setFieldValueMock = jest.fn();
const mockValues = {
fromAccount: 'account1',
toAccount: 'account2',
};

beforeEach(() => {
(useFormikContext as jest.Mock).mockReturnValue({
setFieldValue: setFieldValueMock,
values: mockValues,
});

(useTransfer as jest.Mock).mockReturnValue({
accountLimits: {},
activeWalletExchangeRates: {},
USDExchangeRates: {},
});

(useTransferMessages as jest.Mock).mockReturnValue([
{
action: { buttonLabel: 'Action', navigateTo: '/action', shouldOpenInNewTab: true },
message: { text: 'Error message', values: {} },
type: 'error',
},
{
action: null,
message: { text: 'Info message', values: {} },
type: 'info',
},
]);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should render messages correctly', () => {
render(
<Router>
<TransferMessages />
</Router>
);

expect(screen.getByText('Error message')).toBeInTheDocument();
expect(screen.getByText('Info message')).toBeInTheDocument();

const actionLink = screen.getByRole('link', { name: 'Action' });
expect(actionLink).toHaveAttribute('href', '/action');
expect(actionLink).toHaveAttribute('rel', 'noopener noreferrer');
expect(actionLink).toHaveAttribute('target', '_blank');
});

it('should set isError field based on messages', () => {
render(
<Router>
<TransferMessages />
</Router>
);

expect(setFieldValueMock).toHaveBeenCalledWith('isError', true);
});
});

0 comments on commit 3882059

Please sign in to comment.