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

legit-tests

v1.1.2

Published

a chainable testing library for React

Downloads

31

Readme

##Legit Tests

This is a super friendly testing library for React, inspired by express middleware, it's easily extendable. Why did I make this when you can use React's Test Utils? Because who likes typing out scryRenderedDOMComponentsWithTag and the other method names on there. Not only that, but setting up the render process is also a hassle.

###Install

npm install legit-tests --save

##Example

import Test from 'legit-tests'
//or
import Test from 'legit-tests/no-dom' //don't include jsdom

import { expect } from 'chai'
import sinon from 'sinon'
import TestComponent from './TestComponent'

let spy = sinon.spy()


//Calling a prop
Test(<TestComponent onClick={spy}/>)
.find('button')
.simulate({method: 'click', element: 'button'})
.test(() => {
  expect(spy.called).to.be.true
})

//finding an element
Test(<TestComponent/>)
.find('.box')
.elements('.box', (box) => {
  expect(box.props.children).to.be.equal('found me!')
})

##Middleware

Current list of Middleware

You can write middleware to do anything you repeatedly use. You can pass .use a function, along with an object that it will take in. Each function will be injected with the current instance which includes:

  • component - the actual component itself
  • instance - the rendered component instance
  • helpers - an array you can add on to with data for the end function

Example:

  • See mixin below, this syntax may soon be deprecated

This is the setState function used above.


Test(<TestComponent onClick={spy}/>)
.use(SetState, {})

...

export default function setState(state){
  this.instance.setState(state)
}

##test

The .test function will be given the component instance and the helpers array. You can use a regular function to reference this or an arrow function:

.test(({helpers, instance}) => { ... })
.test(function() {
  //this.instance, this.helpers
})

##element

Use .element if you're just testing an element you found with the .find method. The syntax is a little smaller:

Test(<TestComponent/>)
.find('.box')
.element(box => {
  expect(box.props.children).to.be.equal('found me!')
})
//or specify the element

.find('.box')
.find('div')
.element('.box', box => {
  expect(box.props.children).to.be.equal('found me!')
})

##mixin

Use .mixin if you want to add new middleware as methods to Test. This gives a more natural way of using middleware:

// In this example, CustomFind middleware was added to Test by mixin
// and used if as it was a method on Test itself.

Test(<TestComponent />)
.mixin({
  customFind: CustomFind
})
.customFind('cells', 'table td')
.element('cells', cells => {
  expect(cells.length).to.be.equal(10)
})

##DOM rendering Shallow -- uses React shallow rendering (no DOM)

Test(<TestComponent onClick={spy}/>, {shallow: true})
.find('button')
.simulate({method: 'click', element: 'button'})
.test(() => {
  expect(spy.called).to.be.true
})

Normal -- React render into document fragment

Test(<TestComponent onClick={spy}/>)
.find('button')
.simulate({method: 'click', element: 'button'})
.test(() => {
  expect(spy.called).to.be.true
})

fullDOM -- ReactDOM render into document.body.div of jsdom

Test(<section />, {fullDOM: true})
.test(function() {
  expect(global.window.document.querySelector('section'))
  .to.be.okay
})
.clean() // restores the document.body to empty

You can see more examples in the tests directory.

##Testing Alt Stores

You can now test Alt stores using the same API.

import TestStore from 'legit-tests/alt/store'

TestStore(MyStore, MyActions)
.setInitialState({ todos: todos })
.addTodo({ title: "Get Beer", complete: false })
.test(({ state }) => {
  expect(state.todos).to.eql(expected);
})

You can see the full documentation on the Wiki