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
82
Testing React Components
Andrei Picus
April 28, 2015
Tweet
Share
Other Decks in Programming
See All in Programming
Developer Joy - The New Paradigm
hollycummins
1
380
釣り地図SNSにおける有料機能の実装
nokonoko1203
0
200
Devoxx BE - Local Development in the AI Era
kdubois
0
150
Vueのバリデーション、結局どれを選べばいい? ― 自作バリデーションの限界と、脱却までの道のり ― / Which Vue Validation Library Should We Really Use? The Limits of Self-Made Validation and How I Finally Moved On
neginasu
3
1.7k
オープンソースソフトウェアへの解像度🔬
utam0k
18
3.2k
AIと人間の共創開発!OSSで試行錯誤した開発スタイル
mae616
2
830
Claude Agent SDK を使ってみよう
hyshu
0
1.4k
20251016_Rails News ~Rails 8.1の足音を聴く~
morimorihoge
3
890
Introducing RemoteCompose: break your UI out of the app sandbox.
camaelon
2
160
スマホから Youtube Shortsを見られないようにする
lemolatoon
27
34k
SODA - FACT BOOK(JP)
sodainc
1
9k
社会人になっても趣味開発を続けたい! / traPavilion
mazrean
1
120
Featured
See All Featured
For a Future-Friendly Web
brad_frost
180
10k
Agile that works and the tools we love
rasmusluckow
331
21k
Gamification - CAS2011
davidbonilla
81
5.5k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.2k
Music & Morning Musume
bryan
46
6.9k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
GraphQLの誤解/rethinking-graphql
sonatard
73
11k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
127
54k
Designing for Performance
lara
610
69k
Build The Right Thing And Hit Your Dates
maggiecrowley
38
2.9k
We Have a Design System, Now What?
morganepeng
53
7.9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
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