Skip to content

Commit

Permalink
use react testing library
Browse files Browse the repository at this point in the history
  • Loading branch information
thisisamir98 committed Oct 3, 2022
1 parent 1d44efd commit b773820
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 28 deletions.
39 changes: 12 additions & 27 deletions src/script/components/Modals/PrimaryModal/PrimaryModal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,19 @@
*
*/

import {act} from '@testing-library/react';

import TestPage from 'Util/test/TestPage';
import {render, fireEvent, act} from '@testing-library/react';
import PrimaryModal from '.';
import {PrimaryModalComponent} from './PrimaryModal';

class PrimaryModalPage extends TestPage<{}> {
constructor() {
super(PrimaryModalComponent);
}

getWrapperElement = () => this.get('div#modals');
getPrimaryActionButton = () => this.get('[data-uie-name="do-action"]');
getSecondaryActionButton = () => this.get('[data-uie-name="do-secondary"]');
}

describe('PrimaryModal', () => {
it('does not render when no item is in the queue', async () => {
const PrimaryModalWrapper = new PrimaryModalPage();
expect(PrimaryModalWrapper.getWrapperElement()?.children[0].getAttribute('style')).toBe('display: none;');
const {getByTestId} = render(<PrimaryModalComponent />);
const PrimaryModalWrapper = getByTestId('primary-modals-container');
expect(PrimaryModalWrapper.children[0].getAttribute('style')).toBe('display: none;');
});

it('correctly calls action callback', async () => {
const PrimaryModalWrapper = new PrimaryModalPage();
const {getByTestId} = render(<PrimaryModalComponent />);
const actionCallback = jest.fn();
act(() => {
PrimaryModal.show(PrimaryModal.type.CONFIRM, {
Expand All @@ -58,16 +47,14 @@ describe('PrimaryModal', () => {
},
});
});
const actionButton = PrimaryModalWrapper.getPrimaryActionButton();
if (!actionButton) {
throw new Error('Failed to find action button');
}
PrimaryModalWrapper.click(actionButton);

const actionButton = getByTestId('do-action');
fireEvent.click(actionButton);

expect(actionCallback).toHaveBeenCalledTimes(1);
});
it('correctly calls secondary action callback', async () => {
const PrimaryModalWrapper = new PrimaryModalPage();
const {getByTestId} = render(<PrimaryModalComponent />);
const secondaryActionCallback = jest.fn();
act(() => {
PrimaryModal.show(PrimaryModal.type.CONFIRM, {
Expand All @@ -85,11 +72,9 @@ describe('PrimaryModal', () => {
},
});
});
const secondaryActionButton = PrimaryModalWrapper.getSecondaryActionButton();
if (!secondaryActionButton) {
throw new Error('Failed to find action button');
}
PrimaryModalWrapper.click(secondaryActionButton);

const secondaryActionButton = getByTestId('do-secondary');
fireEvent.click(secondaryActionButton);

expect(secondaryActionCallback).toHaveBeenCalledTimes(1);
});
Expand Down
2 changes: 1 addition & 1 deletion src/script/components/Modals/PrimaryModal/PrimaryModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export const PrimaryModalComponent: React.FC = () => {
const secondaryActions = Array.isArray(secondaryAction) ? secondaryAction : [secondaryAction];

return (
<div id="modals">
<div id="modals" data-uie-name="primary-modals-container">
<ModalComponent
isShown={isModalVisible}
onClosed={onModalHidden}
Expand Down

0 comments on commit b773820

Please sign in to comment.