Skip to content

Commit 0228213

Browse files
committed
test(tests): remove console errors in tests, cleanup
1 parent 1c31744 commit 0228213

File tree

15 files changed

+176
-104
lines changed

15 files changed

+176
-104
lines changed

src/components/ColorInput/ColorInput.spec.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
2+
import { fireEvent } from '@testing-library/react';
33
import { renderWithTheme } from '../../../test/utils';
44
import ColorInput from './ColorInput';
55

@@ -12,8 +12,14 @@ function rgb2hex(str) {
1212
}
1313

1414
describe('<ColorInput />', () => {
15-
// TODO
16-
it('should call handlers', () => {});
15+
it('should call handlers', () => {
16+
const color = '#f0f0dd';
17+
const onChange = jest.fn();
18+
const { container } = renderWithTheme(<ColorInput onChange={onChange} />);
19+
const input = container.querySelector(`[type="color"]`);
20+
fireEvent.change(input, { target: { value: color } });
21+
expect(onChange).toBeCalledTimes(1);
22+
});
1723

1824
it('should properly pass value to input element', () => {
1925
const color = '#f0f0dd';

src/components/Hourglass/Hourglass.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ const Hourglass = React.forwardRef(function HourGlass(props, ref) {
2121
<StyledContainer
2222
style={{
2323
...style,
24-
width: size || '30px',
25-
height: size || '30px'
24+
width: size,
25+
height: size
2626
}}
2727
ref={ref}
2828
{...otherProps}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
4+
import Hourglass from './Hourglass';
5+
6+
describe('<Hourglass />', () => {
7+
it('should render hourglass', () => {
8+
const { container } = render(<Hourglass />);
9+
const barEl = container.firstChild;
10+
11+
expect(barEl).toBeInTheDocument();
12+
});
13+
14+
it('should render correct size', () => {
15+
const { container } = render(<Hourglass size='66px' />);
16+
const hourglass = container.firstChild;
17+
18+
const computedStyles = window.getComputedStyle(hourglass);
19+
expect(computedStyles.width).toBe('66px');
20+
expect(computedStyles.height).toBe('66px');
21+
});
22+
23+
it('should handle custom props', () => {
24+
const customProps = { alt: 'hourglass' };
25+
const { container } = render(<Hourglass {...customProps} />);
26+
const hourglass = container.firstChild;
27+
28+
expect(hourglass).toHaveAttribute('alt', 'hourglass');
29+
});
30+
});

src/components/Radio/Radio.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Radio.defaultProps = {
146146
onChange: undefined,
147147
name: null,
148148
value: undefined,
149-
checked: false,
149+
checked: undefined,
150150
label: '',
151151
disabled: false,
152152
variant: 'default',

src/components/Radio/Radio.spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,7 @@ describe('<Radio />', () => {
2929
it('should disable radio', () => {
3030
const handleChange = jest.fn();
3131

32-
const { getByRole } = renderWithTheme(
33-
<Radio disabled onChange={handleChange} />
34-
);
32+
const { getByRole } = renderWithTheme(<Radio disabled />);
3533
const checkbox = getByRole('radio');
3634
expect(checkbox).toHaveAttribute('disabled');
3735

@@ -49,10 +47,10 @@ describe('<Radio />', () => {
4947
describe('controlled', () => {
5048
it('should check the radio', () => {
5149
const { getByRole, rerender } = renderWithTheme(
52-
<Radio checked={false} />
50+
<Radio checked={false} readOnly />
5351
);
5452

55-
rerender(<Radio checked />);
53+
rerender(<Radio checked readOnly />);
5654
const checkbox = getByRole('radio');
5755

5856
expect(checkbox.checked).toBe(true);
@@ -64,8 +62,10 @@ describe('<Radio />', () => {
6462
});
6563

6664
it('should uncheck the checkbox', () => {
67-
const { getByRole, rerender } = renderWithTheme(<Radio checked />);
68-
rerender(<Radio checked={false} />);
65+
const { getByRole, rerender } = renderWithTheme(
66+
<Radio checked readOnly />
67+
);
68+
rerender(<Radio checked={false} readOnly />);
6969
const checkbox = getByRole('radio');
7070

7171
expect(checkbox.checked).toBe(false);

src/components/Slider/Slider.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import { clamp, percentToValue, roundValueToStep } from '../common/utils';
1414
import useControlledOrUncontrolled from '../common/hooks/useControlledOrUncontrolled';
1515
import useForkRef from '../common/hooks/useForkRef';
16-
import { useIsFocusVisible } from '../common/hooks/focusVisible';
16+
import { useIsFocusVisible } from '../common/hooks/useIsFocusVisible';
1717
import Cutout from '../Cutout/Cutout';
1818

1919
function trackFinger(event, touchId) {

src/components/Table/Table.spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,11 @@ describe('<Table />', () => {
1717
expect(getByRole('table')).toBeInTheDocument();
1818
});
1919
it('renders children', () => {
20-
const textContent = 'Hi there!';
21-
const { getByText } = renderWithTheme(
20+
const { getByTestId } = renderWithTheme(
2221
<Table>
23-
<span>{textContent}</span>
22+
<tbody data-testid='children' />
2423
</Table>
2524
);
26-
expect(getByText(textContent)).toBeInTheDocument();
25+
expect(getByTestId('children')).toBeInTheDocument();
2726
});
2827
});

src/components/TableBody/TableBody.spec.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import { renderWithTheme } from '../../../test/utils';
55
import TableBody from './TableBody';
66

77
describe('<TableBody />', () => {
8+
function mountInTable(node) {
9+
const { container, getByTestId } = renderWithTheme(<table>{node}</table>);
10+
return {
11+
tbody: container.querySelector('table').firstChild,
12+
getByTestId
13+
};
14+
}
15+
816
it('renders TableBody', () => {
9-
const { container } = renderWithTheme(<TableBody />);
10-
const list = container.firstChild;
17+
const { tbody } = mountInTable(<TableBody />);
1118

12-
expect(list).toBeInTheDocument();
19+
expect(tbody).toBeInTheDocument();
20+
expect(tbody.tagName).toBe('TBODY');
1321
});
14-
it('renders tbody element', () => {
15-
const { container } = renderWithTheme(<TableBody />);
1622

17-
expect(container.querySelector('tbody')).toBeInTheDocument();
18-
});
1923
it('renders children', () => {
20-
const textContent = 'Hi there!';
21-
const { getByText } = renderWithTheme(
22-
<TableBody>
23-
<span>{textContent}</span>
24-
</TableBody>
25-
);
26-
expect(getByText(textContent)).toBeInTheDocument();
24+
const children = <tr data-testid='tr' />;
25+
const { getByTestId } = mountInTable(<TableBody>{children}</TableBody>);
26+
expect(getByTestId('tr')).toBeInTheDocument();
2727
});
2828
});

src/components/TableDataCell/TableDataCell.spec.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,27 @@ import { renderWithTheme } from '../../../test/utils';
55
import TableDataCell from './TableDataCell';
66

77
describe('<TableDataCell />', () => {
8-
it('renders TableDataCell', () => {
9-
const { container } = renderWithTheme(<TableDataCell />);
10-
const list = container.firstChild;
8+
function mountInTable(node) {
9+
const { container, getByText } = renderWithTheme(
10+
<table>
11+
<tbody>
12+
<tr>{node}</tr>
13+
</tbody>
14+
</table>
15+
);
16+
return {
17+
td: container.querySelector('tr').firstChild,
18+
getByText
19+
};
20+
}
1121

12-
expect(list).toBeInTheDocument();
22+
it('renders TableDataCell', () => {
23+
const { td } = mountInTable(<TableDataCell />);
24+
expect(td.tagName).toBe('TD');
1325
});
14-
it('renders td element', () => {
15-
const { container } = renderWithTheme(<TableDataCell />);
1626

17-
expect(container.querySelector('td')).toBeInTheDocument();
18-
});
1927
it('renders children', () => {
20-
const textContent = 'Hi there!';
21-
const { getByText } = renderWithTheme(
22-
<TableDataCell>
23-
<span>{textContent}</span>
24-
</TableDataCell>
25-
);
26-
expect(getByText(textContent)).toBeInTheDocument();
28+
const { getByText } = mountInTable(<TableDataCell>children</TableDataCell>);
29+
expect(getByText('children')).toBeInTheDocument();
2730
});
2831
});

src/components/TableHead/TableHead.spec.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@ import { renderWithTheme } from '../../../test/utils';
55
import TableHead from './TableHead';
66

77
describe('<TableHead />', () => {
8+
function mountInTable(node) {
9+
const { container, getByTestId } = renderWithTheme(<table>{node}</table>);
10+
return {
11+
tbody: container.querySelector('table').firstChild,
12+
getByTestId
13+
};
14+
}
15+
816
it('renders TableHead', () => {
9-
const { container } = renderWithTheme(<TableHead />);
10-
const list = container.firstChild;
17+
const { tbody } = mountInTable(<TableHead />);
1118

12-
expect(list).toBeInTheDocument();
19+
expect(tbody).toBeInTheDocument();
20+
expect(tbody.tagName).toBe('THEAD');
1321
});
14-
it('renders thead element', () => {
15-
const { container } = renderWithTheme(<TableHead />);
1622

17-
expect(container.querySelector('thead')).toBeInTheDocument();
18-
});
1923
it('renders children', () => {
20-
const textContent = 'Hi there!';
21-
const { getByText } = renderWithTheme(
22-
<TableHead>
23-
<span>{textContent}</span>
24-
</TableHead>
25-
);
26-
expect(getByText(textContent)).toBeInTheDocument();
24+
const children = <tr data-testid='tr' />;
25+
const { getByTestId } = mountInTable(<TableHead>{children}</TableHead>);
26+
expect(getByTestId('tr')).toBeInTheDocument();
2727
});
2828
});

0 commit comments

Comments
 (0)