Testing Library

Testing Library

  • Docs
  • Recipes
  • Help
  • Blog

›Reason Testing Library

Getting Started

  • Introduction
  • Guiding Principles
  • Using Fake Timers

Frameworks

    DOM Testing Library

    • Introduction
    • Install
    • Example
    • Setup
    • Queries
    • Firing Events
    • Async Utilities
    • Helpers
    • Configuration
    • FAQ
    • Cheatsheet

    React Testing Library

    • Introduction
    • Example
    • Setup
    • API
    • Migrate from Enzyme
    • FAQ
    • Cheatsheet

    Reason Testing Library

    • Introduction
    • Examples

    React Native Testing Library

    • Introduction
    • Example
    • Setup

    Vue Testing Library

    • Introduction
    • Examples
    • Setup
    • API
    • Cheatsheet
    • FAQ

    Marko Testing Library

    • Introduction
    • Setup
    • API

    Angular Testing Library

    • Introduction
    • Examples
    • API

    Preact Testing Library

    • Introduction
    • Example
    • API
    • Learn

    Svelte Testing Library

    • Introduction
    • Setup
    • Example
    • API
  • Cypress Testing Library
  • Puppeteer Testing Library
  • Testcafe Testing Library
  • Nightwatch Testing Library

Ecosystem

  • user-event
  • jest-dom
  • bs-jest-dom
  • jest-native
  • react-select-event
  • eslint-plugin-testing-library
  • eslint-plugin-jest-dom
  • riot-testing-library
  • jasmine-dom
Edit

Examples

You can find more bs-dom-testing-library examples at wyze/bs-dom-testing-library/src/__tests__.

You can find more bs-react-testing-library examples at wyze/bs-react-testing-library/src/__tests__.

React Testing Library

/* Component_test.re */

open Jest;
open Expect;
open ReactTestingLibrary;

test("Component renders", () =>
  <div style=ReactDOMRe.Style.make(~color="rebeccapurple", ())>
    <h1> {ReasonReact.string("Heading")} </h1>
  </div>
  |> render
  |> container
  |> expect
  |> toMatchSnapshot
);

DOM Testing Library

The below examples use bs-webapi to help with typings and creating events.

getByText

/* __tests__/example_test.re */
open Jest;
open DomTestingLibrary;
open Expect;

type parser;

[@bs.new]
external domParser : unit => parser = "DOMParser";

[@bs.send.pipe : parser]
external parseFromString : ( string, [@bs.as "text/html"] _) => Dom.element = "";

[@bs.get]
external body : Dom.element => Dom.element = "";

[@bs.get]
external firstChild : Dom.element => Dom.element = "";

let div = domParser()
  |> parseFromString({j|
      <div>
        <b title="greeting">Hello,</b>
        <p data-testid="world"> World!</p>
        <input type="text" placeholder="Enter something" />
        <input type="text" value="Some value" />
        <img src="" alt="Alt text" />
      </div>
    |j})
  |> body
  |> firstChild;

describe("getByText", () => {
  test("works with string matcher", () => {
    let actual = div |> getByText(~matcher=`Str("Hello,"));

    expect(actual) |> toMatchSnapshot;
  });

  test("works with regex matcher", () => {
    let actual = div |> getByText(~matcher=`RegExp([%bs.re "/\\w!/"]));

    expect(actual) |> toMatchSnapshot;
  });

  test("works with function matcher", () => {
    let matcher = ( _text, node ) => (node |> tagName) === "P";
    let actual = div |> getByText(~matcher=`Func(matcher));

    expect(actual) |> toMatchSnapshot;
  });
});

FireEvent

open Jest;
open DomTestingLibrary;
open Expect;

describe("FireEvent", () => {
  test("click works", () => {
    open Webapi.Dom;

    let node = document |> Document.createElement("button");
    let spy = JestJs.inferred_fn();
    let fn = spy |> MockJs.fn;
    let clickHandler = _ => [@bs] fn("clicked!") |> ignore;

    node |> Element.addEventListener("click", clickHandler);

    FireEvent.click(node);

    expect(spy |> MockJs.calls) |> toEqual([|"clicked!"|]);
  });

  test("change works", () => {
    open Webapi.Dom;

    let node = document |> Document.createElement("input");
    let spy = JestJs.inferred_fn();
    let fn = spy |> MockJs.fn;
    let changeHandler = _ => [@bs] fn("changed!") |> ignore;
    let event = Event.makeWithOptions("change", { "target": { "value": "1" } });

    node |> Element.addEventListener("change", changeHandler);

    FireEvent.change(node, event);

    expect(spy |> MockJs.calls) |> toEqual([|"changed!"|]);
  });
});
Last updated on 2/11/2019
← IntroductionIntroduction →
  • React Testing Library
  • DOM Testing Library
    • getByText
  • FireEvent
Testing Library
Docs
Getting StartedExamplesAPIHelp
Community
BlogStack OverflowDiscord
More
StarGitHubEdit Docs on GitHubHosted by Netlify
Copyright © 2018-2020 Kent C. Dodds and contributors