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
関東Kaggler会_NVIDIA_Nemotron_コンペ_振り返り
rick_ds
0
260
Cloudflare is Agents
chimame
0
130
Japan Community Day at Kubecon + CloudNativeCon Japan 2026: Learning Container Privilege Control by Building My Own Low-Level Container Runtime
ternbusty
1
130
ルールを書いて終わらせないハーネスエンジニアリング
yug1224
4
1.8k
【SRE NEXT 2026 Lunch Session】一人目専任SREの立ち上げを加速する ― AIと進めたオンボーディングで2分を0.04秒にした話
pkshadeck
PRO
0
3.5k
生成AIで帳票OCRが「簡単に」作れる時代になった?
kon_shou
0
480
PHP初心者セッション2026 〜生成AIでは見えない裏側を知る:今だからLAMPを通して仕組みを学ぶ〜
kashioka
0
830
Apache Hive: そしてCloud Native Lakehouseへ
okumin
1
210
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
290
【やさしく解説 設計編・中級 #6】良いアーキテクチャとは ~ 一本の登り道の、行き先 ~
panda728
PRO
0
210
壊れたパーサから始める関数型設計と構成的なパーサ #fp_matsuri
raiga0310
2
440
PHP Application における Kubernetes 内 gRPC 通信
ganchiku
0
580
Featured
See All Featured
Deep Space Network (abreviated)
tonyrice
0
250
Exploring anti-patterns in Rails
aemeredith
3
460
How to Align SEO within the Product Triangle To Get Buy-In & Support - #RIMC
aleyda
2
1.8k
The Spectacular Lies of Maps
axbom
PRO
1
890
No one is an island. Learnings from fostering a developers community.
thoeni
21
3.8k
The SEO identity crisis: Don't let AI make you average
varn
0
530
Building Experiences: Design Systems, User Experience, and Full Site Editing
marktimemedia
0
560
The MySQL Ecosystem @ GitHub 2015
samlambert
251
13k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
What does AI have to do with Human Rights?
axbom
PRO
1
2.3k
Abbi's Birthday
coloredviolet
3
9.2k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
240
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