unit testing react & redux

33
Unit Testing React & Redux Dylan Kirby Slides: github.com/djkirby/react-symposium-unit-testing

Upload: craig-jolicoeur

Post on 12-Apr-2017

328 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Unit Testing React & Redux

Unit Testing React & Redux

Dylan Kirby

Slides: github.com/djkirby/react-symposium-unit-testing

Page 2: Unit Testing React & Redux

- What is unit testing, benefits- Automated javascript testing- Unit testing redux- Unit testing React components- Real world examples- Visual testing (Storybook)- Resources

Overview

Page 3: Unit Testing React & Redux

- What is unit testing, benefits- Automated javascript testing- Unit testing redux- Unit testing React components- Real world examples- Visual testing (Storybook)- Resources

Page 4: Unit Testing React & Redux

What is Unit Testing

Page 6: Unit Testing React & Redux

- What is unit testing, benefits- Automated javascript testing- Unit testing redux- Unit testing React components- Real world examples- Visual testing (Storybook)- Resources

Page 7: Unit Testing React & Redux
Page 8: Unit Testing React & Redux

setup -> exercise -> verify -> teardown

http://www.agile-code.com/blog/the-anatomy-of-a-unit-test/

Page 9: Unit Testing React & Redux

chai

Page 10: Unit Testing React & Redux

- What is unit testing, benefits- Automated javascript testing- Unit testing redux- Unit testing React components- Real world examples- Visual testing (Storybook)- Resources

Page 11: Unit Testing React & Redux

Testing redux action creators

Page 12: Unit Testing React & Redux

Testing redux reducers

Page 13: Unit Testing React & Redux

- What is unit testing, benefits- Automated javascript testing- Unit testing redux- Unit testing React components- Real world examples- Visual testing (Storybook)- Resources

Page 14: Unit Testing React & Redux

Unit Testing React Components

- Check it renders as expected given certain props/state- Check it handles interactions as expected

- React addons test utils- Enzyme

Page 15: Unit Testing React & Redux

React Addons Test Utils

- Simulate events- Render component into DOM- Shallow render

Page 16: Unit Testing React & Redux

- JavaScript Testing utility for React that makes it easier to assert, manipulate, and traverse your React Components' output

- Uses React test utilities, provides different interface to reduce boilerplate

- jQuery-like syntax

- Shallow rendering, full DOM rendering

- Guides for using w/ different test runners

Enzyme

Page 17: Unit Testing React & Redux

Chai.js assertions and convenience functions for testing React Components with enzyme

chai-enzyme

Page 18: Unit Testing React & Redux

Shallow Rendering

at(index)childAt()children()closest(selector)contains(nodeOrNodes)context([key])debug()equals(node)every(selector)everyWhere(predicate)filter(selector)filterWhere(predicate)find(selector)findWhere(predicate)first()forEach(fn)get(index)hasClass(className)html()instance()is(selector)

last()map(fn)not(selector)parent()parents()prop([key])props()reduce(fn[, initialValue])reduceRight(fn[, initialValue])render()setContext(context)setProps(nextProps)setState(nextState)shallow([options])simulate(event[, data])some(selector)someWhere(predicate)state([key])text()type()unmount()update()

Page 19: Unit Testing React & Redux

Shallow Rendering

at(index)childAt()children()closest(selector)contains(nodeOrNodes)context([key])debug()equals(node)every(selector)everyWhere(predicate)filter(selector)filterWhere(predicate)find(selector)findWhere(predicate)first()forEach(fn)get(index)hasClass(className)html()instance()is(selector)

last()map(fn)not(selector)parent()parents()prop([key])props()reduce(fn[, initialValue])reduceRight(fn[, initialValue])render()setContext(context)setProps(nextProps)setState(nextState)shallow([options])simulate(event[, data])some(selector)someWhere(predicate)state([key])text()type()unmount()update()

Page 20: Unit Testing React & Redux

Shallow Rendering

at(index)childAt()children()closest(selector)contains(nodeOrNodes)context([key])debug()equals(node)every(selector)everyWhere(predicate)filter(selector)filterWhere(predicate)find(selector)findWhere(predicate)first()forEach(fn)get(index)hasClass(className)html()instance()is(selector)

