Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/jamiebuilds/react-markers

Add markers to your React components for easy testing with actual DOM elements
https://github.com/jamiebuilds/react-markers

dom react selenium testing

Last synced: 12 days ago
JSON representation

Add markers to your React components for easy testing with actual DOM elements

Awesome Lists containing this project

README

        

# react-markers

> Add markers to your React components for easy testing with actual DOM elements

## Installation

```sh
yarn add react-markers
```

## Usage

Create static elements to use in your component, and export them from your
module.

```js
// ConfirmPage.js
import marker from 'react-markers';

export const confirmForm = marker();
export const confirmButton = marker();

export default function ConfirmPage() {
return (

Confirm action


This will perform an action


Submit

);
}
```

Then in your tests, you can use `find()` and `findAll()` to lookup these
markers from the DOM.

```js
// ConfirmPage.test.js
import { render } from 'react-dom';
import ConfirmPage, { confirmForm, confirmButton } from './ConfirmPage';
import marker from 'react-markers';

const container = document.getElementById('test-container');

render(, container);

let form = marker.find(container, confirmForm); //
let btn = marker.find(form, confirmButton); //
```

Be sure that `process.env.NODE_ENV` is set to `"test"` when running your tests.

## API

#### `marker()`

In test environments this returns an object of props to pass to your DOM
elements. In other environments it returns `null`.

```js
export const myButton = marker();
export default function MyButton() {
return ;
}
```

#### find(element, marker)

`element` is a DOM element and `marker` is the value returned by `marker()`.

It returns either the matched DOM element or `null`.

```js
import MyButton, { myButton } from './MyButton';
render(, container);
findAll(container, myButton); // HTMLElement | null
```

#### findAll(element, marker)

`element` is a DOM element and `marker` is the value returned by `marker()`.

It returns a `NodeList`.

```js
import MyButton, { myButton } from './MyButton';
render(, container);
findAll(container, myButton); // NodeList
```