Skip to content

Create a testing utility to assert Tailwind (twrnc) classnames #691

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
georgewrmarshall opened this issue May 22, 2025 · 0 comments
Open

Comments

@georgewrmarshall
Copy link
Contributor

Description

Create a testing utility to assert Tailwind (twrnc) classnames used in design system React Native components. This utility will assist in unit testing by verifying the correct application of style classes through the transformation of the style prop.

Technical Details

  • Refactor the existing flattenStyles function to a shared test utility.
  • This function recursively normalizes style props, including arrays, nested arrays, and single objects, into a flat array of ViewStyle objects.
  • The utility should expose a clear API to assert the presence or absence of specific Tailwind classnames in the rendered component styles.
  • Ensure compatibility with twrnc-styled components in the MetaMask mobile design system.
function flattenStyles(
  styleProp: StyleProp<ViewStyle> | undefined,
): ViewStyle[] {
  if (styleProp === null || styleProp === undefined) {
    return [];
  }
  if (Array.isArray(styleProp)) {
    return styleProp.flatMap((item) =>
      flattenStyles(item as StyleProp<ViewStyle>),
    );
  }
  if (typeof styleProp === 'object') {
    return [styleProp as ViewStyle];
  }
  return [];
}
  • Potential enhancements:

    • Include helper functions to map classnames to their corresponding style object values.
    • Add Jest matchers for easy test readability.

Acceptance Criteria

  • Utility successfully extracts and flattens styles from any style prop format.
  • Developers can assert specific Tailwind classnames are applied in unit tests.
  • Coverage for edge cases (e.g., null/undefined styles, nested arrays).
  • Tests added for utility function itself.

References

  • Internal discussion on improving design system test coverage.
  • Related utility logic seen in flattenStyles above.
  • Use with twrnc and React Native components in the MetaMask design system.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant