Skip to content

Instantly share code, notes, and snippets.

@mptorz
Last active Sep 26, 2019
Embed
What would you like to do?
Useful wrappers to add identifier with Cavy e2e test library
// EXAMPLE USAGE:
// export default withCavyCustomIdentifier('SomeCustomIdentifier')(SomeScreen)
export const withCavyCustomIdentifier = (identifier: string) => <T,>(originalComponent: ComponentType<T>) => {
const ComponentWithIdentifier = (props: T) => {
const WrappedComponent = !!originalComponent.prototype.render ? originalComponent : wrap(originalComponent)
const generateTestHook = useCavy()
return (<WrappedComponent {...props} ref={generateTestHook(identifier)} />)
}
return ComponentWithIdentifier
}
// EXAMPLE USAGE:
// export default withCavyIdentifier(SomeScreen)
export const withCavyIdentifier = <T,>(originalComponent: ComponentType<T>) => {
const ComponentWithIdentifier = (props: T) => {
const WrappedComponent = !!originalComponent.prototype.render ? originalComponent : wrap(originalComponent)
const generateTestHook = useCavy()
return (<WrappedComponent {...props} ref={generateTestHook(originalComponent.name)} />)
}
return ComponentWithIdentifier
}
// then we can easily use:
// spec.exists('SomeCustomIdentifier') or spec.exists('SomeScreen')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment