GithubHelp home page GithubHelp logo

simplymichael / user-event Goto Github PK

View Code? Open in Web Editor NEW

This project forked from testing-library/user-event

0.0 1.0 0.0 1014 KB

πŸ• Simulate user events for react-testing-library

License: MIT License

JavaScript 100.00%

user-event's Introduction

user-event

dog

Simulate user events for react-testing-library.



Build Status Maintainability Test Coverage All Contributors

The problem

From testing-library/dom-testing-library#107:

[...] it is becoming apparent the need to express user actions on a web page using a higher-level abstraction than fireEvent

The solution

user-event tries to simulate the real events that would happen in the browser as the user interacts with it. For example userEvent.click(checkbox) would change the state of the checkbox.

The library is still a work in progress and any help is appreciated.

Installation

With NPM:

npm install @testing-library/user-event --save-dev

With Yarn:

yarn add @testing-library/user-event --dev

Now simply import it in your tests:

import userEvent from "@testing-library/user-event";

// or

var userEvent = require("@testing-library/user-event");

API

click(element)

Clicks element, depending on what element is it can have different side effects.

import React from "react";
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

test("click", () => {
  const { getByText, getByTestId } = render(
    <div>
      <label htmlFor="checkbox">Check</label>
      <input id="checkbox" data-testid="checkbox" type="checkbox" />
    </div>
  );

  userEvent.click(getByText("Check"));
  expect(getByTestId("checkbox")).toHaveAttribute("checked", true);
});

dblClick(element)

Clicks element twice, depending on what element is it can have different side effects.

import React from "react";
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

test("double click", () => {
  const onChange = jest.fn();
  const { getByTestId } = render(
    <input type="checkbox" id="checkbox" onChange={onChange} />
  );
  const checkbox = getByTestId("checkbox");
  userEvent.dblClick(checkbox);
  expect(onChange).toHaveBeenCalledTimes(2);
  expect(checkbox).toHaveProperty("checked", false);
});

type(element, text, [options])

Writes text inside an <input> or a <textarea>.

import React from "react";
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

const { getByTestId } = test("click", () => {
  render(<textarea data-testid="email" />);
});

userEvent.type(getByTestId("email"), "Hello, World!");
expect(getByTestId("email")).toHaveAttribute("value", "Hello, World!");

If options.allAtOnce is true, type will write text at once rather than one character at the time. false is the default value.

options.delay is the number of milliseconds that pass between two characters are typed. By default it's 0. You can use this option if your component has a different behavior for fast or slow users.

selectOptions(element, values)

Selects the specified option(s) of a <select> or a <select multiple> element.

import React from "react";
import { render } from "@testing-library/react";
import userEvent from "@testing-library/user-event";

const { getByTestId } = render(
  <select multiple data-testid="select-multiple">
    <option data-testid="val1" value="1">
      1
    </option>
    <option data-testid="val2" value="2">
      2
    </option>
    <option data-testid="val3" value="3">
      3
    </option>
  </select>
);

userEvent.selectOptions(getByTestId("select-multiple"), ["1", "3"]);

expect(getByTestId("val1").selected).toBe(true);
expect(getByTestId("val2").selected).toBe(false);
expect(getByTestId("val3").selected).toBe(true);

The values parameter can be either an array of values or a singular scalar value.

Contributors

Thanks goes to these wonderful people (emoji key):

Giorgio Polvara
Giorgio Polvara

πŸ› πŸ’» πŸ“– πŸ€” πŸš‡ πŸ‘€ ⚠️
Weyert de Boer
Weyert de Boer

πŸ’» ⚠️
Tim Whitbeck
Tim Whitbeck

πŸ› πŸ’»
MichaΓ«l De Boey
MichaΓ«l De Boey

πŸ“–
Michael Lasky
Michael Lasky

πŸ’» πŸ“– πŸ€”
Ahmad Esmaeilzadeh
Ahmad Esmaeilzadeh

πŸ“–
Caleb Eby
Caleb Eby

πŸ’» πŸ›
AdriΓ  Fontcuberta
AdriΓ  Fontcuberta

πŸ› ⚠️ πŸ’»
Sky Wickenden
Sky Wickenden

πŸ› πŸ’»
Bodnar Bogdan
Bodnar Bogdan

πŸ› πŸ’»
Zach Perrault
Zach Perrault

πŸ“–
Ryan Stelly
Ryan Stelly

πŸ“–
Ben Monro
Ben Monro

πŸ’»

This project follows the all-contributors specification. Contributions of any kind welcome!

user-event's People

Contributors

gpx avatar greenkeeper[bot] avatar afontcu avatar calebeby avatar kt-weyert avatar benmonro avatar bogdanbodnar avatar dhalton avatar michaeldeboey avatar flgmwt avatar skywickenden avatar michaellasky avatar

Watchers

James Cloos 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.