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
88
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Testing React Components
Andrei Picus
April 28, 2015
Other Decks in Programming
See All in Programming
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
140
PHPで使える日時の表現と、その知り方 #frontend_phpcon_do
o0h
PRO
0
250
Spring Security 実践 ─ GraphQL APIで実務に役立つ 認証・認可 を学ぶ
wagyu
0
250
The NotImplementedError Problem in Ruby
koic
1
830
Vue × Nuxt × Oxc どこまで使える?実運用の現在地
andpad
0
270
そのテスト、説明できますか?~LWテスト戦略FW~のご紹介
nakahara
0
150
Javaの型とAI時代に型が大事な理由 / java types and type in AI era
kishida
2
140
Make SRE Operations Easier with Azure SRE Agent
kkamegawa
0
6.7k
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
590
JJUG CCC 2026 Spring: JSpecify で実現する Kotlin フレンドリーな Java API 設計
ternbusty
1
180
The ROI of Quarkus for Spring Boot Applications
hollycummins
0
120
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
160
Featured
See All Featured
Being A Developer After 40
akosma
91
590k
Efficient Content Optimization with Google Search Console & Apps Script
katarinadahlin
PRO
1
630
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
200
The Illustrated Children's Guide to Kubernetes
chrisshort
51
52k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
1
3.6k
Building Applications with DynamoDB
mza
96
7.1k
Utilizing Notion as your number one productivity tool
mfonobong
4
320
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
201
75k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
340
Designing for Timeless Needs
cassininazir
1
260
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