Skip to content

Commit

Permalink
test(Modal): add EDS assert for prop misuse
Browse files Browse the repository at this point in the history
  • Loading branch information
booc0mtaco committed Jul 10, 2024
1 parent 59c8de0 commit 4339ae8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/components/Modal/Modal.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,26 @@ describe('Modal', () => {
expect(renderMethod).toThrow(Error);
consoleErrorMock.mockRestore();
});

it('prints a warning when height is used with size="sm"', () => {
const modalWithoutTitleOrAriaLabel = (
<Modal height="auto" onClose={() => {}} open size="sm">
<Modal.Header>
<Modal.Title>Modal Title</Modal.Title>
</Modal.Header>
<Modal.Body>Modal body content.</Modal.Body>
<Modal.Footer>Modal footer content.</Modal.Footer>
</Modal>
);
const renderMethod = () => {
render(modalWithoutTitleOrAriaLabel);
};

// expect console error from react, suppressed.
const consoleWarningMock = jest.spyOn(console, 'warn');
consoleWarningMock.mockImplementation();
expect(renderMethod).not.toThrow();
expect(consoleWarningMock).toHaveBeenCalledTimes(1);
consoleWarningMock.mockRestore();
});
});
7 changes: 6 additions & 1 deletion src/components/Modal/Modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import clsx from 'clsx';
import type { MutableRefObject, ReactNode } from 'react';
import React from 'react';

import { assertEdsUsage } from '../../util/logging';
import type { ExtractProps } from '../../util/utility-types';
import type { Size } from '../../util/variant-types';

Expand Down Expand Up @@ -302,7 +303,11 @@ export const Modal = (props: ModalProps) => {
}
}

// TODO-AH: add check to make sure folks aren't using size="lg" with "height"
// check to make sure folks aren't using size="lg" with "height"
assertEdsUsage(
[rest.size !== 'lg' && typeof rest.height !== 'undefined'],
'Height is only supported when size is set to "lg"',
);

const componentClassName = clsx(styles['modal'], modalContainerClassName);

Expand Down

0 comments on commit 4339ae8

Please sign in to comment.