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
83
Testing React Components
Andrei Picus
April 28, 2015
Tweet
Share
Other Decks in Programming
See All in Programming
AI駆動開発の本音 〜Claude Code並列開発で見えたエンジニアの新しい役割〜
hisuzuya
3
420
AI主導でFastAPIのWebサービスを作るときに 人間が構造化すべき境界線
okajun35
0
400
AIエージェントのキホンから学ぶ「エージェンティックコーディング」実践入門
masahiro_nishimi
7
1.2k
受け入れテスト駆動開発(ATDD)×AI駆動開発 AI時代のATDDの取り組み方を考える
kztakasaki
2
490
「やめとこ」がなくなった — 1月にZennを始めて22本書いた AI共創開発のリアル
atani14
0
330
文字コードの話
qnighy
43
16k
2026/02/04 AIキャラクター人格の実装論 口 調の模倣から、コンテキスト制御による 『思想』と『行動』の創発へ
sr2mg4
0
640
CSC307 Lecture 11
javiergs
PRO
0
580
要求定義・仕様記述・設計・検証の手引き - 理論から学ぶ明確で統一された成果物定義
orgachem
PRO
1
480
atmaCup #23でAIコーディングを活用した話
ml_bear
4
710
猫の手も借りたい!ので AIエージェント猫を作って社内に放した話 Claude Code × Container Lambda の Slack Bot "DevNeko"
naramomi7
0
220
今更考える「単一責任原則」 / Thinking about the Single Responsibility Principle
tooppoo
3
1.2k
Featured
See All Featured
Building Adaptive Systems
keathley
44
2.9k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
470
End of SEO as We Know It (SMX Advanced Version)
ipullrank
3
4k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
55
3.3k
Code Reviewing Like a Champion
maltzj
527
40k
Testing 201, or: Great Expectations
jmmastey
46
8.1k
The AI Revolution Will Not Be Monopolized: How open-source beats economies of scale, even for LLMs
inesmontani
PRO
3
3.1k
RailsConf 2023
tenderlove
30
1.4k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
1.7k
sira's awesome portfolio website redesign presentation
elsirapls
0
170
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
270
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