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
75
Testing React Components
Andrei Picus
April 28, 2015
Tweet
Share
Other Decks in Programming
See All in Programming
ASP.NETアプリケーションのモダナイゼーションについて
tomokusaba
0
210
Browser and UI #2 HTML/ARIA
ken7253
2
160
Road to RubyKaigi: Making Tinny Chiptunes with Ruby
makicamel
4
510
スモールスタートで始めるためのLambda×モノリス(Lambdalith)
akihisaikeda
2
310
By the way Google Cloud Next 2025に行ってみてどうだった
ymd65536
0
110
RuboCop: Modularity and AST Insights
koic
2
2.1k
「理解」を重視したAI活用開発
fast_doctor
0
220
個人開発の学生アプリが企業譲渡されるまで
akidon0000
1
1.1k
iOSアプリで測る!名古屋駅までの 方向と距離
ryunakayama
0
140
flutter_kaigi_mini_4.pdf
nobu74658
0
130
設計の本質:コード、システム、そして組織へ / The Essence of Design: To Code, Systems, and Organizations
nrslib
10
3.5k
Ruby's Line Breaks
yui_knk
3
2.1k
Featured
See All Featured
How to Think Like a Performance Engineer
csswizardry
23
1.5k
We Have a Design System, Now What?
morganepeng
52
7.5k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
331
21k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Build your cross-platform service in a week with App Engine
jlugia
230
18k
Rebuilding a faster, lazier Slack
samanthasiow
81
9k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Building Flexible Design Systems
yeseniaperezcruz
329
39k
Being A Developer After 40
akosma
91
590k
Optimizing for Happiness
mojombo
378
70k
The Power of CSS Pseudo Elements
geoffreycrofte
75
5.8k
Mobile First: as difficult as doing things right
swwweet
223
9.6k
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