-
Notifications
You must be signed in to change notification settings - Fork 115
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
findComponent
doesn't work as expected on <Text> components.
#101
Comments
Hey, maybe I can help you, please post a console.log of your text variable( const text = await spec.findComponent('Test.Text'); ). |
Hi @sdroadie !
I just tried out a quick example using React Native 0.57 and can't see any issues... With a hooked up
I'm closing this issue for now as I can't replicate, but please let me know if this is still an issue and I'll try to help further (as @Rafael-Martins said, a log of your text variable would be useful) |
Hi @AbigailMcP! I am having a similar issue with // spec
const nameFeedback = await spec.findComponent('Text.Name');
console.log('nameFeedback', nameFeedback);
await containsText(nameFeedback, 'hello'); // helper
const containsText = async (component, text) => {
if (!component.props.children.includes(text)) {
throw new Error(`Could not find text ${text}`);
}
}; // component
<Text style={hasError} ref={useCavy()('Text.Name')}>
{message}
</Text> Here is the log:
|
So it doesn't look like |
Ahhh, that makes sense - thanks for sharing your solution @funador! Maybe something similar was at play when @sdroadie was having issues. It might not be much extra help, but Cavy does have a helper function |
@AbigailMcP question.... would import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { FunctionComponent } from 'some-ui-library';
import { hook, wrap } from 'cavy';
class Scene extends Component {
render() {
// This works
const TestableFunctionComponent = wrap(FunctionComponent);
// Does this work?
const TestableText = wrap(Text)
return (
<View>
<TestableText
ref={this.props.generateTestHook('Scene.Text')}
/>
<TestableFunctionComponent
ref={this.props.generateTestHook('Scene.FunctionComponent')}
otherProp={...}
/>
</View>
);
}
} |
Hey @funador - I'm not sure I completely understand your use case. Under the hood, Maybe I'm misunderstanding what you're trying to do! |
I am trying to access props on Wondering if there is a way around it or if we have to test a class/function with |
Ahhh I see, yes it's a bit frustrating but that's the only way around it at the moment. We'll have a think about whether Cavy could help out more in those situations, but for now I'll add a note to the README about writing your own tester functions using RN own components like Thanks for pointing this out @funador 👌 |
@AbigailMcP any updates or practical work-around on this? |
I am having exactly the same problem. @AbigailMcP I couldn't really find a note. If it is added can you post a link here. Also since this problem hasn't really been resolved, would it be possible to reopen this issue? |
Hi everyone! A quick update on the above issue. The problem The solution (update to wrap function) We're proposing that you should be able to pass a non-function component like You'll then be able to test // src/MyPage.js
import React from 'react';
import { View, Text } from 'react-native';
import { useCavy, wrap } from 'cavy';
export default ({ data }) => {
const generateTestHook = useCavy();
const TestableText = wrap(Text);
return (
<View>
<TestableText ref={generateTestHook('MyPage.Title')}>
{data.title}
</TestableText>
</View>
)
}; In your test: // specs/MySpec.js
import { containsText } from './helpers';
export default function(spec) {
spec.describe('In this context', async function() {
spec.it('shows this text', async function() {
const textComponent = await spec.findComponent('MyPage.Title');
await containsText(text, 'Welcome');
});
});
} With custom helper function: // specs/helpers.js
export async function containsText(component, text) {
if (!component.props.children.includes(text)) {
throw new Error(`Could not find text ${text}`);
};
} I also propose that we make that I'll be working on this enhancement over the next day or so - will keep y'all updated. |
@mptorz just FYI this will affect the typings for |
No worries! I am quite busy at work, so I will probably do the PR with typings over the weekend anyway :) Thanks for addressing this issue! |
Included in version 3.1.0 released today 🎉 |
According to the README.md,
<Text>
components should be inspectable usingfindComponent
, and I've been unable to do this. In the following example spec:Assume here that
Test.Input
andTest.Text
exist, that filling inTest.Input
sets the text inTest.Text
, and thatcontainsText
matches the example in the README.md. When this test is run, it returns an error, loggingCannot read property 'children' of undefined
. It looks likefindComponent
isn't returning what is expected.This is on React Native 0.56.
The text was updated successfully, but these errors were encountered: