Skip to content

Commit

Permalink
Merge pull request #4 from suisin-deriv/update_api_token_test_case_ba…
Browse files Browse the repository at this point in the history
…sed_on_comments

Update api token test case based on comments
  • Loading branch information
utkarsha-deriv committed Sep 6, 2023
2 parents 3a407a9 + a2e18be commit 5dd89d8
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ it('should render ApiTokenArticle', () => {
expect(screen.getByText('API token'));
expect(
screen.getByText(
`To access your mobile apps and other third-party apps, you'll first need to generate an API token.`
/To access your mobile apps and other third-party apps, you'll first need to generate an API token./i
)
).toBeInTheDocument();
});
Original file line number Diff line number Diff line change
@@ -1,53 +1,35 @@
import React from 'react';
import { Formik, Form } from 'formik';
import { screen, render } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import ApiTokenCard from '../api-token-card';
import { Formik, Form } from 'formik';

describe('<ApiTokenCard />', () => {
const mock_props = {
name: 'Api_token_card_test_case',
value: false,
display_name: 'API Token Card',
description: 'API Token Description',
setFieldValue: jest.fn(),
display_name: <div>API Token Card</div>,
description: <div>API Token Description</div>,
};

it('should render ApiTokenCard', () => {
const renderComponent = (children?: JSX.Element) => {
render(
<Formik initialValues={{ [mock_props.name]: mock_props.value }} onSubmit={jest.fn()}>
<Form>
<ApiTokenCard {...mock_props} />
<ApiTokenCard {...mock_props}>{children}</ApiTokenCard>
</Form>
</Formik>
);
};

it('should render ApiTokenCard', () => {
renderComponent();
expect(screen.getByText('API Token Card')).toBeInTheDocument();
expect(screen.getByText('API Token Description')).toBeInTheDocument();
});

it('should render ApiTokenCard with children', () => {
const children = <div>API Token Children</div>;

render(
<Formik initialValues={{ [mock_props.name]: mock_props.value }} onSubmit={jest.fn()}>
<Form>
<ApiTokenCard {...mock_props}>{children}</ApiTokenCard>
</Form>
</Formik>
);
renderComponent(children);
expect(screen.getByText('API Token Children')).toBeInTheDocument();
});

it('should run setFieldValue after clicking on checkbox', () => {
render(
<Formik initialValues={{ [mock_props.name]: mock_props.value }} onSubmit={jest.fn()}>
<Form>
<ApiTokenCard {...mock_props} />
</Form>
</Formik>
);
const message = screen.getByText('API Token Card');
userEvent.click(message);
expect(mock_props.setFieldValue).toBeCalled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ document.body.appendChild(modal_root_el);

describe('ApiTokenDeleteButton', () => {
const mock_props = {
api_tokens: undefined,
api_tokens: [
{
display_name: '',
last_used: '',
scopes: [],
token: '',
},
],
deleteToken: jest.fn(() => Promise.resolve()),
footer_ref: document.createElement('div'),
overlay_ref: document.createElement('div'),
Expand All @@ -25,7 +32,7 @@ describe('ApiTokenDeleteButton', () => {
},
};

const renderAPiDeleteButton = () => {
const renderAPIDeleteButton = () => {
render(
<ApiTokenContext.Provider value={mock_props}>
<ApiTokenDeleteButton {...mock_token} />
Expand All @@ -34,20 +41,20 @@ describe('ApiTokenDeleteButton', () => {
};

it('should render ApiTokenDeleteButton', () => {
renderAPiDeleteButton();
renderAPIDeleteButton();
expect(screen.getByTestId('dt_token_delete_icon')).toBeInTheDocument();
expect(screen.queryByText('Delete this token')).not.toBeInTheDocument();
});

it('should display Delete this token when mouse enter', () => {
renderAPiDeleteButton();
renderAPIDeleteButton();
const delete_icon = screen.getByTestId('dt_token_delete_icon');
userEvent.hover(delete_icon);
expect(screen.getByText('Delete this token')).toBeInTheDocument();
});

it('should not Delete this token when mouse leave', () => {
renderAPiDeleteButton();
renderAPIDeleteButton();
const delete_icon = screen.getByTestId('dt_token_delete_icon');
userEvent.hover(delete_icon);
expect(screen.getByText('Delete this token')).toBeInTheDocument();
Expand All @@ -56,7 +63,7 @@ describe('ApiTokenDeleteButton', () => {
});

it('should display Popup when delete icon is clicked', () => {
renderAPiDeleteButton();
renderAPIDeleteButton();
const delete_icon = screen.getByTestId('dt_token_delete_icon');
userEvent.click(delete_icon);
expect(screen.getByText('Delete token')).toBeInTheDocument();
Expand All @@ -66,7 +73,7 @@ describe('ApiTokenDeleteButton', () => {
});

it('should close the modal when clicked on Cancel', async () => {
renderAPiDeleteButton();
renderAPIDeleteButton();
const delete_icon = screen.getByTestId('dt_token_delete_icon');
userEvent.click(delete_icon);
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
Expand All @@ -79,7 +86,7 @@ describe('ApiTokenDeleteButton', () => {
});

it('should should trigger deleteToken when clicked on Yes, delete', async () => {
renderAPiDeleteButton();
renderAPIDeleteButton();
const delete_icon = screen.getByTestId('dt_token_delete_icon');
userEvent.click(delete_icon);
expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
Expand Down

0 comments on commit 5dd89d8

Please sign in to comment.