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: switch description list tests to react testing library #1363

Merged
merged 5 commits into from
Feb 28, 2024
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

This file was deleted.

84 changes: 84 additions & 0 deletions packages/react/src/components/DescriptionList/index.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import {
DescriptionList,
DescriptionListItem,
DescriptionTerm,
DescriptionDetails
} from './';
import axe from '../../axe';

describe('DescriptionList components', () => {
test('renders a dl element', () => {
render(<DescriptionList data-testid="dl">a</DescriptionList>);
expect(screen.getByTestId('dl').tagName).toBe('DL');
expect(screen.getByTestId('dl')).toBeInTheDocument();
});

test('renders a dt element', () => {
render(<DescriptionTerm>a</DescriptionTerm>);
expect(screen.getByRole('term')).toBeInTheDocument();
});

test('renders a dd element', () => {
render(<DescriptionDetails>a</DescriptionDetails>);
expect(screen.getByRole('definition')).toBeInTheDocument();
});

test('handles uncollapsed prop', () => {
render(<DescriptionList data-testid="dl">a</DescriptionList>);
expect(screen.getByTestId('dl')).not.toHaveClass(
'DescriptionList--collapsed'
);
});

test('handles collapsed prop', () => {
render(
<DescriptionList data-testid="dl" collapsed>
a
</DescriptionList>
);
expect(screen.getByTestId('dl')).toHaveClass('DescriptionList--collapsed');
});

test('passes classNames through', () => {
render(<DescriptionList className="a">a</DescriptionList>);
expect(screen.getByText(/a/i)).toHaveClass('a');

render(<DescriptionTerm className="b">b</DescriptionTerm>);
expect(screen.getByText(/b/i)).toHaveClass('b');

render(<DescriptionDetails className="c">c</DescriptionDetails>);
expect(screen.getByText(/c/i)).toHaveClass('c');
});

test('passes props through', () => {
render(
<DescriptionList data-testid="dl" data-foo="list">
a
</DescriptionList>
);
render(<DescriptionTerm data-foo="term">a</DescriptionTerm>);
render(<DescriptionDetails data-foo="detail">a</DescriptionDetails>);

expect(screen.getByTestId('dl')).toHaveAttribute('data-foo', 'list');
expect(screen.getByRole('term')).toHaveAttribute('data-foo', 'term');
expect(screen.getByRole('definition')).toHaveAttribute(
'data-foo',
'detail'
);
});

test('returns no axe violations', async () => {
const { container } = render(
<DescriptionList>
<DescriptionListItem>
<DescriptionTerm>Term</DescriptionTerm>
<DescriptionDetails>details</DescriptionDetails>
</DescriptionListItem>
</DescriptionList>
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
});
Loading