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
70
Testing React Components
Andrei Picus
April 28, 2015
Tweet
Share
Other Decks in Programming
See All in Programming
Going beyond Apache Parquet's default settings
xhochy
0
140
Tailwind CSSを本気でカスタマイズする方法
fsubal
15
5.5k
Fragment Composition of GraphQL
quramy
13
1.5k
障害対応を起点としたもっといい開発と運用のサイクル作りのためにできること / Hatena Enginner Seminar #29
polamjag
0
400
Micro Frontends for Java Microservices - Utah JUG 2024
mraible
PRO
1
110
Let's learn code review
riofujimon
2
600
GitHub Copilotのススメ
marcy731
1
230
GitLab CI/CD で C#/WPFアプリケーションのテストとインストーラーのビルド・デプロイを自動化する
hacarus
0
440
Introducing Kotlin Multiplatform in an existing mobile app - Workshop Edition | AndroidMakers Paris
prof18
0
160
Implementing Design Systems in Swift
seyfoyun
2
480
Polars入門
daikikatsuragawa
1
190
Goのmultiple errorsについて (2024年4月版)
syumai
4
1.2k
Featured
See All Featured
Happy Clients
brianwarren
92
6.4k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
117
18k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
8
3.4k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
323
20k
Designing on Purpose - Digital PM Summit 2013
jponch
111
6.5k
Fireside Chat
paigeccino
22
2.6k
Designing the Hi-DPI Web
ddemaree
276
33k
Fashionably flexible responsive web design (full day workshop)
malarkey
398
65k
Producing Creativity
orderedlist
PRO
338
39k
How GitHub (no longer) Works
holman
305
140k
The Cost Of JavaScript in 2023
addyosmani
21
3.9k
Building Flexible Design Systems
yeseniaperezcruz
320
37k
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