Skip to content
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

feat: no-await-react-state in /react ruleset #50

Open
codejedi365 opened this issue Nov 14, 2021 · 0 comments
Open

feat: no-await-react-state in /react ruleset #50

codejedi365 opened this issue Nov 14, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request

Comments

@codejedi365
Copy link
Collaborator

Feature Request

Create a rule that warns against bad patterns which do not use the Smart Assertion Query mechanism when using the getReact() client function.

Problem & Goal

If using testcafe-react-selectors, you can retrieve the React Component State through a ReactSelector("MyComponent").getReact() call. If the statement is prepended with await then it will return a static object of the current state of react component upon the resolution of the client function. In the case react was still processing this could cause brittle test cases. Recommend using the Smart Assertion pattern through the use of getReact() and passing the client function directly into the t.expect() method.

The goal is to prevent brittle test cases by enabling the Smart Assertion callback to use the ClientFunction returned by getReact() inside t.expect()'s.

Expected behavior

// page.test.js -- invalid code
import { ReactSelector } from 'testcafe-react-selectors';

fixture `TODO list test`
	.page('http://localhost:1337');

test('Check list item', async t => {
    const el         = ReactSelector('TodoList');
    const component  = await el.getReact();

    await t.expect(component.props.priority).eql('High');
    await t.expect(component.state.isActive).eql(false);
    await t.expect(component.key).eql('componentID');
});
# Run
eslint ./page.test.js
// page.test.js -- correct code
import { ReactSelector } from 'testcafe-react-selectors';

fixture `TODO list test`
	.page('http://localhost:1337');

test('Check list item', async t => {
    const el         = ReactSelector('TodoList');
    const component  = el.getReact();   // Returns ClientFunction()

    // Smart Assertion Query Mechanism enabled
    await t.expect(component.props.priority).eql('High');
    await t.expect(component.state.isActive).eql(false);
    await t.expect(component.key).eql('componentID');
    // Alternatives without reuse
    // await t.expect(el.getReact(({ state }) => state.isActive)).eql(false);
    // await t.expect(el.getReact().key).eql('componentID');
});
@codejedi365 codejedi365 added the enhancement New feature or request label Nov 14, 2021
@codejedi365 codejedi365 self-assigned this Nov 14, 2021
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

No branches or pull requests

1 participant