Skip to content

fix: Respect maxLength prop in type() function #1641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: final tweaks
  • Loading branch information
mdjastrzebski committed Aug 9, 2024
commit 27d6d974a4b72dc6335a1958ac1a26306af95957
7 changes: 1 addition & 6 deletions src/user-event/type/__tests__/type.test.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import * as React from 'react';
import { TextInput, TextInputProps, View } from 'react-native';
import {
createEventLogger,
getEventsNames,
lastEvent,
lastEventPayload,
} from '../../../test-utils';
import { createEventLogger, getEventsNames, lastEventPayload } from '../../../test-utils';
import { render, screen } from '../../..';
import { userEvent } from '../..';

Expand Down
9 changes: 3 additions & 6 deletions src/user-event/type/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ export async function type(
for (const key of keys) {
const previousText = element.props.value ?? currentText;
const proposedText = applyKey(previousText, key);
let isAccepted = false;
if (isTextChangeAllowed(element, proposedText)) {
currentText = proposedText;
isAccepted = true;
}
const isAccepted = isTextChangeAccepted(element, proposedText);
currentText = isAccepted ? proposedText : previousText;

await emitTypingEvents(element, {
config: this.config,
Expand Down Expand Up @@ -138,7 +135,7 @@ function applyKey(text: string, key: string) {
return text + key;
}

function isTextChangeAllowed(element: ReactTestInstance, text: string) {
function isTextChangeAccepted(element: ReactTestInstance, text: string) {
const maxLength = element.props.maxLength;
return maxLength === undefined || text.length <= maxLength;
}