Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(ui-checkbox): migrate old Checkbox tests #1312

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions packages/ui-checkbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@
"devDependencies": {
"@instructure/ui-babel-preset": "8.46.1",
"@instructure/ui-color-utils": "8.46.1",
"@instructure/ui-test-locator": "8.46.1",
"@instructure/ui-test-utils": "8.46.1",
"@instructure/ui-themes": "8.46.1"
"@instructure/ui-themes": "8.46.1",
"@instructure/ui-axe-check": "8.46.1",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^14.0.0",
"@testing-library/user-event": "^14.4.3"
},
"peerDependencies": {
"react": ">=16.8 <=18"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,26 @@
*/

import React from 'react'
import { expect, mount, within } from '@instructure/ui-test-utils'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'

import { runAxeCheck } from '@instructure/ui-axe-check'
import { CheckboxFacade } from '../index'

describe('<CheckboxFacade />', async () => {
it('should render', async () => {
const subject = await mount(<CheckboxFacade>label text</CheckboxFacade>)
const checkboxFacade = within(subject.getDOMNode())
expect(checkboxFacade).to.exist()
const TEST_TEXT = 'test-text'

describe('<CheckboxFacade />', () => {
it('should render', () => {
render(<CheckboxFacade>{TEST_TEXT}</CheckboxFacade>)
const facade = screen.getByText(TEST_TEXT)

expect(facade).toBeInTheDocument()
})

it('should meet a11y standards', async () => {
const subject = await mount(<CheckboxFacade>label text</CheckboxFacade>)
const checkboxFacade = within(subject.getDOMNode())
expect(await checkboxFacade.accessible()).to.be.true()
const { container } = render(<CheckboxFacade>{TEST_TEXT}</CheckboxFacade>)
const axeCheck = await runAxeCheck(container)

expect(axeCheck).toBe(true)
})
})
30 changes: 0 additions & 30 deletions packages/ui-checkbox/src/Checkbox/CheckboxLocator.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,26 @@
*/

import React from 'react'
import { expect, mount, within } from '@instructure/ui-test-utils'
import { render, screen } from '@testing-library/react'
import '@testing-library/jest-dom/extend-expect'

import { runAxeCheck } from '@instructure/ui-axe-check'
import { ToggleFacade } from '../index'

describe('<ToggleFacade />', async () => {
it('should render', async () => {
const subject = await mount(<ToggleFacade>label text</ToggleFacade>)
const toggleFacade = within(subject.getDOMNode())
expect(toggleFacade).to.exist()
const TEST_TEXT = 'test-text'

describe('<ToggleFacade />', () => {
it('should render', () => {
render(<ToggleFacade>{TEST_TEXT}</ToggleFacade>)
const facade = screen.getByText(TEST_TEXT)

expect(facade).toBeInTheDocument()
})

it('should meet a11y standards', async () => {
const subject = await mount(<ToggleFacade>label text</ToggleFacade>)
const toggleFacade = within(subject.getDOMNode())
expect(await toggleFacade.accessible()).to.be.true()
const { container } = render(<ToggleFacade>{TEST_TEXT}</ToggleFacade>)
const axeCheck = await runAxeCheck(container)

expect(axeCheck).toBe(true)
})
})
221 changes: 221 additions & 0 deletions packages/ui-checkbox/src/Checkbox/__new-tests__/Checkbox.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 - present Instructure, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

import React from 'react'
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import '@testing-library/jest-dom/extend-expect'

import { runAxeCheck } from '@instructure/ui-axe-check'
import { Checkbox } from '../index'
import { CheckboxProps } from '../props'

const TEST_VALUE = 'test-value'
const TEST_NAME = 'test-name'
const TEST_LABEL = 'test-label'

const initProps = {
label: TEST_LABEL,
defaultChecked: true,
value: TEST_VALUE,
name: TEST_NAME
}

const renderCheckbox = (props?: Partial<CheckboxProps>) => {
const allProps: CheckboxProps = {
...initProps,
...props
}
return render(<Checkbox {...allProps} />)
}

describe('<Checkbox />', () => {
it('renders an input with type "checkbox"', () => {
renderCheckbox()
const inputElem = screen.getByRole('checkbox')

expect(inputElem).toBeInTheDocument()
expect(inputElem.tagName).toBe('INPUT')
expect(inputElem).toHaveAttribute('type', 'checkbox')
})

it('`simple` variant only displays a checkmark when checked', async () => {
const { container } = renderCheckbox({
variant: 'simple',
defaultChecked: false
})
const checkboxElement = container.querySelector('input[type="checkbox"]')
const svgElement = container.querySelector('svg')

expect(svgElement).not.toBeInTheDocument()

userEvent.click(checkboxElement!)
await waitFor(() => {
const svgElementAfterClick = container.querySelector('svg')
expect(svgElementAfterClick).toBeInTheDocument()
})
})

it('`simple` variant supports indeterminate/mixed state', () => {
renderCheckbox({ variant: 'simple', indeterminate: true })

const inputElem = screen.getByRole('checkbox')

expect(inputElem).toBeInTheDocument()
expect(inputElem).toHaveAttribute('aria-checked', 'mixed')
})

describe('events', () => {
it('when clicked, fires onClick and onChange events', async () => {
const onClick = jest.fn()
const onChange = jest.fn()
renderCheckbox({ onClick, onChange })
const checkboxElement = screen.getByRole('checkbox')

userEvent.click(checkboxElement)

await waitFor(() => {
expect(onClick).toHaveBeenCalled()
expect(onChange).toHaveBeenCalled()
})
})

it('when clicked, does not call onClick or onChange when disabled', async () => {
const onClick = jest.fn()
const onChange = jest.fn()
renderCheckbox({ onClick, onChange, disabled: true })
const checkboxElement = screen.getByRole('checkbox')

fireEvent.click(checkboxElement)

await waitFor(() => {
expect(onClick).not.toHaveBeenCalled()
expect(onChange).not.toHaveBeenCalled()
expect(checkboxElement).toBeDisabled()
})
})

it('when clicked, does not call onClick or onChange when readOnly', async () => {
const onClick = jest.fn()
const onChange = jest.fn()
renderCheckbox({ onClick, onChange, readOnly: true })
const checkboxElement = screen.getByRole('checkbox')

fireEvent.click(checkboxElement)

await waitFor(() => {
expect(onClick).not.toHaveBeenCalled()
expect(onChange).not.toHaveBeenCalled()
})
})

it('calls onChange when enter key is pressed', async () => {
const onChange = jest.fn()
renderCheckbox({ onChange })
const checkboxElement = screen.getByRole('checkbox')

userEvent.type(checkboxElement, '{enter}')

await waitFor(() => {
expect(onChange).toHaveBeenCalled()
})
})

it('responds to onBlur event', async () => {
const onBlur = jest.fn()
renderCheckbox({ onBlur })

userEvent.tab()
userEvent.tab()

await waitFor(() => {
expect(onBlur).toHaveBeenCalled()
})
})

it('responds to onFocus event', async () => {
const onFocus = jest.fn()
renderCheckbox({ onFocus })

userEvent.tab()

await waitFor(() => {
expect(onFocus).toHaveBeenCalled()
})
})

it('focuses with the focus helper', () => {
const checkboxRef = React.createRef<Checkbox>()
render(<Checkbox ref={checkboxRef} {...initProps} />)
const checkboxElement = screen.getByRole('checkbox')

expect(checkboxElement).not.toHaveFocus()

checkboxRef.current?.focus()

expect(checkboxElement).toHaveFocus()
})

it('calls onMouseOver', async () => {
const onMouseOver = jest.fn()
renderCheckbox({ onMouseOver })
const checkboxElement = screen.getByRole('checkbox')

userEvent.hover(checkboxElement)

await waitFor(() => {
expect(onMouseOver).toHaveBeenCalled()
})
})

it('calls onMouseOut', async () => {
const onMouseOut = jest.fn()
renderCheckbox({ onMouseOut })
const checkboxElement = screen.getByRole('checkbox')

userEvent.hover(checkboxElement)
userEvent.unhover(checkboxElement)

await waitFor(() => {
expect(onMouseOut).toHaveBeenCalled()
})
})
})

describe('for a11y', () => {
it('`simple` variant should meet standards', async () => {
const { container } = renderCheckbox({ variant: 'simple' })
const axeCheck = await runAxeCheck(container)

expect(axeCheck).toBe(true)
})

it('`toggle` variant should meet standards', async () => {
const { container } = renderCheckbox({ variant: 'toggle' })
const axeCheck = await runAxeCheck(container)

expect(axeCheck).toBe(true)
})
})
})
Loading
Loading