From 3402856fdcbeb89381dfdf03afac0f624fd4b655 Mon Sep 17 00:00:00 2001 From: Nick Taylor Date: Wed, 29 Jul 2020 10:35:42 -0400 Subject: [PATCH] Make component more accessible (#9563) * Used for modal to stay on the semantic HTML train. * Added tests and fixed a11y issues with component. --- app/javascript/crayons/Modal/Modal.jsx | 41 +++++----- .../crayons/Modal/__tests__/Modal.test.jsx | 77 +++++++++++++++++++ 2 files changed, 99 insertions(+), 19 deletions(-) create mode 100644 app/javascript/crayons/Modal/__tests__/Modal.test.jsx diff --git a/app/javascript/crayons/Modal/Modal.jsx b/app/javascript/crayons/Modal/Modal.jsx index dddaa85ee..b214e0f2f 100644 --- a/app/javascript/crayons/Modal/Modal.jsx +++ b/app/javascript/crayons/Modal/Modal.jsx @@ -17,6 +17,21 @@ function getAdditionalClassNames({ size, className }) { return additionalClassNames; } +const CloseIcon = () => ( + + Close + + +); + export const Modal = ({ children, size = 'default', @@ -25,44 +40,32 @@ export const Modal = ({ overlay, onClose, }) => { - const Icon = () => ( - - Close - - - ); - return (
-
+
{title.length > 0 && title && (

{title}

)}
{children}
- {overlay &&
} + {overlay && ( +
+ )}
); }; diff --git a/app/javascript/crayons/Modal/__tests__/Modal.test.jsx b/app/javascript/crayons/Modal/__tests__/Modal.test.jsx new file mode 100644 index 000000000..1af598725 --- /dev/null +++ b/app/javascript/crayons/Modal/__tests__/Modal.test.jsx @@ -0,0 +1,77 @@ +import { h } from 'preact'; +import { axe } from 'jest-axe'; +import { render } from '@testing-library/preact'; +import { Modal } from '../Modal'; + +it('should have no a11y violations', async () => { + const { container } = render( + This is the modal body content, + ); + const results = await axe(container); + + expect(results).toHaveNoViolations(); +}); + +it('should close when the close button is clicked', async () => { + const onClose = jest.fn(); + const { getByLabelText } = render( + + This is the modal body content + , + ); + + const closeButton = getByLabelText('Close', { selector: 'button' }); + + closeButton.click(); + + expect(onClose).toHaveBeenCalledTimes(1); +}); + +it('should render with additional class names', async () => { + const { getByTestId } = render( + + This is the modal body content + , + ); + + const modalContainer = getByTestId('modal-container'); + + expect( + modalContainer.classList.contains('some-additional-class-name'), + ).toEqual(true); +}); + +it('should render with an overlay', async () => { + const { getByTestId } = render( + + This is the modal body content + , + ); + + const modalOverlay = getByTestId('modal-overlay'); + + expect(modalOverlay).not.toBeNull(); +}); + +it('should render with a different size modal', async () => { + const { getByTestId } = render( + + This is the modal body content + , + ); + + const modalContainer = getByTestId('modal-container'); + + expect(modalContainer.classList.contains('crayons-modal--large')).toEqual( + true, + ); +});