src/js/components/Cards/__tests__/Cards-test.tsx
import React from 'react';
import 'jest-styled-components';
import 'jest-axe/extend-expect';
import 'regenerator-runtime/runtime';
import '@testing-library/jest-dom';
import { render } from '@testing-library/react';
import { axe } from 'jest-axe';
import { Grommet } from '../../Grommet';
import { Cards } from '..';
import { Card } from '../../Card';
import { CardHeader } from '../../CardHeader';
const data: string[] = [];
for (let i = 0; i < 95; i += 1) {
data.push(`entry-${i}`);
}
describe('Cards', () => {
test('should have no accessibility violations', async () => {
const { container } = render(
<Grommet>
<Cards aria-label="Cards" data={[{ a: 'alpha' }, { a: 'beta' }]} />
</Grommet>,
);
const results = await axe(container);
expect(results).toHaveNoViolations();
});
test('renders a11yTitle and aria-label', () => {
const { container, getByLabelText } = render(
<Grommet>
<Cards a11yTitle="test" data={[{ a: 'alpha' }, { a: 'beta' }]} />
<Cards aria-label="test-2" data={[{ a: 'alpha' }, { a: 'beta' }]} />
</Grommet>,
);
expect(getByLabelText('test')).toBeTruthy();
expect(getByLabelText('test-2')).toBeTruthy();
expect(container).toMatchSnapshot();
});
test('empty', () => {
const { container } = render(
<Grommet>
<Cards />
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});
test('data strings', () => {
const { container } = render(
<Grommet>
<Cards data={['one', 'two']} />
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});
test('data objects', () => {
const { container } = render(
<Grommet>
<Cards
data={[
{ a: 'one', b: 1 },
{ a: 'two', b: 2 },
]}
/>
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});
test('children render', () => {
const { container } = render(
<Grommet>
<Cards data={['one', 'two']}>
{(item, index) => (
<Card key={item}>
<CardHeader>
`${item} - ${index}`
</CardHeader>
</Card>
)}
</Cards>
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});
test('margin string', () => {
const { container } = render(
<Grommet>
<Cards data={['one', 'two']} margin="large" />
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});
test('margin object', () => {
const { container } = render(
<Grommet>
<Cards data={['one', 'two']} margin={{ horizontal: 'large' }} />
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});
test('pad string', () => {
const { container } = render(
<Grommet>
<Cards data={['one', 'two']} pad="large" />
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});
test('pad object', () => {
const { container } = render(
<Grommet>
<Cards data={['one', 'two']} pad={{ horizontal: 'large' }} />
</Grommet>,
);
expect(container.firstChild).toMatchSnapshot();
});
});