last()map(fn)not(selector)parent()parents()prop([key])props()reduce(fn[, initialValue])reduceRight(fn[, initialValue])render()setContext(context)setProps(nextProps)setState(nextState)shallow([options])simulate(event[, data])some(selector)someWhere(predicate)state([key])text()type()unmount()update()

Page 21: Unit Testing React & Redux

Shallow Rendering

at(index)childAt()children()closest(selector)contains(nodeOrNodes)context([key])debug()equals(node)every(selector)everyWhere(predicate)filter(selector)filterWhere(predicate)find(selector)findWhere(predicate)first()forEach(fn)get(index)hasClass(className)html()instance()is(selector)

last()map(fn)not(selector)parent()parents()prop([key])props()reduce(fn[, initialValue])reduceRight(fn[, initialValue])render()setContext(context)setProps(nextProps)setState(nextState)shallow([options])simulate(event[, data])some(selector)someWhere(predicate)state([key])text()type()unmount()update()

Page 22: Unit Testing React & Redux

Shallow Rendering

at(index)childAt()children()closest(selector)contains(nodeOrNodes)context([key])debug()equals(node)every(selector)everyWhere(predicate)filter(selector)filterWhere(predicate)find(selector)findWhere(predicate)first()forEach(fn)get(index)hasClass(className)html()instance()is(selector)

last()map(fn)not(selector)parent()parents()prop([key])props()reduce(fn[, initialValue])reduceRight(fn[, initialValue])render()setContext(context)setProps(nextProps)setState(nextState)shallow([options])simulate(event[, data])some(selector)someWhere(predicate)state([key])text()type()unmount()update()

Page 23: Unit Testing React & Redux

Shallow Rendering

at(index)childAt()children()closest(selector)contains(nodeOrNodes)context([key])debug()equals(node)every(selector)everyWhere(predicate)filter(selector)filterWhere(predicate)find(selector)findWhere(predicate)first()forEach(fn)get(index)hasClass(className)html()instance()is(selector)

last()map(fn)not(selector)parent()parents()prop([key])props()reduce(fn[, initialValue])reduceRight(fn[, initialValue])render()setContext(context)setProps(nextProps)setState(nextState)shallow([options])simulate(event[, data])some(selector)someWhere(predicate)state([key])text()type()unmount()update()

Page 24: Unit Testing React & Redux

Shallow Rendering

at(index)childAt()children()closest(selector)contains(nodeOrNodes)context([key])debug()equals(node)every(selector)everyWhere(predicate)filter(selector)filterWhere(predicate)find(selector)findWhere(predicate)first()forEach(fn)get(index)hasClass(className)html()instance()is(selector)

last()map(fn)not(selector)parent()parents()prop([key])props()reduce(fn[, initialValue])reduceRight(fn[, initialValue])render()setContext(context)setProps(nextProps)setState(nextState)shallow([options])simulate(event[, data])some(selector)someWhere(predicate)state([key])text()type()unmount()update()

Page 25: Unit Testing React & Redux

at(index)contains(nodeOrNodes)childAt()children()closest(selector)context([key])debug()detach()every(selector)everyWhere(predicate)filter(selector)filterWhere(predicate)find(selector)findWhere(predicate)first()forEach(fn)get(index)hasClass(className)html()instance()is(selector)last()

Full DOM Rendering - mount()

map(fn)mount()not(selector)parent()parents()prop([key])props()reduce(fn[, initialValue])reduceRight(fn[, initialValue()])ref(refName)render()setContext(context)setProps(nextProps)setState(nextState)simulate(event[, data])some(selector)someWhere(predicate)state([key])text()type()unmount()update()

Page 26: Unit Testing React & Redux

- What is unit testing, benefits- Automated javascript testing- Unit testing redux- Unit testing React components- Real world examples- Visual testing (Storybook)- Resources

Page 27: Unit Testing React & Redux

Examples

Page 28: Unit Testing React & Redux

Examples

Page 29: Unit Testing React & Redux

Examples

Page 30: Unit Testing React & Redux

Examples

Page 31: Unit Testing React & Redux

- What is unit testing, benefits- Automated javascript testing- Unit testing redux- Unit testing React components- Real world examples- Visual testing (Storybook)- Resources

Page 32: Unit Testing React & Redux

- What is unit testing, benefits- Automated javascript testing- Unit testing redux- Unit testing React components- Real world examples- Visual testing (Storybook)- Resources