diff --git a/components/button/src/button/__tests__/Button.test.js b/components/button/src/button/__tests__/Button.test.js index 9126fb798f..b36146c492 100644 --- a/components/button/src/button/__tests__/Button.test.js +++ b/components/button/src/button/__tests__/Button.test.js @@ -4,6 +4,52 @@ import React from 'react' import { Button } from '../button.js' describe(') + + expect(consoleSpy).not.toHaveBeenCalled() + }) + + it('does not warn if aria-label and title is present', () => { + render( + + ) + + expect(consoleSpy).not.toHaveBeenCalled() + }) + + it('warns if no children are present with no arial-label and title', () => { + render() + + expect(consoleSpy).toHaveBeenCalledWith( + 'Button component has no children but is missing title and ariaLabel attribute.' + ) + }) + + it('No warning if there are no children but arial label and title', () => { + render( + + ) + + expect(consoleSpy).not.toHaveBeenCalled() + }) + }) + it('renders a default data-test attribute', () => { const dataTest = 'dhis2-uicore-button' const wrapper = mount( diff --git a/components/button/src/split-button/split-button.test.js b/components/button/src/split-button/split-button.test.js new file mode 100644 index 0000000000..d7c8ef2027 --- /dev/null +++ b/components/button/src/split-button/split-button.test.js @@ -0,0 +1,85 @@ +import { render, fireEvent, cleanup, waitFor } from '@testing-library/react' +import React from 'react' +import '@testing-library/jest-dom/extend-expect' +import { SplitButton } from './split-button.js' + +describe('SplitButton', () => { + afterEach(cleanup) + + it('renders button with children', () => { + const { getByText } = render(Click me) + expect(getByText('Click me')).toBeInTheDocument() + }) + + it('toggles dropdown when left button is clicked', () => { + const { getByTestId, queryByTestId } = render() + const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') + + fireEvent.click(toggleButton) + expect( + queryByTestId('dhis2-uicore-splitbutton-menu') + ).toBeInTheDocument() + + fireEvent.click(toggleButton) + expect( + queryByTestId('dhis2-uicore-splitbutton-menu') + ).not.toBeInTheDocument() + }) + + it('renders dropdown content when open is true', () => { + const { getByTestId } = render( + Dropdown Content} /> + ) + + const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') + fireEvent.click(toggleButton) + + expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument() + }) + + it("does not close dropdown 'Enter' key is pressed", async () => { + const { getByTestId } = render( + Dropdown Content} /> + ) + + const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') + fireEvent.click(toggleButton) + expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument() + + fireEvent.keyDown(document, { key: 'Enter' }) + + // Use waitFor to wait for the DOM to update + await waitFor(() => { + expect( + getByTestId('dhis2-uicore-splitbutton-menu') + ).toBeInTheDocument() + }) + }) + + it('closes dropdown when escape key is pressed', async () => { + const { getByTestId, queryByTestId } = render( + Dropdown Content} /> + ) + + const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') + fireEvent.click(toggleButton) + expect(getByTestId('dhis2-uicore-splitbutton-menu')).toBeInTheDocument() + + fireEvent.keyDown(document, { key: 'Escape' }) + + // Use waitFor to wait for the DOM to update + await waitFor(() => { + expect( + queryByTestId('dhis2-uicore-splitbutton-menu') + ).not.toBeInTheDocument() + }) + }) + + it('adds title and aria-label attributes to the right button', () => { + const { getByTestId } = render() + const toggleButton = getByTestId('dhis2-uicore-splitbutton-toggle') + + expect(toggleButton).toHaveAttribute('title', 'Toggle dropdown') + expect(toggleButton).toHaveAttribute('aria-label', 'Toggle dropdown') + }) +}) diff --git a/components/loader/src/circular-loader/__tests__/circular-loader.test.js b/components/loader/src/circular-loader/__tests__/circular-loader.test.js new file mode 100644 index 0000000000..bc34f5f735 --- /dev/null +++ b/components/loader/src/circular-loader/__tests__/circular-loader.test.js @@ -0,0 +1,26 @@ +import { mount } from 'enzyme' +import React from 'react' +import { CircularLoader } from '../circular-loader.js' + +describe('Circular Loader', () => { + it('renders the circular loader with aria label', () => { + const wrapper = mount( + + ) + const actual = wrapper.find({ 'data-test': 'circular-loader-test' }) + expect(actual.prop('role')).toBe('progressbar') + expect(actual.prop('aria-label')).toBe('Circular Loader') + }) + + it('renders the circular loader without aria label', () => { + const wrapper = mount( + + ) + const actual = wrapper.find({ 'data-test': 'circular-loader-test' }) + expect(actual.prop('aria-label')).toBe(undefined) + expect(actual.prop('role')).toBe('progressbar') + }) +}) diff --git a/components/loader/src/circular-loader/circular-loader.js b/components/loader/src/circular-loader/circular-loader.js index 2ede281513..a512c321ff 100644 --- a/components/loader/src/circular-loader/circular-loader.js +++ b/components/loader/src/circular-loader/circular-loader.js @@ -10,6 +10,7 @@ const CircularLoader = ({ invert, className, dataTest, + 'aria-label': ariaLabel, }) => (