Skip to content

TextField - create new driver and refactor tests to it #2890

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 7 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/components/text/Text.driver.new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {usePressableDriver} from '../../testkit/new/usePressable.driver';
export const TextDriver = (props: ComponentProps) => {
const driver = usePressableDriver<TextProps>(useComponentDriver(props));

const getText = () => {
const getText = (): React.ReactNode | string | undefined => {
return driver.getProps().children;
};

Expand Down
94 changes: 94 additions & 0 deletions src/components/textField/TextField.driver.new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import _ from 'lodash';
import {fireEvent} from '@testing-library/react-native';
import {TextFieldProps} from './types';
import {useComponentDriver, ComponentProps} from '../../testkit/new/Component.driver';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this a good name for the driver arguments? Perhaps ComponentDriverArguments or DriverArguments will be better?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're talking about ComponentProps then this is not the PR to change it; feel free to create a PR for it.

import {TextDriver} from '../text/Text.driver.new';

export const TextFieldDriver = (props: ComponentProps) => {
const driver = useComponentDriver<TextFieldProps>(props);

const floatingPlaceholderDriver = TextDriver({
renderTree: props.renderTree,
testID: `${props.testID}.floatingPlaceholder`
});
const labelDriver = TextDriver({
renderTree: props.renderTree,
testID: `${props.testID}.label`
});
const validationMsgDriver = TextDriver({
renderTree: props.renderTree,
testID: `${props.testID}.validationMessage`
});
const charCounterDriver = TextDriver({
renderTree: props.renderTree,
testID: `${props.testID}.charCounter`
});

const getValue = (): string | undefined => {
return driver.getProps().value ?? driver.getProps().defaultValue;
};

const changeText = (text: string): void => {
fireEvent.changeText(driver.getElement(), text);
};

const focus = (): void => {
fireEvent(driver.getElement(), 'focus');
};

const blur = (): void => {
fireEvent(driver.getElement(), 'blur');
};

const isEnabled = (): boolean => {
return !driver.getProps().accessibilityState?.disabled;
};

const getPlaceholder = () => {
const exists = (): boolean => {
const hasPlaceholder = !!driver.getProps().placeholder;
const hasText = !!getValue();
return hasPlaceholder && (!hasText || (hasText && floatingPlaceholderDriver.exists()));
};
const getText = (): string | undefined => {
if (exists()) {
return driver.getProps().placeholder;
}
};

return {...floatingPlaceholderDriver, exists, getText};
};

const getLabel = () => {
const exists = (): boolean => {
return labelDriver.exists() && !floatingPlaceholderDriver.exists();
};

return {...labelDriver, exists};
};

const getValidationMessage = () => {
const exists = (): boolean => {
return validationMsgDriver.exists() && !_.isEmpty(validationMsgDriver.getText());
};

return {...validationMsgDriver, exists};
};

const getCharCounter = () => {
return charCounterDriver;
};

return {
...driver,
getValue,
changeText,
focus,
blur,
isEnabled,
getPlaceholder,
getLabel,
getValidationMessage,
getCharCounter
};
};
Loading