Giter Site home page Giter Site logo

mulyoved / testcafe-react-selectors Goto Github PK

View Code? Open in Web Editor NEW

This project forked from devexpress/testcafe-react-selectors

1.0 2.0 0.0 64 KB

TesCafe selector extensions for React apps.

Home Page: https://devexpress.github.io/testcafe/

License: MIT License

JavaScript 95.77% HTML 4.23%

testcafe-react-selectors's Introduction

testcafe-react-selectors

This plugin provides selector extensions that make it easier to test ReactJS components with TestCafe. These extensions allow you to select page elements in a way that is native to React.

Install

$ npm install testcafe-react-selectors

Usage

Create selectors for ReactJS components

ReactSelector allows you to select page elements by the name of the component class or the nested component element.

Suppose you have the following JSX.

<TodoApp className="todo-app">
    <TodoInput />
    <TodoList>
        <TodoItem priority="High">Item 1</TodoItem>
        <TodoItem priority="Low">Item 2</TodoItem>
    </TodoList>   
    
    <div className="items-count">Items count: <span>{this.state.itemCount}</span></div>
</TodoApp>

To get a root DOM element for a component, pass the component name to the ReactSelector constructor.

import ReactSelector from 'testcafe-react-selector';

const todoInput = ReactSelector('TodoInput');

To obtain a nested component or DOM element, you can use a combined selector or add DOM element's tag name.

import ReactSelector from 'testcafe-react-selector';

const TodoList         = ReactSelector('TodoApp TodoList');
const itemsCountStatus = ReactSelector('TodoApp div');
const itemsCount       = ReactSelector('TodoApp div span');

Warning: if you specify a DOM element’s tag name, React selectors search for the element among the component’s children without looking into nested components. For instance, for the JSX above the ReactSelector('TodoApp div') selector will be equal to Selector('.todo-app > div').

Selectors returned by ReactSelector( selector ) are recognized as TestCafe selectors. You can combine them with regular selectors and filter with .withText, .nth, .find and other functions. To search for elements within a component, you can use the following combined approach.

import ReactSelector from 'testcafe-react-selector';

var itemsCount = ReactSelector('TodoApp').find('.items-count span');

Let’s use the API described above to add a task to a Todo list and check that the number of items changed.

import ReactSelector from 'testcafe-react-selector';

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

test('Add new task', async t => {
    const todoTextInput = ReactSelector('TodoInput');
    const todoItem      = ReactSelector('TodoList TodoItem');

    await t
        .typeText(todoTextInput, 'My Item')
        .pressKey('enter')
        .expect(todoItem.count).eql(3);
});

Obtaining component's props and state

As an alternative to testcafe snapshot properties, you can obtain state or props of a ReactJS component. You can use them in an assertion directly thus simplifying assertion logic.

To obtain component properties and state, use the React selector’s .getReact() method.

If you call this method with zero parameters, it returns an object of the following structure.

{
    props: <component_props>,
    state: <component_state>
}

Where props are React component properties excluding properties of its children, state – the state of the component.

Example

import ReactSelector from 'testcafe-react-selector';

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

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

    await t.expect(el.getReact().props.priority).eql('High');
    await t.expect(el.getReact().state.isActive).eql(false);
});

As an alternative, the .getReact() method can take a function that returns the required property or state. This function acts as a filter. Its argument is an object returned by .getReact(), i.e. { props: ..., state: ...}.

ReactSelector('Component').getReact(({ props, state }) => {...})

Example

import ReactSelector from 'testcafe-react-selector';

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

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

    await t
        .expect(el.getReact(({ props }) => props.priority)).eql('High')
        .expect(el.getReact(({ state }) => state.isActive)).eql(false);
});

The .getReact() method can be called for the ReactSelector or the snapshot this selector returns.

Limitations

testcafe-react-selectors support ReactJS starting with version 15. ReactSelector can only find components inherited from React.Component. To check if a component can be found, use the react-dev-tools extension. Search for a component starts from the root React component, so selectors like ReactSelector('body MyComponent') will return null.

testcafe-react-selectors's People

Contributors

kirovboris avatar inikulin avatar vasilystrelyaev avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar Muly Oved avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.