• Stars
    star
    248
  • Rank 163,560 (Top 4 %)
  • Language
    JavaScript
  • Created about 9 years ago
  • Updated over 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Chainable, easy to read, React testing library

##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

More Repositories

1

react-fetch

Component wrapper for isomorphic-fetch, passes response to children
JavaScript
120
star
2

ReactJSNews

Everything from reactjsnews.com :)
CSS
119
star
3

scheduler

A pure React implementation of a drag and drop scheduler
JavaScript
115
star
4

modal

a react modal that lets you easily customize the css
JavaScript
92
star
5

legible

the cleanest way to make http requests in js / node
JavaScript
47
star
6

image

an img tag, with lazy loading
JavaScript
47
star
7

redux-save

Full featured middleware for handling async actions and automagically saving data (For RN & Web)
JavaScript
32
star
8

table

the simplest, most hands-off table helper
JavaScript
31
star
9

context-menu

Replace the native right-click context menu with your own info
JavaScript
30
star
10

popover

Popovers for React
JavaScript
29
star
11

immutable-proxy

JavaScript
28
star
12

redux-tutorial

code from a tutorial
JavaScript
26
star
13

forms

Form and schema builder for React **not ready for use yet**
JavaScript
16
star
14

tabs

tabs, done right
JavaScript
14
star
15

fader

a HOC that will fade a component in and out
JavaScript
11
star
16

override-decorator

Simple override decorator for ES7
JavaScript
4
star
17

enzymed

decorators and automatic testing for enzyme by airbnb
JavaScript
3
star
18

rubyfill

An (opinionated) collection of Ruby'ish polyfills for JavaScript
JavaScript
2
star
19

accordion

A simple accordion component based on Thoughbot's accordion in refills
JavaScript
1
star
20

cards

A simple component to render cards from the Thoughtbot Refills library
JavaScript
1
star
21

id

a small utility for generating unique id's (useful for React)
JavaScript
1
star