Skip to content
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
9 changes: 6 additions & 3 deletions package-lock.json

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

9 changes: 6 additions & 3 deletions packages/ui-toggle-details/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
},
"license": "MIT",
"devDependencies": {
"@instructure/ui-axe-check": "10.14.0",
"@instructure/ui-babel-preset": "10.14.0",
"@instructure/ui-test-locator": "10.14.0",
"@instructure/ui-test-queries": "10.14.0",
"@instructure/ui-test-utils": "10.14.0",
"@instructure/ui-themes": "10.14.0"
"@instructure/ui-themes": "10.14.0",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
"vitest": "^2.1.8"
},
"dependencies": {
"@babel/runtime": "^7.26.0",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
/*
* 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 { render, screen, waitFor } from '@testing-library/react'
import { runAxeCheck } from '@instructure/ui-axe-check'
import userEvent from '@testing-library/user-event'
import { vi } from 'vitest'
import type { MockInstance } from 'vitest'

import '@testing-library/jest-dom'
import { ToggleDetails } from '../index'

describe('<ToggleDetails />', () => {
let consoleWarningMock: ReturnType<typeof vi.spyOn>
let consoleErrorMock: ReturnType<typeof vi.spyOn>

beforeEach(() => {
// Mocking console to prevent test output pollution
consoleWarningMock = vi
.spyOn(console, 'warn')
.mockImplementation(() => {}) as MockInstance
consoleErrorMock = vi
.spyOn(console, 'error')
.mockImplementation(() => {}) as MockInstance
})

afterEach(() => {
consoleWarningMock.mockRestore()
consoleErrorMock.mockRestore()
})

it('should hide its content', async () => {
render(<ToggleDetails summary="Click me">Content</ToggleDetails>)
const content = screen.queryByText('Content')

expect(content).not.toBeInTheDocument()
})

it('should place the icon after the summary when prop is set', async () => {
const { container } = render(
<ToggleDetails iconPosition="end" summary="Click me">
Content
</ToggleDetails>
)
const summary = container.querySelector(
'[class$="-toggleDetails__summaryText"]'
)
const icon = container.querySelector('[class$="-toggleDetails__icon"]')

expect(icon?.previousSibling).toBe(summary)
})

it('should have an aria-controls attribute', async () => {
const { container } = render(
<ToggleDetails summary="Click me">Details</ToggleDetails>
)

const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')
const content = container.querySelector(
'[class$="-toggleDetails__details"]'
)

expect(toggle).toHaveAttribute('aria-controls', content?.id)
})

it('should have an aria-expanded attribute', async () => {
const { container } = render(
<ToggleDetails summary="Click me">Details</ToggleDetails>
)
const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')

expect(toggle).toHaveAttribute('aria-expanded', 'false')
})

it('should not have aria attributes when it has no children', async () => {
render(
<ToggleDetails summary="Click me" data-testid="td__0"></ToggleDetails>
)
const toggle = screen.getByTestId('td__0')

expect(toggle).not.toHaveAttribute('aria-controls')
expect(toggle).not.toHaveAttribute('aria-expanded')
})

it('should toggle on click events', async () => {
const { container } = render(
<ToggleDetails summary="Click me">Details</ToggleDetails>
)
const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')

expect(toggle).toHaveAttribute('aria-expanded', 'false')

await userEvent.click(screen.getByText('Click me'))

await waitFor(() => {
expect(toggle).toHaveAttribute('aria-expanded', 'true')
})
})

it('should call onToggle on click events', async () => {
const onToggle = vi.fn()

render(
<ToggleDetails summary="Click me" expanded={false} onToggle={onToggle}>
Details
</ToggleDetails>
)

await userEvent.click(screen.getByText('Click me'))

await waitFor(() => {
const args = onToggle.mock.calls[0]

expect(onToggle).toHaveBeenCalledTimes(1)
expect(args[0].type).toBe('click')
expect(args[1]).toBe(true)
})
})

it('should call onToggle on click events when it has no children', async () => {
const onToggle = vi.fn()

render(
<ToggleDetails
summary="Click me"
expanded={false}
onToggle={onToggle}
data-testid="td__1"
></ToggleDetails>
)
const toggle = screen.getByTestId('td__1')
await userEvent.click(toggle)

await waitFor(() => {
const args = onToggle.mock.calls[0]

expect(onToggle).toHaveBeenCalledTimes(1)
expect(args[0].type).toBe('click')
expect(args[1]).toBe(true)
})
})

it('should be initialized by defaultExpanded prop', async () => {
const { container } = render(
<ToggleDetails summary="Click me" defaultExpanded>
Content
</ToggleDetails>
)
const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')
const content = container.querySelector(
'[class$="-toggleDetails__details"]'
)

expect(toggle).toHaveAttribute('aria-expanded', 'true')
expect(content).toHaveTextContent('Content')
})

it('should meet a11y standards', async () => {
const { container } = render(
<ToggleDetails summary="Click me">Content</ToggleDetails>
)
const axeCheck = await runAxeCheck(container)

expect(axeCheck).toBe(true)
})

it('focuses with the focus helper', async () => {
let toggleRef: any

const { container } = render(
<ToggleDetails
summary="Click me"
ref={(el) => {
toggleRef = el
}}
>
Content
</ToggleDetails>
)
const toggle = container.querySelector('[class$="-toggleDetails__toggle"]')

expect(document.activeElement).not.toBe(toggle)

toggleRef?.focus()

expect(document.activeElement).toBe(toggle)
})
})
Loading
Loading