Skip to content

Commit

Permalink
test: refactor card, container, col, and row component tests to types…
Browse files Browse the repository at this point in the history
…cript and eliminate enzyme (#6166)

* Refactor Card component test to eliminate enzyme

Signed-off-by: Jay Clark <jay@jayeclark.dev>

* Fix CardSpec issues

Signed-off-by: Jay Clark <jay@jayeclark.dev>

* Migrate Container, Col, and Row tests

Signed-off-by: Jay Clark <jay@jayeclark.dev>
  • Loading branch information
jayeclark committed Jan 4, 2022
1 parent 02ec71c commit b1ec614
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 172 deletions.
50 changes: 0 additions & 50 deletions test/CardSpec.js

This file was deleted.

58 changes: 58 additions & 0 deletions test/CardSpec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { render } from '@testing-library/react';

import Card from '../src/Card';

describe('<Card>', () => {
it('should output a div', () => {
const { getByText } = render(<Card>Card</Card>);
getByText('Card').tagName.toLowerCase().should.equal('div');
getByText('Card').classList.contains('card').should.be.true;
});

it('should have additional classes', () => {
const { getByText } = render(<Card className="custom-class">Card</Card>);
getByText('Card').classList.contains('custom-class').should.be.true;
});

it('accepts a bg prop', () => {
const { getByText } = render(<Card bg="primary">Card</Card>);
getByText('Card').classList.contains('bg-primary').should.be.true;
});

it('accepts a text prop', () => {
const { getByText } = render(<Card text="success">Card</Card>);
getByText('Card').classList.contains('text-success').should.be.true;
});

it('accepts a border prop', () => {
const { getByText } = render(<Card border="danger">Card</Card>);
getByText('Card').classList.contains('border-danger').should.be.true;
});

it('should render children', () => {
const { getByTestId } = render(
<Card data-testid="test-card">
<p>hello</p>
</Card>,
);
getByTestId('test-card').children.length.should.equal(1);
getByTestId('test-card')
.children[0].tagName.toLowerCase()
.should.equal('p');
});

it('accepts as prop', () => {
const { getByText } = render(<Card as="section">body</Card>);
getByText('body').tagName.toLowerCase().should.equal('section');
});

it('allows for the body shorthand', () => {
const { getByText } = render(<Card body>test</Card>);
getByText('test').classList.contains('card-body').should.be.true;
});

it('Should have div as default component', () => {
const { getByTestId } = render(<Card data-testid="default-test" />);
getByTestId('default-test').tagName.toLowerCase().should.equal('div');
});
});
52 changes: 0 additions & 52 deletions test/ColSpec.js

This file was deleted.

86 changes: 86 additions & 0 deletions test/ColSpec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { render } from '@testing-library/react';

import Col from '../src/Col';

describe('Col', () => {
it('Should include "col" when there are no sizes', () => {
const { getByText } = render(<Col>Column</Col>);
getByText('Column').classList.contains('col').should.be.true;
});

it('Should include "col" when xs is true', () => {
const { getByText } = render(<Col xs>Column</Col>);
getByText('Column').classList.contains('col').should.be.true;

render(<Col xs={{ span: true }}>Column2</Col>);
getByText('Column2').classList.contains('col').should.be.true;
});

it('Should include sizes', () => {
const { getByText } = render(
<Col xs={4} md={8} lg={{ span: 12 }}>
Column
</Col>,
);
getByText('Column').classList.length.should.equal(3);
getByText('Column').classList.contains('col-4').should.be.true;
getByText('Column').classList.contains('col-md-8').should.be.true;
getByText('Column').classList.contains('col-lg-12').should.be.true;
});

it('Should include offsets', () => {
const { getByText } = render(
<Col
xs={{ span: 4, offset: 1 }}
md={{ span: 8, order: 1 }}
lg={{ order: 'last' }}
>
Column
</Col>,
);
getByText('Column').classList.length.should.equal(5);
getByText('Column').classList.contains('col-md-8').should.be.true;
getByText('Column').classList.contains('order-md-1').should.be.true;
getByText('Column').classList.contains('col-4').should.be.true;
getByText('Column').classList.contains('offset-1').should.be.true;
getByText('Column').classList.contains('order-lg-last').should.be.true;
});

it('Should allow span to be null', () => {
const { getByText } = render(
// @ts-ignore
<Col xs="6" md={{ span: null, order: 1 }}>
Column
</Col>,
);
getByText('Column').classList.contains('col-6').should.be.true;
getByText('Column').classList.contains('order-md-1').should.be.true;
getByText('Column').classList.contains('col-md').should.equal(false);
});

it('Should allow span to be false', () => {
const { getByText } = render(
<Col xs="6" md={{ span: false, order: 1 }}>
Column
</Col>,
);
getByText('Column').classList.contains('col-6').should.be.true;
getByText('Column').classList.contains('order-md-1').should.be.true;
getByText('Column').classList.contains('col-md').should.equal(false);
});

it('Should allow span to be auto', () => {
const { getByText } = render(
<Col md="auto" lg={{ span: 'auto' }}>
Column
</Col>,
);
getByText('Column').classList.contains('col-md-auto').should.be.true;
getByText('Column').classList.contains('col-lg-auto').should.be.true;
});

it('Should have div as default component', () => {
const { getByText } = render(<Col>Column</Col>);
getByText('Column').tagName.toLowerCase().should.equal('div');
});
});
27 changes: 0 additions & 27 deletions test/ContainerSpec.js

This file was deleted.

33 changes: 33 additions & 0 deletions test/ContainerSpec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { render } from '@testing-library/react';

import Container from '../src/Container';

describe('<Container>', () => {
it('should render props correctly', () => {
const { getByText } = render(
<Container className="whatever">Container</Container>,
);
getByText('Container').classList.contains('whatever').should.be.true;
});

it('turns grid into "full-width" layout via "fluid" property set', () => {
const { getByText } = render(<Container fluid>Container</Container>);
getByText('Container').classList.contains('container-fluid').should.be.true;
});

it('Should include size breakpoint class when fluid is set to sm, md, lg or xl', () => {
const { getByText } = render(<Container fluid="sm">Container</Container>);
getByText('Container').classList.contains('container-sm').should.be.true;
});

it('allows custom elements instead of "div"', () => {
const { getByText } = render(<Container as="section">Container</Container>);
getByText('Container').classList.contains('container').should.be.true;
getByText('Container').tagName.toLowerCase().should.equal('section');
});

it('Should have div as default component', () => {
const { getByText } = render(<Container>Container</Container>);
getByText('Container').tagName.toLowerCase().should.equal('div');
});
});
43 changes: 0 additions & 43 deletions test/RowSpec.js

This file was deleted.

Loading

0 comments on commit b1ec614

Please sign in to comment.