Skip to content

Commit 8cca13a

Browse files
Daniel Yefetdanielyefet
Daniel Yefet
authored andcommitted
Add unit tests
1 parent c43b9be commit 8cca13a

File tree

7 files changed

+3547
-379
lines changed

7 files changed

+3547
-379
lines changed

.babelrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["@babel/preset-react"]
2+
"presets": ["@babel/preset-env", "@babel/preset-react"]
33
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# react-input-auto-format
22

3-
A super simple input component that formats as you type.
3+
React Input Auto Format (catchy, I know) automatically formats the value of an input field while you're typing in it.
44

55
```HTML
66
<Input format="## - ## - ##">

jest.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
testEnvironment: 'jsdom',
3+
};

package.json

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,26 @@
1818
],
1919
"devDependencies": {
2020
"@babel/core": "^7.15.5",
21+
"@babel/preset-env": "^7.15.8",
2122
"@babel/preset-react": "^7.14.5",
2223
"@rollup/plugin-babel": "^5.3.0",
24+
"@testing-library/dom": "^8.7.2",
25+
"@testing-library/react": "^12.1.2",
26+
"@testing-library/user-event": "^13.2.1",
27+
"babel-jest": "^27.2.5",
28+
"jest": "^27.2.5",
29+
"jest-dom": "^4.0.0",
2330
"react": "^17.0.2",
2431
"react-dom": "^17.0.2",
2532
"rollup": "^2.58.0"
2633
},
2734
"peerDependencies": {
28-
"react": "^17.8.0",
29-
"react-dom": "^17.8.0"
35+
"react": "^17.0.2",
36+
"react-dom": "^17.0.2"
3037
},
3138
"scripts": {
3239
"build": "rollup -c",
33-
"start": "rollup -c -w"
40+
"start": "rollup -c -w",
41+
"test": "jest --no-watchman"
3442
}
3543
}

src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@ import React, { useEffect, useRef, useState } from 'react';
22

33
function noop() {}
44

5-
function format(value, pattern, placeholder = '#') {
5+
function format(value, pattern) {
66
if (!pattern) return value;
77

8+
const placeholder = '#';
9+
810
let endOfValue = 0;
911
let characterIndex = 0;
1012
let newValue = value;
@@ -46,7 +48,7 @@ function Input({
4648
value: userValue = '',
4749
...rest
4850
}) {
49-
const [value, setValue] = useState(userValue);
51+
const [value, setValue] = useState(format(userValue, pattern));
5052
const inputRef = useRef();
5153
const infoRef = useRef({});
5254

src/index.spec.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import React from 'react';
2+
import { render } from '@testing-library/react';
3+
import userEvent from '@testing-library/user-event';
4+
5+
import Input from '.';
6+
7+
describe('The Input component', () => {
8+
function getInput(props) {
9+
return render(<Input type="text" {...props} />).getByRole('textbox');
10+
}
11+
12+
it('should render', () => {
13+
expect(getInput().value).toBe('');
14+
});
15+
16+
it('should accept a default value', () => {
17+
expect(getInput({ value: 'foo' }).value).toBe('foo');
18+
});
19+
20+
it('should format the default value', () => {
21+
expect(getInput({ value: 'foo', format: '# - # - #' }).value).toBe(
22+
'f - o - o'
23+
);
24+
});
25+
26+
it('should format while typing', () => {
27+
const input = getInput({ format: '## - ## - ##' });
28+
29+
userEvent.type(input, '12');
30+
31+
expect(input.value).toBe('12 - ');
32+
});
33+
34+
it('should broadcast the raw value', () => {
35+
const handleValueChange = jest.fn();
36+
37+
const input = getInput({
38+
format: '# - # - #',
39+
onValueChange: handleValueChange,
40+
});
41+
42+
userEvent.type(input, '123');
43+
44+
expect(handleValueChange).toBeCalledWith('123');
45+
});
46+
47+
it.each`
48+
format | type | moveCursorTo | typeAgain | cursorPosition
49+
${'## - ##'} | ${'12'} | ${null} | ${null} | ${5}
50+
${'## - ## - ##'} | ${'12345'} | ${3} | ${'1'} | ${6}
51+
${'## - ## - ##'} | ${'123456'} | ${null} | ${'{backspace}'} | ${11}
52+
${'## - ## - ##'} | ${'123456'} | ${7} | ${'{backspace}'} | ${6}
53+
${'## - ## - ##'} | ${'123456'} | ${4} | ${'{backspace}'} | ${1}
54+
`(
55+
'should position the cursor correctly',
56+
({ format, type, typeAgain, moveCursorTo, cursorPosition }) => {
57+
const input = getInput({ format });
58+
59+
userEvent.type(input, type);
60+
61+
if (moveCursorTo) input.setSelectionRange(moveCursorTo, moveCursorTo);
62+
if (typeAgain) userEvent.type(input, typeAgain);
63+
64+
expect(input.selectionStart).toBe(cursorPosition);
65+
}
66+
);
67+
});

0 commit comments

Comments
 (0)