Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Testing React Components
Search
Andrei Picus
April 28, 2015
Programming
83
1
Share
Testing React Components
Andrei Picus
April 28, 2015
Other Decks in Programming
See All in Programming
KagglerがMixSeekを触ってみた
morim
0
360
PHPで TLSのプロトコルを実装してみる
higaki_program
0
610
Tamach-sre-3_ANDPAD-shimaison93
mane12yurks38
0
210
Coding at the Speed of Thought: The New Era of Symfony Docker
dunglas
0
3.6k
コードレビューをしない選択 #でぃーぷらすトウキョウ
kajitack
3
1.2k
S3ストレージクラスの「見える」「ある」「使える」は全部違う ─ 体験から見た、仕様の深淵を覗く
ya_ma23
0
1.2k
PHP でエミュレータを自作して Ubuntu を動かそう
m3m0r7
PRO
2
150
Symfony + NelmioApiDocBundle を使った スキーマ駆動開発 / Schema Driven Development with NelmioApiDocBundle
okashoi
0
250
条件判定に名前、つけてますか? #phperkaigi #c
77web
2
890
Rethinking API Platform Filters
vinceamstoutz
0
4.2k
Smarter Angular mit Transformers.js & Prompt API
christianliebel
PRO
1
110
存在論的プログラミング: 時間と存在を記述する
koriym
5
680
Featured
See All Featured
How STYLIGHT went responsive
nonsquared
100
6k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
A designer walks into a library…
pauljervisheath
210
24k
Building an army of robots
kneath
306
46k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.8k
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
97
The Anti-SEO Checklist Checklist. Pubcon Cyber Week
ryanjones
0
110
Heart Work Chapter 1 - Part 1
lfama
PRO
5
35k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
610
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.1k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
330
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
310
Transcript
Testing React components Andrei Picus
Who?
[email protected]
github.com/NiGhTTraX Started with JS 10 years ago In
love with TDD
What? 1. How to write good unit tests. 2. How
to unit test a React component. 3. How to write the tests. 4. How to run the tests.
What’s a unit test? A test, whose result (pass or
fail) depends on the correctness of only one interesting piece of behaviour.
Testing a client-server relation Do I ask the interface the
right questions? Do I correctly handle all of the responses? Do I respond correctly to the questions? Do I try to respond to the questions? Contract C S INTERFACE Collaboration
The interface • lifecycle methods • callbacks • public methods
• DOM event handlers
Inter-component communication Do I send the correct props? C Do
I call the right callbacks? Do I react correctly to the new props? Do I implement the callback? API P
Intra-component communication Do I compute the correct state? render() Component
Do I call the right callbacks? Do I render the right things? Do I implement the callback? API
Mocking component methods Component = React.createClass({}) Component._class.prototype .__reactAutoBindMap[method] // cb
.constructor[method] // static [method] // public
Mocking component methods You need to do it on the
component’s class before rendering.
Mocking children • Mock the NodeJS module. • Have a
mixin/lib that loads a component and mock that. • Shallow render.
SinonJS sinon.spy(Component, ‘updateFoo’) sinon.stub(Component, ‘render’) .returns(null) sinon.stub(Component, ‘loadChild’) .returns(null)
sinon-chai expect(stub) .to.have.been.called.and .to.have.been.calledWith(42);
Setup a component Put it into a certain state and
then assert stuff on it. Don’t do two actions that mutate state in a single test => create another test where you load the component in the 2nd state
Running the tests Browserify (webpack doesn’t work with sinon) MochaJS
ChaiJS SinonJS Karma Istanbul
Example config NiGhTTraX/react-test-buffet
That’s all folks