Skip to content

Commit e374712

Browse files
committed
feat: add testing-library plugin and configure for test files
- add @testing-library/react-native and eslint-plugin-testing-library as dev dependencies - configure eslint to use testing-library plugin for test files - update react-native-builder-bob to latest version - add react-test-renderer as dev dependency
1 parent ffddb10 commit e374712

File tree

6 files changed

+365
-20
lines changed

6 files changed

+365
-20
lines changed

eslint.config.mjs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,14 @@ const compat = new FlatCompat({
1616

1717
export default defineConfig([
1818
{
19-
extends: fixupConfigRules(compat.extends('@react-native', 'prettier')),
19+
files: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
20+
extends: fixupConfigRules(
21+
compat.extends(
22+
'@react-native',
23+
'prettier',
24+
'plugin:testing-library/react'
25+
)
26+
),
2027
plugins: { prettier },
2128
rules: {
2229
'react/react-in-jsx-scope': 'off',
@@ -30,12 +37,10 @@ export default defineConfig([
3037
useTabs: false,
3138
},
3239
],
40+
'testing-library/prefer-screen-queries': 'off',
3341
},
3442
},
3543
{
36-
ignores: [
37-
'node_modules/',
38-
'lib/'
39-
],
44+
ignores: ['node_modules/', 'lib/'],
4045
},
4146
]);

package.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,21 @@
8888
"@evilmartians/lefthook": "^1.5.0",
8989
"@react-native/eslint-config": "^0.78.0",
9090
"@release-it/conventional-changelog": "^9.0.2",
91+
"@testing-library/react-native": "^13.2.0",
9192
"@types/jest": "^29.5.5",
9293
"@types/react": "^19.0.12",
9394
"commitlint": "^19.6.1",
9495
"del-cli": "^5.1.0",
9596
"eslint": "^9.22.0",
9697
"eslint-config-prettier": "^10.1.1",
9798
"eslint-plugin-prettier": "^5.2.3",
99+
"eslint-plugin-testing-library": "^7.2.2",
98100
"jest": "^29.7.0",
99101
"prettier": "^3.0.3",
100102
"react": "18.3.1",
101103
"react-native": "0.76.8",
102-
"react-native-builder-bob": "^0.39.0",
104+
"react-native-builder-bob": "^0.40.11",
105+
"react-test-renderer": "18.3.1",
103106
"release-it": "^17.10.0",
104107
"typescript": "^5.2.2"
105108
},
@@ -176,6 +179,6 @@
176179
"create-react-native-library": {
177180
"languages": "js",
178181
"type": "library",
179-
"version": "0.49.0"
182+
"version": "0.50.3"
180183
}
181184
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { fireEvent, render as TLRender } from '@testing-library/react-native';
2+
import { TextInputOTP, TextInputOTPSlot } from '../components';
3+
import type { TextInputOTPProps } from '../types';
4+
5+
function render(props?: Partial<TextInputOTPProps>) {
6+
return TLRender(
7+
<TextInputOTP maxLength={6} {...props}>
8+
<TextInputOTPSlot index={0} />
9+
<TextInputOTPSlot index={1} />
10+
<TextInputOTPSlot index={2} />
11+
<TextInputOTPSlot index={3} />
12+
<TextInputOTPSlot index={4} />
13+
<TextInputOTPSlot index={5} />
14+
</TextInputOTP>
15+
);
16+
}
17+
18+
describe('TextInputOTP Component', () => {
19+
beforeAll(() => {
20+
jest.resetAllMocks();
21+
});
22+
23+
it('should call onFilled with the complete code when all digits are filled', () => {
24+
const CODE = '123456';
25+
const mockedOnFilled = jest.fn();
26+
const view = render({ onFilled: mockedOnFilled });
27+
fireEvent.changeText(view.getByTestId('hidden-text-input'), CODE);
28+
29+
expect(mockedOnFilled).toHaveBeenCalledWith(CODE);
30+
});
31+
});

src/components/text-input-otp-slot.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Pressable, Text, StyleSheet } from 'react-native';
33
import { Caret } from './caret';
44
import { useTextInputOTP } from '../hooks/use-text-input-otp';
55
import { useSlotBorderStyles } from '../hooks/use-slot-border-styles';
6-
import { SLOT_HEIGHT, SLOT_WIDTH } from '../constants';
6+
import { DEFAULT_DARK_COLOR, SLOT_HEIGHT, SLOT_WIDTH } from '../constants';
77
import type {
88
TextInputOTPSlotInternalProps,
99
TextInputOTPSlotProps,
@@ -60,7 +60,7 @@ const styles = StyleSheet.create({
6060
alignItems: 'center',
6161
},
6262
slotText: {
63-
color: '#030712',
63+
color: DEFAULT_DARK_COLOR,
6464
fontSize: 14,
6565
fontWeight: 'bold',
6666
},

src/components/text-input.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export const TextInput = forwardRef<
1919

2020
return (
2121
<RNTextInput
22+
testID="hidden-text-input"
2223
ref={inputRef}
2324
keyboardType="number-pad"
2425
autoFocus={autoFocus}

0 commit comments

Comments
 (0)