• Stars
    star
    495
  • Rank 88,974 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 9 years ago
  • Updated about 8 years ago

Reviews

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

Repository Details

a redux middleware to automatically generate tests for reducers through ui interaction

Redux Test Recorder

NOTE YOUR STATE TREE MUST BE SERIALIZABLE

Redux test recorder is a redux middleware for automatically generating tests for your reducers based on the actions in your app. Currently I've written redux-test-recorder-react a component to provide a gui for recording tests in react but I'm hopeful recording components for other frameworks can be created in the future.

Build Status

Also take a look at our latest build which currently runs a test generated using this module by taking advantage of the eval command. For a better idea of what is going on, you can take a look at the test file here.

Install

npm install redux-test-recorder --save-dev

Use

First set up your store utilizing the exported middleware from redux-test-recorder. Export the props included with redux-test-recorder at this time as well.

import reduxRecord from 'redux-test-recorder';
const reducer = (state = initState, { type, payload }) => {
  let newState;
  switch (type) {
    case 'INCREMENT':
      newState = state + 1;
      break;
    case 'DECREMENT':
      newState = state - 1;
      break;
    default:
      newState = state;
  }
  return newState;
}

const record = reduxRecord({reducer});
export const store = createStore(reducer, applyMiddleware(record.middleware));
export const recordProps = record.props;

Then, if you are using with React you can install redux-test-recorder-react and import the recordProps exported by the instantiation of the middleware and pass those into the record component.

import {store, recordProps } from './store';
import TestRecorder from 'redux-test-recorder-react';
const Counter = ({count, dispatch}) => {
  return (
    <div>
      <button onClick={() => dispatch(increment())}>+</button>
      <h1>{count}</h1>
      <button onClick={() => dispatch(decrement())}>-</button>
    </div>
  );
}

