Skip to content

feat(native): Add toContainElement() #146

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

Open
wants to merge 14 commits into
base: feat/native-to-be-visible
Choose a base branch
from

Conversation

kdquistanchala
Copy link
Contributor

This PR adds the toContainElement() matcher for React Native

@kdquistanchala kdquistanchala added the enhancement New feature or request label Mar 24, 2025
@kdquistanchala kdquistanchala self-assigned this Mar 24, 2025
.toHaveMessage("Expected element <View ... /> to contain element <Text ... />.");
});
});
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we please add some tests for when there are no child nor parent elements 🙏

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the recommendation! To take this into account, I've changed the structure of the tests so they're clearer and easier to read 🚀

Copy link
Contributor

@suany0805 suany0805 left a comment

Choose a reason for hiding this comment

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

Nice work @kdquistanchala !

Copy link
Member

@JoseLion JoseLion left a comment

Choose a reason for hiding this comment

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

Looks good so far. I left a few comments. Let me know if you have any questions 🙂

Comment on lines +144 to +158
const isElementContained = (
parentElement: ReactTestInstance,
childElement: ReactTestInstance,
): boolean => {
if (parentElement === null || childElement === null) {
return false;
}

return (
parentElement.findAll(
node =>
node.type === childElement.type && node.props === childElement.props,
).length > 0
);
};
Copy link
Member

Choose a reason for hiding this comment

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

Does this need to be an inner function? We're on the same scope, so we don't even need to pass args to the function. We should be able to rework this so a simple variable easily 🤔

}

return (
parentElement.findAll(
Copy link
Member

Choose a reason for hiding this comment

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

You can use the .some() method instead of finding all elements and then checking if the length is > 0 🙂

Comment on lines +277 to +282
<View testID="grandParentId">
<View testID="parentId">
<View testID="childId" />
</View>
<Text testID="textId" />
</View>,
Copy link
Member

Choose a reason for hiding this comment

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

Can we use <Text> elements so that we can avoid the testIDs?

@@ -271,4 +271,64 @@ describe("[Unit] ElementAssertion.test.ts", () => {
});
});
});

describe (".toContainElement", () => {
const element = render(
Copy link
Member

Choose a reason for hiding this comment

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

Destructiring helps to make the queries simpler:

Suggested change
const element = render(
const { getByText, ... } = render(

Comment on lines +275 to +276
describe (".toContainElement", () => {
const element = render(
Copy link
Member

Choose a reason for hiding this comment

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

As a good practice, elements should be rendered on each test it(..) function, so the cleanup function cleans what's rendered before each test, avoid flaky tests, etc.

@kdquistanchala kdquistanchala force-pushed the feat/native-to-be-visible branch from 35e5052 to 82fa78b Compare April 25, 2025 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants