Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Testing React Components
Search
Andrei Picus
April 28, 2015
Programming
90
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
Security issues being discussed on Web Platforms
petamoriken
0
1.4k
SONY CISC-NEWS NWS-1750 + NWB-225 フレームバッファの NetBSD/news68k ドライバ実装 / OSC2026Hiroshima
tsutsui
0
160
Turning Architecture into Unit Tests in the AI Era (NSSpain XIV)
steliosf
PRO
1
120
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
510
AI が書く Go コードの品質を劇的に向上させる Linter: “declscope”
mpyw
0
450
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
4
340
Are APIs Still Relevant in the AI Era?
soyuka
0
370
iOSDC2026登壇資料.pdf
riofujimon
0
200
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
1
670
Verilogで学ぶCPU自作入門.pdf
uyuki234
7
3.8k
海上で動くGoサーバー: goroutineとchannelでさばく航行データストリーム
atsuki_seo
0
1k
世界の中心で、AI(App Intents)をさけぶ ー App Intents中心設計の実践ガイド
touyou
0
730
Featured
See All Featured
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
The agentic SEO stack - context over prompts
schlessera
0
950
[RailsConf 2023] Rails as a piece of cake
palkan
59
7k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
250
It's Worth the Effort
3n
188
29k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
Designing Experiences People Love
moore
143
24k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
VelocityConf: Rendering Performance Case Studies
addyosmani
331
25k
Producing Creativity
orderedlist
PRO
348
41k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
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