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
1
72
Testing React Components
Andrei Picus
April 28, 2015
Tweet
Share
Other Decks in Programming
See All in Programming
Windows版PHPのビルド手順とPHP 8.4における変更点
matsuo_atsushi
0
360
フロントエンドテストの育て方
quramy
8
2.1k
MCP世界への招待: AIエンジニアが創る次世代エージェント連携の世界
gunta
1
360
私の愛したLaravel 〜レールを超えたその先へ〜
kentaroutakeda
11
3k
Identifying and Analyzing Fake OSS with Malware - fukuoka.go#21
rhykw
0
520
新卒から4年間、20年もののWebサービスと 向き合って学んだソフトウェア考古学
oguri
7
6.4k
Develop Faster With FrankenPHP
dunglas
1
1.4k
英語文法から学ぶ、クリーンな設計の秘訣
newnomad
1
260
複雑なフォームと複雑な状態管理にどう向き合うか / #newt_techtalk vol. 15
izumin5210
4
2.4k
プログラミング教育のコスパの話
superkinoko
0
110
JavaOne 2025: Advancing Java Profiling
jbachorik
1
300
Going Structural with Named Tuples
bishabosha
0
140
Featured
See All Featured
Side Projects
sachag
452
42k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
47
5.3k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
CSS Pre-Processors: Stylus, Less & Sass
bermonpainter
356
30k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
22
2.6k
RailsConf 2023
tenderlove
29
1k
Done Done
chrislema
183
16k
Raft: Consensus for Rubyists
vanstee
137
6.8k
Designing for Performance
lara
605
69k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
8
690
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.1k
Bootstrapping a Software Product
garrettdimon
PRO
307
110k
Transcript
Testing React components Andrei Picus
Who? andrei.picus@hootsuite.com 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