Skip to content

Commit 31f6b90

Browse files
committed
refactor: tweaks
1 parent 86e866e commit 31f6b90

File tree

6 files changed

+62
-61
lines changed

6 files changed

+62
-61
lines changed

src/test-utils/events.ts

Lines changed: 5 additions & 1 deletion
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

Lines changed: 6 additions & 11 deletions
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/press/__tests__/press.real-timers.test.tsx

Lines changed: 5 additions & 5 deletions
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

Lines changed: 7 additions & 7 deletions
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

Lines changed: 3 additions & 5 deletions
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',

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

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
import * as React from 'react';
22
import { TextInput, TextInputProps, View } from 'react-native';
3-
import { createEventLogger, EventEntry } from '../../../test-utils';
3+
import {
4+
createEventLogger,
5+
getEventsNames,
6+
lastEvent,
7+
lastEventPayload,
8+
} from '../../../test-utils';
49
import { render, screen } from '../../..';
510
import { userEvent } from '../..';
611

@@ -43,8 +48,7 @@ describe('type()', () => {
4348
const user = userEvent.setup();
4449
await user.type(screen.getByTestId('input'), 'abc');
4550

46-
const eventNames = events.map((e) => e.name);
47-
expect(eventNames).toEqual([
51+
expect(getEventsNames(events)).toEqual([
4852
'pressIn',
4953
'focus',
5054
'pressOut',
@@ -74,8 +78,7 @@ describe('type()', () => {
7478
const user = userEvent.setup();
7579
await user.type(screen.getByTestId('input'), 'abc');
7680

77-
const eventNames = events.map((e) => e.name);
78-
expect(eventNames).toEqual([
81+
expect(getEventsNames(events)).toEqual([
7982
'pressIn',
8083
'focus',
8184
'pressOut',
@@ -104,8 +107,7 @@ describe('type()', () => {
104107
const user = userEvent.setup();
105108
await user.type(screen.getByTestId('input'), 'ab');
106109

107-
const eventNames = events.map((e) => e.name);
108-
expect(eventNames).toEqual([
110+
expect(getEventsNames(events)).toEqual([
109111
'pressIn',
110112
'focus',
111113
'pressOut',
@@ -132,8 +134,7 @@ describe('type()', () => {
132134
const user = userEvent.setup();
133135
await user.type(screen.getByTestId('input'), 'ab');
134136

135-
const eventNames = events.map((e) => e.name);
136-
expect(eventNames).toEqual([]);
137+
expect(getEventsNames(events)).toEqual([]);
137138
});
138139

139140
it('supports backspace', async () => {
@@ -144,8 +145,7 @@ describe('type()', () => {
144145
const user = userEvent.setup();
145146
await user.type(screen.getByTestId('input'), '{Backspace}a');
146147

147-
const eventNames = events.map((e) => e.name);
148-
expect(eventNames).toEqual([
148+
expect(getEventsNames(events)).toEqual([
149149
'pressIn',
150150
'focus',
151151
'pressOut',
@@ -172,8 +172,7 @@ describe('type()', () => {
172172
const user = userEvent.setup();
173173
await user.type(screen.getByTestId('input'), '{Enter}\n');
174174

175-
const eventNames = events.map((e) => e.name);
176-
expect(eventNames).toEqual([
175+
expect(getEventsNames(events)).toEqual([
177176
'pressIn',
178177
'focus',
179178
'pressOut',
@@ -204,7 +203,7 @@ describe('type()', () => {
204203
skipPress: true,
205204
});
206205

207-
const eventNames = events.map((e) => e.name);
206+
const eventNames = getEventsNames(events);
208207
expect(eventNames).not.toContainEqual('pressIn');
209208
expect(eventNames).not.toContainEqual('pressOut');
210209
expect(eventNames).toEqual([
@@ -217,7 +216,7 @@ describe('type()', () => {
217216
'blur',
218217
]);
219218

220-
expect(lastEvent(events, 'endEditing')?.payload).toMatchObject({
219+
expect(lastEventPayload(events, 'endEditing')).toMatchObject({
221220
nativeEvent: { text: 'a', target: 0 },
222221
});
223222
});
@@ -230,8 +229,7 @@ describe('type()', () => {
230229
submitEditing: true,
231230
});
232231

233-
const eventNames = events.map((e) => e.name);
234-
expect(eventNames).toEqual([
232+
expect(getEventsNames(events)).toEqual([
235233
'pressIn',
236234
'focus',
237235
'pressOut',
@@ -244,7 +242,7 @@ describe('type()', () => {
244242
'blur',
245243
]);
246244

247-
expect(lastEvent(events, 'submitEditing')?.payload).toMatchObject({
245+
expect(lastEventPayload(events, 'submitEditing')).toMatchObject({
248246
nativeEvent: { text: 'a', target: 0 },
249247
currentTarget: {},
250248
target: {},
@@ -264,8 +262,12 @@ describe('type()', () => {
264262
const user = userEvent.setup();
265263
await user.type(screen.getByTestId('input'), 'abc');
266264

267-
const eventNames = events.map((e) => e.name);
268-
expect(eventNames).toEqual(['changeText', 'changeText', 'changeText', 'endEditing']);
265+
expect(getEventsNames(events)).toEqual([
266+
'changeText',
267+
'changeText',
268+
'changeText',
269+
'endEditing',
270+
]);
269271

270272
expect(events).toMatchSnapshot('input: "abc"');
271273
});
@@ -320,8 +322,13 @@ describe('type()', () => {
320322

321323
await userEvent.type(screen.getByTestId('input'), 'abc');
322324

323-
const eventNames = events.map((event) => event.name);
324-
expect(eventNames).toEqual(['focus', 'changeText', 'changeText', 'changeText', 'blur']);
325+
expect(getEventsNames(events)).toEqual([
326+
'focus',
327+
'changeText',
328+
'changeText',
329+
'changeText',
330+
'blur',
331+
]);
325332
});
326333

327334
// See: https://github.com/callstack/react-native-testing-library/issues/1588
@@ -347,8 +354,7 @@ describe('type()', () => {
347354
const user = userEvent.setup();
348355
await user.type(screen.getByTestId('input'), 'abcd');
349356

350-
const eventNames = events.map((e) => e.name);
351-
expect(eventNames).toEqual([
357+
expect(getEventsNames(events)).toEqual([
352358
'pressIn',
353359
'focus',
354360
'pressOut',
@@ -366,14 +372,12 @@ describe('type()', () => {
366372
'blur',
367373
]);
368374

369-
expect(lastEvent(events, 'changeText')?.payload).toBe('ab');
370-
expect(lastEvent(events, 'endEditing')?.payload.nativeEvent).toMatchObject({
371-
target: 0,
372-
text: 'ab',
375+
expect(lastEventPayload(events, 'changeText')).toBe('ab');
376+
expect(lastEventPayload(events, 'endEditing')).toMatchObject({
377+
nativeEvent: {
378+
target: 0,
379+
text: 'ab',
380+
},
373381
});
374382
});
375383
});
376-
377-
function lastEvent(events: EventEntry[], name: string) {
378-
return events.filter((e) => e.name === name).pop();
379-
}

0 commit comments

Comments
 (0)