Skip to content

[RFC] Add jsx pragma integration #169

@rams23

Description

@rams23

Hi.
First of all thank you for the cavy library. I started using it and I was thinking if can be possible to reduce the normal code impact, removing wrap functions, useCavy, HOC etc with a jsx pragma that replaces createElement with a simple wrap.

I already executed some test and created a custom jsx function like this:

// index.js of cavy-jsx
let testStore = null;

export function setTestStore(store) {
  testStore = store;
}

export function cavyJsx(type, props, children) {

  if ('cavyTestId' in props && testStore) {
    const WrappedType = wrap(type);
    const generateTestHook = cavy(testStore);
    const newProps = {...props, ref: generateTestHook(props.cavyTestId)};
    return React.createElement(WrappedType, newProps, children);
  }

  return React.createElement.apply(undefined, arguments);
}



//index.test.js
import { setTestStore } from "cavy-jsx";

const testHookStore = new TestHookStore();

setTestStore(testHookStore);



//LoginScreen.js
/** @jsx cavyJsx */
/** @jsxFrag React.Fragment */
import { cavyJsx } from 'cavy-jsx';
...
<TextInput
          value={password}
          cavyTestId="LoginScreen.PasswordInput"
          onChangeText={setPassword}
/>
<TextInput
          value={password}
          cavyTestId="LoginScreen.PasswordInput"
          onChangeText={setPassword}
/>
<Button 
          cavyTestId="LoginScreen.Button"
          onPress={doLogin}
>
      <Text>Login</Text>
</Button>

this way the code stays clean but cavy still works and can also wrap components automatically.
I was also trying to configure @babel/plugin-transform-react-jsx to automatically inject the pragma with no success but i thing it can be feasible. The solution with the eplicit jsx pragma already works.

What do you thing?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions