-
Notifications
You must be signed in to change notification settings - Fork 735
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
Changes from 5 commits
8d63a5d
567044d
21fc283
3758b70
7345c99
41bcd6f
7ee8d41
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're talking about |
||
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 | ||
}; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't it suppose to return a string?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought so, and then I saw it does not, although I think we do compare it to string in tests, maybe we should return
as string | undefined
so it's more clear?I am really not sure
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's see if it meets us when we or others use it..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As you've mentioned, this can technically be a
Text
child