npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

react-wiring-library

v1.1.1

Published

A library for making react-testing-library-wiring

Downloads

27

Readme

Read The Docs

Build Status Code Coverage version downloads MIT License

PRs Welcome

Watch on GitHub Star on GitHub

The Problem

You've tried react-testing-library and love its core API, but have struggled with getting to full coverage on complicated components. Testing isolated interactions is easy enough, but when multiple parts of a component change at once, or you have to take complex sequencing into account, your tests become significantly harder to write and maintain.

The Solution

By letting you describe the relevant structure of your component tree in advance, react-wiring-library makes fully testing complicated components not only possible, but intuitive and scalable.

The wiring tree's structure lets you fully wrangle your interaction code, generate developer friendly snapshots, and easily customize react-testing-library's api to the specifics of your project.

Installation

yarn add --dev react-wiring-library
yarn add --dev @testing-library/react

Prerequisites

react-wiring-library is built off of react-testing-library, so a basic familiarity with that framework is required. In particular, make sure to take a look at the different queries that are available, and how they work.

Run The Example

Copy this test into your project and run it.

import {getRender} from 'react-wiring-library'
import React, {useState} from 'react'

const Todo = ({name}) => {
  const [isCompleted, setIsComplete] = useState(false)
  return (
    <div>
      <input
        data-testid="checkbox"
        onClick={() => setIsComplete(prev => !prev)}
        type="checkbox"
      />
      {!isCompleted && <span>{name}</span>}
    </div>
  )
}

const TodoList = ({todos}) => {
  return (
    <ul data-testid="todo-list">
      {todos.map((todo, index) => (
        <li data-testid="todo" key={index}>
          <Todo name={todo.name} />
        </li>
      ))}
    </ul>
  )
}

const wiringTree = {
  children: {
    // query will be findTodoList and returned object will be todoList

    todoList: {
      // findTodoList => findByTestId('todo-list')
      findValue: 'todo-list',
      // combine the child `todoStrings` into a single string with each todo on a new line
      serialize: (val, {todoStrings}) => {
        return todoStrings.map(string => `- ${string}`).join('\n')
      },
      children: {
        todo: {
          // findTodo = ({ index }) => findAllbyTestId('todo')[index]
          isMultiple: true,
          findValue: 'todo',
          // makes this possible
          // { toggle } = await findTodo({ index: 0 }}
          extend: (val, {findCheckbox}) => {
            return {
              toggle: async () => {
                const {click} = await findCheckbox()
                click()
              },
            }
          },
          // combine the serialized check box with the text content of the 'todo' DOM node
          // - ✅ Todo One
          serialize: (val, {checkboxString}) => {
            return `${checkboxString}  ${val ? val.textContent : ''}`
          },
          children: {
            checkbox: {
              //findCheckbox = () => findByTestId('checkbox')
              findValue: 'checkbox',
              // convert the checkbox DOM node into the appropriate emoji
              serialize: val => (val.checked ? '✅' : '⬜️'),
            },
          },
        },
      },
    },
  },
}

const render = getRender(wiringTree)

describe('TodoList', () => {
  test('should render a list of todos', async () => {
    const {findTodoList} = render(
      <TodoList
        todos={[
          {
            name: 'Todo One',
          },
          {
            name: 'Todo Two',
          },
        ]}
      />,
    )
    const {
      todoList, // the dom element returned by `findByTestId('todo-list')
      findTodo, // findByTestId('todo-list')
    } = await findTodoList()
    // -◻️ Todo One
    // -◻️ Todo Two
    expect(todoList).toMatchSnapshot('on initial render')
    const {
      toggle, // the function created in extend
    } = await findTodo({
      index: 0, // get the first todo, could also pass { filter: (todo) => // filter todos to one }
    })
    await toggle()
    // -✅ Todo One
    // -◻️ Todo Two
    expect(todoList).toMatchSnapshot('after clicking first todo')
  })
})

Get a Feel

First, take a look at the snapshots generated after the tests run. If you're using VSCode, we'd highly recommend adding snapshot-tools to make it easier work with snapshots.

Here's a few things you could try to familiarize yourself with the basics.

  • Comment out the toggle call on line 143 and see how the tests fail.
  • Add a new assert for clicking on the second todo.
  • Change the values returned by the serializers and note how the tests fail.
  • Change the data-testid attribute on todo to something else, and note the error that gets thrown.
  • Change the key of todoList to just list and update everything that's dependent on the change.

Next Steps

Now that you've got the lay of the land, check out the basic tutorials

LICENSE

MIT