Skip to content

Commit 6a51a81

Browse files
fix: Respect maxLength prop in type() function (#1641)
* fix: Respect maxLength prop in type() function This commit modifies the `type()` function to respect the `maxLength` prop of the input element. If the current text length exceeds the `maxLength` value, typing events will not be emitted. This ensures that the input value does not exceed the specified maximum length. 1. Create an input element with a `maxLength` prop. 2. Use the `type()` function to input text that exceeds the `maxLength` value. 3. Verify that the input value does not exceed the specified maximum length. 4. Check that no typing events are emitted once the `maxLength` is reached. Additionally, see that `yarn test` passes with an additional test added to `src/user-event/type/__tests__/type.test.tsx` * refactor: code review changes * refactor: tweaks * refactor: final tweaks * refactor: final tweaks 2 * chore: tweak codecov --------- Co-authored-by: Maciej Jastrzębski <mdjastrzebski@gmail.com>
1 parent 6cc4c0c commit 6a51a81

File tree

9 files changed

+152
-88
lines changed

9 files changed

+152
-88
lines changed

.codecov.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ coverage:
22
precision: 2
33
round: down
44
range: 70...100
5+
status:
6+
project:
7+
default:
8+
threshold: 0.5% # Allowable coverage drop in percentage points
59

610
comment:
711
behavior: default
812
require_changes: false
9-
require_base: no
13+
require_base: false

src/test-utils/events.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ export function createEventLogger() {
1919
return { events, logEvent };
2020
}
2121

22-
export function getEventsName(events: EventEntry[]) {
22+
export function getEventsNames(events: EventEntry[]) {
2323
return events.map((event) => event.name);
2424
}
25+
26+
export function lastEventPayload(events: EventEntry[], name: string) {
27+
return events.filter((e) => e.name === name).pop()?.payload;
28+
}

src/user-event/__tests__/clear.test.tsx

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { TextInput, TextInputProps, View } from 'react-native';
3-
import { createEventLogger } from '../../test-utils';
3+
import { createEventLogger, getEventsNames } from '../../test-utils';
44
import { render, userEvent, screen } from '../..';
55

66
beforeEach(() => {
@@ -47,8 +47,7 @@ describe('clear()', () => {
4747
const user = userEvent.setup();
4848
await user.clear(textInput);
4949

50-
const eventNames = events.map((e) => e.name);
51-
expect(eventNames).toEqual([
50+
expect(getEventsNames(events)).toEqual([
5251
'focus',
5352
'selectionChange',
5453
'keyPress',
@@ -71,8 +70,7 @@ describe('clear()', () => {
7170
const user = userEvent.setup();
7271
await user.clear(textInput);
7372

74-
const eventNames = events.map((e) => e.name);
75-
expect(eventNames).toEqual([
73+
expect(getEventsNames(events)).toEqual([
7674
'focus',
7775
'selectionChange',
7876
'keyPress',
@@ -92,8 +90,7 @@ describe('clear()', () => {
9290
const user = userEvent.setup();
9391
await user.clear(textInput);
9492

95-
const eventNames = events.map((e) => e.name);
96-
expect(eventNames).toEqual([
93+
expect(getEventsNames(events)).toEqual([
9794
'focus',
9895
'selectionChange',
9996
'keyPress',
@@ -140,8 +137,7 @@ describe('clear()', () => {
140137
const user = userEvent.setup();
141138
await user.clear(textInput);
142139

143-
const eventNames = events.map((e) => e.name);
144-
expect(eventNames).toEqual([
140+
expect(getEventsNames(events)).toEqual([
145141
'focus',
146142
'selectionChange',
147143
'keyPress',
@@ -170,8 +166,7 @@ describe('clear()', () => {
170166
const user = userEvent.setup();
171167
await user.clear(screen.getByTestId('input'));
172168

173-
const eventNames = events.map((e) => e.name);
174-
expect(eventNames).toEqual(['changeText', 'endEditing']);
169+
expect(getEventsNames(events)).toEqual(['changeText', 'endEditing']);
175170

176171
expect(events).toMatchSnapshot();
177172
});

src/user-event/clear.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,14 @@ export async function clear(this: UserEventInstance, element: ReactTestInstance)
3131
};
3232
dispatchEvent(element, 'selectionChange', EventBuilder.TextInput.selectionChange(selectionRange));
3333

34-
// 3. Press backspace
34+
// 3. Press backspace with selected text
3535
const finalText = '';
36-
await emitTypingEvents(this.config, element, 'Backspace', finalText, previousText);
36+
await emitTypingEvents(element, {
37+
config: this.config,
38+
key: 'Backspace',
39+
text: finalText,
40+
previousText,
41+
});
3742

3843
// 4. Exit element
3944
await wait(this.config);

src/user-event/press/__tests__/press.real-timers.test.tsx

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
TouchableOpacity,
88
View,
99
} from 'react-native';
10-
import { createEventLogger, getEventsName } from '../../../test-utils';
10+
import { createEventLogger, getEventsNames } from '../../../test-utils';
1111
import { render, screen } from '../../..';
1212
import { userEvent } from '../..';
1313
import * as WarnAboutRealTimers from '../../utils/warn-about-real-timers';
@@ -34,7 +34,7 @@ describe('userEvent.press with real timers', () => {
3434
);
3535
await user.press(screen.getByTestId('pressable'));
3636

37-
expect(getEventsName(events)).toEqual(['pressIn', 'press', 'pressOut']);
37+
expect(getEventsNames(events)).toEqual(['pressIn', 'press', 'pressOut']);
3838
});
3939

4040
test('does not trigger event when pressable is disabled', async () => {
@@ -130,7 +130,7 @@ describe('userEvent.press with real timers', () => {
130130
);
131131
await user.press(screen.getByTestId('pressable'));
132132

133-
expect(getEventsName(events)).toEqual(['pressIn', 'press', 'pressOut']);
133+
expect(getEventsNames(events)).toEqual(['pressIn', 'press', 'pressOut']);
134134
});
135135

136136
test('crawls up in the tree to find an element that responds to touch events', async () => {
@@ -200,7 +200,7 @@ describe('userEvent.press with real timers', () => {
200200
);
201201
await userEvent.press(screen.getByText('press me'));
202202

203-
expect(getEventsName(events)).toEqual(['pressIn', 'press', 'pressOut']);
203+
expect(getEventsNames(events)).toEqual(['pressIn', 'press', 'pressOut']);
204204
});
205205

206206
test('does not trigger on disabled Text', async () => {
@@ -254,7 +254,7 @@ describe('userEvent.press with real timers', () => {
254254
);
255255
await userEvent.press(screen.getByPlaceholderText('email'));
256256

257-
expect(getEventsName(events)).toEqual(['pressIn', 'pressOut']);
257+
expect(getEventsNames(events)).toEqual(['pressIn', 'pressOut']);
258258
});
259259

260260
test('does not call onPressIn and onPressOut on non editable TetInput', async () => {

src/user-event/press/__tests__/press.test.tsx

+7-7
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
View,
99
Button,
1010
} from 'react-native';
11-
import { createEventLogger, getEventsName } from '../../../test-utils';
11+
import { createEventLogger, getEventsNames } from '../../../test-utils';
1212
import { render, screen } from '../../..';
1313
import { userEvent } from '../..';
1414

@@ -129,7 +129,7 @@ describe('userEvent.press with fake timers', () => {
129129
);
130130
await user.press(screen.getByTestId('pressable'));
131131

132-
expect(getEventsName(events)).toEqual(['pressIn', 'press', 'pressOut']);
132+
expect(getEventsNames(events)).toEqual(['pressIn', 'press', 'pressOut']);
133133
});
134134

135135
test('crawls up in the tree to find an element that responds to touch events', async () => {
@@ -199,7 +199,7 @@ describe('userEvent.press with fake timers', () => {
199199
);
200200

201201
await userEvent.press(screen.getByText('press me'));
202-
expect(getEventsName(events)).toEqual(['pressIn', 'press', 'pressOut']);
202+
expect(getEventsNames(events)).toEqual(['pressIn', 'press', 'pressOut']);
203203
});
204204

205205
test('press works on Button', async () => {
@@ -208,7 +208,7 @@ describe('userEvent.press with fake timers', () => {
208208
render(<Button title="press me" onPress={logEvent('press')} />);
209209

210210
await userEvent.press(screen.getByText('press me'));
211-
expect(getEventsName(events)).toEqual(['press']);
211+
expect(getEventsNames(events)).toEqual(['press']);
212212
});
213213

214214
test('longPress works Text', async () => {
@@ -226,7 +226,7 @@ describe('userEvent.press with fake timers', () => {
226226
);
227227

228228
await userEvent.longPress(screen.getByText('press me'));
229-
expect(getEventsName(events)).toEqual(['pressIn', 'longPress', 'pressOut']);
229+
expect(getEventsNames(events)).toEqual(['pressIn', 'longPress', 'pressOut']);
230230
});
231231

232232
test('does not trigger on disabled Text', async () => {
@@ -280,7 +280,7 @@ describe('userEvent.press with fake timers', () => {
280280
);
281281

282282
await userEvent.press(screen.getByPlaceholderText('email'));
283-
expect(getEventsName(events)).toEqual(['pressIn', 'pressOut']);
283+
expect(getEventsNames(events)).toEqual(['pressIn', 'pressOut']);
284284
});
285285

286286
test('longPress works on TextInput', async () => {
@@ -295,7 +295,7 @@ describe('userEvent.press with fake timers', () => {
295295
);
296296

297297
await userEvent.longPress(screen.getByPlaceholderText('email'));
298-
expect(getEventsName(events)).toEqual(['pressIn', 'pressOut']);
298+
expect(getEventsNames(events)).toEqual(['pressIn', 'pressOut']);
299299
});
300300

301301
test('does not call onPressIn and onPressOut on non editable TextInput', async () => {

src/user-event/type/__tests__/type-managed.test.tsx

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22
import { TextInput } from 'react-native';
3-
import { createEventLogger } from '../../../test-utils';
3+
import { createEventLogger, getEventsNames } from '../../../test-utils';
44
import { render, screen } from '../../..';
55
import { userEvent } from '../..';
66

@@ -56,8 +56,7 @@ describe('type() for managed TextInput', () => {
5656
const user = userEvent.setup();
5757
await user.type(screen.getByTestId('input'), 'Wow');
5858

59-
const eventNames = events.map((e) => e.name);
60-
expect(eventNames).toEqual([
59+
expect(getEventsNames(events)).toEqual([
6160
'pressIn',
6261
'focus',
6362
'pressOut',
@@ -90,8 +89,7 @@ describe('type() for managed TextInput', () => {
9089
const user = userEvent.setup();
9190
await user.type(screen.getByTestId('input'), 'ABC');
9291

93-
const eventNames = events.map((e) => e.name);
94-
expect(eventNames).toEqual([
92+
expect(getEventsNames(events)).toEqual([
9593
'pressIn',
9694
'focus',
9795
'pressOut',

0 commit comments

Comments
 (0)