-
Notifications
You must be signed in to change notification settings - Fork 625
Migrate batch of components from Jest to Vitest #6356
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
Changes from all commits
f31ba42
c325650
db81b05
afbaf71
d8234d9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,41 @@ | ||
import {describe, expect, it} from 'vitest' | ||
import {render} from '@testing-library/react' | ||
import {Header} from '..' | ||
import {render, behavesAsComponent, checkExports} from '../utils/testing' | ||
import {render as HTMLRender} from '@testing-library/react' | ||
import axe from 'axe-core' | ||
|
||
describe('Header', () => { | ||
behavesAsComponent({Component: Header}) | ||
|
||
checkExports('Header', { | ||
default: Header, | ||
}) | ||
|
||
describe('Header.Item', () => { | ||
behavesAsComponent({Component: Header.Item, options: {skipAs: true}}) | ||
|
||
it('accepts and applies className', () => { | ||
expect(render(<Header.Item className="primer" />).props.className).toContain('primer') | ||
const {container} = render(<Header.Item className="primer" />) | ||
expect(container.firstChild).toHaveClass('primer') | ||
}) | ||
|
||
it('should support `className` on the outermost element', () => { | ||
const Element = () => <Header.Item className={'test-class-name'} /> | ||
expect(HTMLRender(<Element />).container.firstChild).toHaveClass('test-class-name') | ||
expect(render(<Element />).container.firstChild).toHaveClass('test-class-name') | ||
}) | ||
}) | ||
|
||
describe('Header.Link', () => { | ||
behavesAsComponent({Component: Header.Link}) | ||
|
||
it('should support `className` on the outermost element', () => { | ||
const Element = () => <Header.Link className={'test-class-name'} /> | ||
expect(HTMLRender(<Element />).container.firstChild).toHaveClass('test-class-name') | ||
expect(render(<Element />).container.firstChild).toHaveClass('test-class-name') | ||
}) | ||
}) | ||
|
||
it('should have no axe violations', async () => { | ||
const {container} = HTMLRender( | ||
<Header> | ||
<Header.Item full> | ||
<Header.Link href="#">One</Header.Link> | ||
</Header.Item> | ||
<Header.Item> | ||
<Header.Link href="#">Two</Header.Link> | ||
</Header.Item> | ||
<Header.Item>Three</Header.Item> | ||
</Header>, | ||
) | ||
const results = await axe.run(container) | ||
expect(results).toHaveNoViolations() | ||
}) | ||
|
||
it('renders a <div> and <a>', () => { | ||
expect(render(<Header />).type).toEqual('header') | ||
expect(render(<Header.Link />).type).toEqual('a') | ||
it('renders a <header> and <a>', () => { | ||
const {container: headerContainer} = render(<Header />) | ||
const {container: linkContainer} = render(<Header.Link />) | ||
expect((headerContainer.firstChild as Element).tagName).toEqual('HEADER') | ||
expect((linkContainer.firstChild as Element).tagName).toEqual('A') | ||
}) | ||
|
||
it('sets aria-label appropriately', () => { | ||
expect(render(<Header aria-label="Test label" />).props['aria-label']).toEqual('Test label') | ||
const {container} = render(<Header aria-label="Test label" />) | ||
expect(container.firstChild).toHaveAttribute('aria-label', 'Test label') | ||
}) | ||
|
||
it('should support `className` on the outermost element', () => { | ||
const Element = () => <Header className={'test-class-name'} /> | ||
expect(HTMLRender(<Element />).container.firstChild).toHaveClass('test-class-name') | ||
expect(render(<Element />).container.firstChild).toHaveClass('test-class-name') | ||
}) | ||
}) |
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,54 +1,48 @@ | ||||||
import Link from '..' | ||||||
import {render, behavesAsComponent, checkExports} from '../../utils/testing' | ||||||
import {render as HTMLRender} from '@testing-library/react' | ||||||
import axe from 'axe-core' | ||||||
import {describe, expect, it, vi} from 'vitest' | ||||||
import {render} from '@testing-library/react' | ||||||
import Link from '../Link' | ||||||
|
||||||
describe('Link', () => { | ||||||
behavesAsComponent({Component: Link}) | ||||||
|
||||||
checkExports('Link', { | ||||||
default: Link, | ||||||
}) | ||||||
|
||||||
it('should support `className` on the outermost element', () => { | ||||||
const Element = () => <Link href="#" className={'test-class-name'} /> | ||||||
expect(HTMLRender(<Element />).container.firstChild).toHaveClass('test-class-name') | ||||||
}) | ||||||
|
||||||
it('should have no axe violations', async () => { | ||||||
const {container} = HTMLRender(<Link href="www.github.com">GitHub</Link>) | ||||||
const results = await axe.run(container) | ||||||
expect(results).toHaveNoViolations() | ||||||
expect(render(<Element />).container.firstChild).toHaveClass('test-class-name') | ||||||
}) | ||||||
|
||||||
it('passes href down to link element', () => { | ||||||
expect(render(<Link href="https://github.com" />)).toMatchSnapshot() | ||||||
const {container} = render(<Link href="https://github.com" />) | ||||||
expect(container.firstChild).toHaveAttribute('href', 'https://github.com') | ||||||
}) | ||||||
|
||||||
it('respects hoverColor prop', () => { | ||||||
expect(render(<Link hoverColor="accent.fg" />)).toMatchSnapshot() | ||||||
const {container} = render(<Link hoverColor="accent.fg" />) | ||||||
expect(container.firstChild).toHaveStyle('--fgColor-accent: #0969da') | ||||||
}) | ||||||
|
||||||
it('respects the "sx" prop', () => { | ||||||
expect(render(<Link sx={{fontStyle: 'italic'}} />)).toHaveStyleRule('font-style', 'italic') | ||||||
const {container} = render(<Link sx={{fontStyle: 'italic'}} />) | ||||||
expect(container.firstChild).toHaveStyle('font-style: italic') | ||||||
}) | ||||||
|
||||||
it('applies button styles when rendering a button element', () => { | ||||||
expect(render(<Link as="button" />)).toMatchSnapshot() | ||||||
const {container} = render(<Link as="button" />) | ||||||
expect((container.firstChild as Element).tagName).toBe('BUTTON') | ||||||
}) | ||||||
|
||||||
it('respects the "muted" prop', () => { | ||||||
expect(render(<Link muted />)).toMatchSnapshot() | ||||||
const {container} = render(<Link muted />) | ||||||
expect(container.firstChild).toHaveAttribute('data-muted', 'true') | ||||||
}) | ||||||
|
||||||
it('respects the "sx" prop when "muted" prop is also passed', () => { | ||||||
expect(render(<Link muted sx={{color: 'fg.onEmphasis'}} />)).toMatchSnapshot() | ||||||
it('respects the "sx" prop when "muted" prop is also passed', () => { | ||||||
const {container} = render(<Link muted sx={{color: 'fg.onEmphasis'}} />) | ||||||
expect(container.firstChild).toHaveAttribute('data-muted', 'true') | ||||||
expect(container.firstChild).toHaveStyle('color: rgb(89, 99, 110)') | ||||||
}) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The hardcoded RGB color value 'rgb(89, 99, 110)' makes this test brittle to design token changes. Consider using a more flexible assertion that doesn't rely on exact color values.
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
|
||||||
it('logs a warning when trying to render invalid "as" prop', () => { | ||||||
const consoleSpy = jest.spyOn(global.console, 'error').mockImplementation() | ||||||
const consoleSpy = vi.spyOn(globalThis.console, 'error').mockImplementation(() => {}) | ||||||
|
||||||
HTMLRender(<Link as="i" />) | ||||||
render(<Link as="i" />) | ||||||
expect(consoleSpy).toHaveBeenCalled() | ||||||
|
||||||
consoleSpy.mockRestore() | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The hardcoded color value '#0969da' makes this test brittle to design token changes. Consider using a more flexible assertion or checking for the CSS custom property presence instead of its exact value.
Copilot uses AI. Check for mistakes.