Skip to content

Commit 33fb43f

Browse files
committed
3.0.0-dev.0
* Adding unit tests * Removing eslint cypress config
1 parent e6af5df commit 33fb43f

17 files changed

+310
-18
lines changed

.eslintrc.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,11 @@ module.exports = {
44
node: true,
55
jest: true,
66
es6: true,
7-
'cypress/globals': true,
87
},
9-
plugins: ['cypress'],
108
extends: [
119
'@djthoms/eslint-config',
1210
'@djthoms/eslint-config/react',
1311
'@djthoms/eslint-config/typescript',
14-
'plugin:cypress/recommended',
1512
],
1613
rules: {
1714
'@typescript-eslint/explicit-function-return-type': 0,

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"scripts": {
2121
"lint": "eslint \"src/**/*.{ts,tsx}\"",
22-
"test": "jest --silent",
22+
"test": "jest",
2323
"clean": "rimraf pkg/**",
2424
"build": "npm-run-all --parallel ts-check lint clean test && pika build",
2525
"ts-check": "tsc --noEmit",

src/components/checkbox/useCheckboxState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export type UseCheckboxState = {
55
onChange?: React.InputHTMLAttributes<HTMLInputElement>['onChange'];
66
};
77

8-
const dispatch = (value: any) => (state: UseCheckboxState['state']) => {
8+
const dispatch = (value: string) => (state: UseCheckboxState['state']) => {
99
if (Array.isArray(state)) {
1010
const index = state.indexOf(value);
1111

src/components/state/State.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ import classNames from 'classnames';
33
import { PCRCheckboxRadioProps } from '../../typings/PCRCheckboxRadioProps';
44

55
type StateProps = React.HTMLAttributes<HTMLDivElement> & {
6-
color: PCRCheckboxRadioProps['color'];
6+
color?: PCRCheckboxRadioProps['color'];
77
icon?: React.ReactNode;
88
};
99

1010
/**
1111
* A tiny component to abstract away pretty-checkbox "state" div.
1212
* Shared by all components.
1313
*/
14-
export const State = ({ color, icon, id, children }: StateProps) => {
14+
export const State = ({ color, icon, id, children, ...rest }: StateProps) => {
1515
return (
16-
<div className={classNames('state', color && `p-${color}`)}>
16+
<div className={classNames('state', color && `p-${color}`)} {...rest}>
1717
{icon}
1818
<label htmlFor={id}>{children}</label>
1919
</div>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import * as React from 'react';
2+
import { render } from '@testing-library/react';
3+
4+
import { State } from '../State';
5+
6+
describe('State tests', () => {
7+
it('should render without errors', () => {
8+
expect(() => {
9+
render(<State>Hello</State>);
10+
}).not.toThrow();
11+
});
12+
13+
it('should render the label', () => {
14+
const { queryByText } = render(<State>Hello</State>);
15+
expect(queryByText('Hello')).not.toBeNull();
16+
});
17+
18+
it('should add colors', () => {
19+
const { getByTestId } = render(
20+
<State data-testid="test" color="danger">
21+
Danger!
22+
</State>
23+
);
24+
25+
expect(getByTestId('test').className.includes('danger')).toBe(true);
26+
});
27+
});

src/hooks/__tests__/useIcon.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import * as React from 'react';
2+
import { renderHook } from '@testing-library/react-hooks';
3+
import { useIcon } from '../useIcon';
4+
5+
describe('useIcon hook tests', () => {
6+
it('should return an object when icon is not defined', () => {
7+
const { result } = renderHook(() => useIcon(undefined));
8+
9+
expect(result.current).toMatchObject({});
10+
});
11+
12+
it('should return an icon with a custom classname and type', () => {
13+
const { result } = renderHook(() =>
14+
useIcon(
15+
React.createElement('i', { className: 'far far-star' }) as React.ReactElement<
16+
any,
17+
any
18+
>
19+
)
20+
);
21+
22+
expect(result.current).toMatchObject({ iconType: 'icon' });
23+
24+
// @ts-ignore
25+
expect(result.current.icon.props.className).toContain('icon');
26+
});
27+
28+
it('should work with svg elements', () => {
29+
const { result } = renderHook(() =>
30+
useIcon(
31+
React.createElement('svg', { className: 'far far-star' }) as React.ReactElement<
32+
any,
33+
any
34+
>
35+
)
36+
);
37+
38+
expect(result.current).toMatchObject({ iconType: 'svg' });
39+
});
40+
41+
it('should work with img elements', () => {
42+
const { result } = renderHook(() =>
43+
useIcon(
44+
React.createElement('img', { className: 'far far-star' }) as React.ReactElement<
45+
any,
46+
any
47+
>
48+
)
49+
);
50+
51+
expect(result.current).toMatchObject({ iconType: 'image' });
52+
});
53+
});

src/hooks/__tests__/useUUID.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { nanoid } from 'nanoid';
3+
import { useUUID } from '../useUUID';
4+
5+
jest.mock('nanoid');
6+
7+
// @ts-ignore
8+
nanoid.mockImplementation(() => '1234');
9+
10+
afterAll(() => {
11+
// @ts-ignore
12+
nanoid.mockRestore();
13+
});
14+
15+
describe('useUUID tests', () => {
16+
it('should generate a UUID', () => {
17+
const { result } = renderHook(() => useUUID());
18+
expect(result.current).toEqual('pcr_1234');
19+
});
20+
});

src/hooks/useUUID.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,4 @@ import * as React from 'react';
22
import { nanoid } from 'nanoid';
33

44
const PREFIX = 'pcr_';
5-
6-
export const useUUID = (id?: string) => {
7-
const uuid = React.useRef(id || PREFIX + nanoid(8));
8-
9-
return uuid.current;
10-
};
5+
export const useUUID = () => React.useRef(PREFIX + nanoid(8)).current;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { renderHook } from '@testing-library/react-hooks';
2+
import { useCheckboxRadioProps } from '../useCheckboxRadioProps';
3+
4+
describe('useCheckboxRadioProps tests', () => {
5+
it('should remove checkbox and radio props and filter out common props', () => {
6+
const { result } = renderHook(() =>
7+
useCheckboxRadioProps({
8+
shape: 'curve',
9+
})
10+
);
11+
12+
expect(result.current).toMatchObject({
13+
shape: 'curve',
14+
htmlProps: expect.any(Object),
15+
});
16+
});
17+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { useClassNames } from '../useClassNames';
2+
3+
describe('useClassNames tests', () => {
4+
it('should return default styles', () => {
5+
expect(useClassNames({})).toMatchObject({
6+
'p-default': true,
7+
'p-bigger': undefined,
8+
});
9+
});
10+
11+
it('should not be a default when animation tada, rotate, or jelly', () => {
12+
const trials = {
13+
tada: false,
14+
rotate: false,
15+
jelly: false,
16+
pulse: true,
17+
smooth: true,
18+
'': true,
19+
};
20+
21+
Object.keys(trials).forEach(key => {
22+
// @ts-ignore
23+
expect(useClassNames({ animation: key })).toMatchObject({
24+
// @ts-ignore
25+
'p-default': trials[key],
26+
});
27+
});
28+
});
29+
30+
it('should not be default when switch is defined', () => {
31+
expect(useClassNames({}, true)).toMatchObject({
32+
'p-default': false,
33+
});
34+
});
35+
36+
it('should not be default when an icon is used', () => {
37+
expect(useClassNames({ iconType: 'svg' })).toMatchObject({
38+
'p-default': false,
39+
});
40+
});
41+
42+
it('should return an expected object', () => {
43+
expect(
44+
useClassNames({
45+
animation: 'pulse',
46+
shape: 'round',
47+
variant: 'thick',
48+
})
49+
).toMatchInlineSnapshot(`
50+
Object {
51+
"p-bigger": undefined,
52+
"p-default": true,
53+
"p-locked": undefined,
54+
"p-plain": undefined,
55+
"p-pulse": "pulse",
56+
"p-round": "round",
57+
"p-thick": "thick",
58+
"p-undefined": undefined,
59+
}
60+
`);
61+
});
62+
});

0 commit comments

Comments
 (0)