Skip to content

balavishnuvj/rtl-simple-queries

Repository files navigation

rtl-simple-queries

Simple wrapper queries for @testing-library/react


Build Status Code Coverage version downloads MIT License

All Contributors

PRs Welcome Code of Conduct

The problem

To get an element from while testing with - @testing-library/react, we have 6 variants viz. getBy, getAllBy, queryBy, queryAllBy, findBy, and findAllBy.

We have to remember which one to use when. The name of methods is entirely a familiarity thing. But just by the name of the method it bit difficult to know which one would throw error if an element is not found or which takes into account async updates.

This solution

This library wraps those methods with easier to remember APIs

Table of Contents

Installation

This module is distributed via npm which is bundled with node and should be installed as one of your project's devDependencies:

npm install --save-dev rtl-simple-queries

Usage

screen can be exported from 'rtl-simple-queries'

Addional parameters from sync query allowEmpty and allowMultiple and for async we only have one option allowMultiple

import {screen} from 'rtl-simple-queries'

screen.fetchByText(/text/, {allowEmpty: true, allowMultiple: false})
screen.fetchByText(/text/, {allowMultiple: false})
screen.fetchByText(/text/)

// async
await screen.fetchByTextAsync(/text/, {allowMultiple: true})

To use queies for scoped container we could do

// test-utils. js
function render(component, options) {
  const utils = rtlRender(component, options)
  const queries = getSimpleQueries(utils)
  return {...utils, ...renderSyncQueries}
}

// use this render instead of importing from @testing-library/react
const {fetchByText} = render(<p>text</p>)

Other Solutions

You can refer to cheatsheet as a quick reference.

Issues

Looking to contribute? Look for the Good First Issue label.

πŸ› Bugs

Please file an issue for bugs, missing documentation, or unexpected behavior.

See Bugs

πŸ’‘ Feature Requests

Please file an issue to suggest new features. Vote on feature requests by adding a πŸ‘. This helps maintainers prioritize what to work on.

See Feature Requests

Contributors ✨

Thanks goes to these people (emoji key):


Balavishnu V J

πŸ’» πŸ“– πŸš‡ ⚠️

Kent C. Dodds

πŸ€” πŸ“–

Paul Coroneos

πŸ“–

Adam Ochayon

πŸ“–

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

LICENSE

MIT