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
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Andrei Picus
April 28, 2015
Programming
84
1
Share
Testing React Components
Andrei Picus
April 28, 2015
Other Decks in Programming
See All in Programming
Building on Bluesky's AT Protocol with Ruby
mackuba
0
110
Symfony AI in Action - SymfonyLive Berlin 2026
chr_hertel
1
120
UaaL×Androidアプリのメモリ計測 — Memory Profilerの先へ
rio432
0
110
AI-DLC Deep Dive
yuukiyo
9
5.6k
ハーネスエンジニアリングにどう向き合うか 〜ルールファイルを超えて開発プロセスを設計する〜 / How to approach harness engineering
rkaga
28
19k
検索設計から 推論設計への重心移動と Recall-First Retrieval
po3rin
5
1.5k
Spec-Driven Development with AI Agents (Workshop, May 2026)
antonarhipov
2
310
AIと共に生きる技術選定 2026
sgash708
0
120
Liberating Ruby's Parser from Lexer Hacks
ydah
2
2.6k
t *testing.T は どこからやってくるの?
otakakot
1
910
PHPでバイナリをパースして理解するASN.1
muno92
PRO
0
420
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
760
Featured
See All Featured
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
Public Speaking Without Barfing On Your Shoes - THAT 2023
reverentgeek
1
380
Ruling the World: When Life Gets Gamed
codingconduct
0
220
Mobile First: as difficult as doing things right
swwweet
225
10k
Site-Speed That Sticks
csswizardry
13
1.2k
エンジニアに許された特別な時間の終わり
watany
106
240k
RailsConf 2023
tenderlove
30
1.4k
Scaling GitHub
holman
464
140k
Facilitating Awesome Meetings
lara
57
6.8k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
530
Code Review Best Practice
trishagee
74
20k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
54k
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