const ConnectedCounter = connect(state => {
  return {count: state};
})(Counter);
const Root = () => {
  return (
    <div>
      <Provider store={store}>
        <div><ConnectedCounter /><TestRecorder {...recordProps} /></div>
      </Provider>
    </div>;
};

This will allow you to generate tests on your reducer with a record button in the bottom right corner.

Args

  • reducer - the root reducer of your redux app, used in the generated test.
  • includeReducer - a boolean value, if true, a stringified version of your reducer will be incuded in your generated test, if false, a note to import reducer for testing will be added. defaults to false. Useful when used to generate actual tests so you can test updated functionality.
  • stateKey(optional) - if instead of recording the whole state you only want to record a specific piece, pass that here (useful with actionSubset prop explained next). To get to a -nth level child use dots, as in: api.login.errors.
  • actionSubset(optional) - allows you to record against a subset of actions instead of all actions. Useful combined with stateKey to test a single reducer.
  • equality(optional) - a function used to determine if the reducer returned correct state. Receives result of the reducer call and nextState returned during the flow of the application (note, this api is in flux). deafults to ===. This argument can also be a string. This is useful if you want to call a function you will include in your test file, since calling external functions will not properly stringify that external function.
  • imports(optional)` - a string argument where you can pass in other modules that you would like included iny our test file. Useful if you want to reference external functions in your equality check.
  • testLib(optional) - defaults to tape. Currently supports tape, ava, and mocha, jest, and redux-ava. You can also optionally supply a function to this argument to generate your own tests. Will receive state, actions, imports, reducer, equalityFunction as arguments and expects return type to be a string containing your test contents.
  • numTestsToSave(optional, defaults to 5) - number of previous tests that will be accessible in the panel when tests are being displayed. Higher number = newer.

Create Your Own Testing Interface

If you're not satisfied with the built in testing interface or would like to experiment with something different, it's relatively straightforward. The return of the exported function is an object with two keys middleware and props. The middleware key contains, well, the middleware, props contains information for accessing the current state of the tests. These include startRecord, stopRecord , which are functions that start and stop test recording. createNewTest which creates and then returns a new test, hideTest , which resets all values to initial value, and most importantly listen , a function that takes a function and calls listeners any time any values related to recording status or generated tests is changed. More documentation on this coming soon.

You can take a look at what creating your own interface looks like here - here for what this looks like for the tape implementation.

More Repositories

1

react-native-syntax-highlighter

a syntax highlighter for react native using https://github.com/conorhastings/react-syntax-highlighter under the hood
JavaScript
169
star
2

routeql

backend agnostic, graphql style colocation of data requests in React
JavaScript
122
star
3

react-emoji-react

a clone of slack emoji reactions in react
JavaScript
120
star
4

use-reducer-with-side-effects

JavaScript
88
star
5

react-syntax-highlighter-virtualized-renderer

a way to use react-virtualized to render lines of code for react-syntax-highlighter
JavaScript
31
star
6

react-component-demo

A React Component To make live editable demos of other React Components
JavaScript
24
star
7

remove-flow-types-loader

JavaScript
20
star
8

react-close-on-escape

higher order component to close other react component on press of escape key (or fire whatever function you want but probably it should close)
JavaScript
14
star
9

react-thermometer

a simple thermometer gauge component (does not tell temperature)
JavaScript
12
star
10

apply-loader-after-first-build-webpack-plugin

a webpack plugin that allows you to dynamically add a loader after the initial build has occurred (useful when using watch) and want to do something like lint only changed and new files
JavaScript
5
star
11

redux-test-recorder-react

component providing a gui for redux-test-recorder in react applications
JavaScript
4
star
12

react-code-window

JavaScript
3
star
13

put-stuff-on-website

JavaScript
3
star
14

whitelist-object

a small module for reducing an object to only the desired keys
JavaScript
3
star
15

match-default

JavaScript
3
star
16

github-stars-elm

get total github stars for user , learning elm by writing elm
Elm
3
star
17

redux-session-storage

redux middleware for recording redux actions for a particular session to session storage
JavaScript
3
star
18

react-one-of

take in a set of components and make sure only one is rendered.
JavaScript
3
star
19

react-allowed

a react component that calls a function prop isAllowed to determine whether or not to render children
JavaScript
3
star
20

get-emoji

give emoji name get emoji image
JavaScript
2
star
21

storybook-addon-react-profiler

JavaScript
2
star
22

redux-connected-proptypes

an interface for defining React component props using redux, react-redux and PropTypes
JavaScript
2
star
23

react-async-branch

a component that will asynchronously load, one of two components based on a condition
JavaScript
2
star
24

react-test-generator

generate basic sanity check tests for react components, major WIP, ready to be used by someone, who knows when?
JavaScript
2
star
25

netflix-originals

a react calendar app for netflix original movie releases
JavaScript
2
star
26

react-expiration

A simple component that uses a render callback to expose if a the current date is past a user set expirationDate
JavaScript
2
star
27

clojurescript

learning clojurescript
Clojure
1
star
28

object-value-equality

JavaScript
1
star
29

state-reducers-hook

manage state by dispatching changes using react hooks
TypeScript
1
star
30

AgeComparer

Compare your age to the ages to the average age of professional athletes and become depressed about how relatively old you are in comparison.
JavaScript
1
star
31

object-description-filter

filter nested object based on descriptor object
JavaScript
1
star
32

version-upgrade

don't manually go through all of the different places your node version is stored, let this do it for you!
JavaScript
1
star
33

highlight-code

a javascript module to add syntax highlighting to a code string, uses inline styles for encapsulation
JavaScript
1
star
34

is-today-the-day-that-the-birth-of-jesus-is-celebrated

JavaScript
1
star
35

elm-md

a markdown editor written in elm
Elm
1
star
36

react-app-init

an opinionated, declarative way to start a react/redux app.
JavaScript
1
star
37

use-worker-hook

JavaScript
1
star
38

react-style-px-suffix-codemod

append px to shorthand values in style objects in react in prep for react 15 warning
JavaScript
1
star
39

GIF-Framer

takes in an animated gif and returns all the frames as individual images
JavaScript
1
star
40

highlight.js-js-styles

all of highlight.js css stylesheets as javascript objects
JavaScript
1
star