Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Unit Testing For React Component
Search
camcam_lemon
February 22, 2018
Programming
99
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Unit Testing For React Component
camcam_lemon
February 22, 2018
More Decks by camcam_lemon
See All by camcam_lemon
オレを実装してデザイン実装楽したい
lemon
0
93
要素のサイズを変えずに押しやすくする
lemon
0
100
iOSのキーボード入力ビューをカスタマイズする
lemon
0
340
視え方と文字の大きさ
lemon
1
480
Yarn WorkSpaces × React Nativeの環境構築
lemon
0
340
フロントエンドにおけるアーキテクチャとの向き合い方
lemon
10
5.1k
UI/UXデザイナーがデザインしてるもの
lemon
2
350
react-reduxで追加されたHooks APIの良い所と使い方
lemon
5
1.1k
ESLintで始めるTypeScriptの静的解析
lemon
8
2.2k
Other Decks in Programming
See All in Programming
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
580
パズルゲームの作り方 / how to make puzzle games
kaityo256
PRO
2
280
IBM Bob でAI駆動開発『QC-Playbook』
ichirokano2
0
110
ハーネス設計入門 〜 基礎知識の整理から実務へのステップアップ 〜
kinopeee
22
22k
R8 の設定見直しでアプリサイズ削減
andpad
0
210
Agentic Software Factoryに、すごく賢いIF文を。 / super smart IF statement into the Agentic Software Factory.
rkaga
8
2.1k
[Rails World 2026] Durable orchestration on Rails: from continuation to workflow
palkan
1
720
更なる可用性を求めて、5年間運用したKotlinのアプリケーションをGoでリプレイスする話
ken_tunc
0
570
golang-bob.pdf
iwatsukayura
0
120
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
350
気持ちぃ〜角丸 Squircle
ryu1013
0
280
Agents on Rails - Rails at Scale 2026
irinanazarova
0
530
Featured
See All Featured
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.7k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
390
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
450
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
280
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
Typedesign – Prime Four
hannesfritz
42
3.2k
Designing for humans not robots
tammielis
254
26k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
37
6.6k
Darren the Foodie - Storyboard
khoart
PRO
4
4k
Transcript
UNIT TESTING FOR REACT COMPONENTS
Instructions Name @camcam_lemon (甲斐田 亮一) Occupation フロントエンドエンジニア Company 日本事務器株式会社 Hobby
お酒作る(飲む)の大好きマン スノボうまくなりたいマン
Use Testing Tools Testing framework - mocha Coverage - nyc
- istanbul Assertion - power-assert Test helper - jsdom - enzyme - sinon - testdouble.js - fetch-mock
Use Testing Tools Testing framework - mocha Coverage - nyc
- istanbul Assertion - power-assert Test helper - jsdom - enzyme - sinon - testdouble.js - fetch-mock
“ ReactComponentのテスト どうやって書いてますか・・・?
“ refのテストってどう書くんだろ・・・?
“ イベントハンドラは・・・
“ クライアント座標って・・・
公式ページ見ただけじゃ あんまりテスト書けないよ
テストで詰まりそうなポイント ▸ refコールバック ▸ DOMオブジェクトへのアクセス ▸ 引数有りのイベントハンドラ ▸ 時間がからむ処理(setTimeout()) ▸
グローバルオブジェクトへのアクセス ▹ location絡むとたいていイく
refコールバック ▸
[email protected]
からref情報を読み込むよう になった ▸ refが文字列属性かコールバック属性かで テストの仕方が変わる
“
“
このままだとテストは死ぬ
“
DOMオブジェクトへのアクセス ▸ DOMの情報はテスト環境では空のオブジェ クトになっている ▸ getBoundingClientRect()など一部のメソッ ドはmount()でレンダーすれば取得できる (返り値は常に0)
shallowでテストできないんだ・・・ Material-UIラップしてるからmount()できな いよ・・・
“ shallowでもテストできます 値も設定することができる!!
“
“
引数有りのイベントハンドラ ▸ simulate()の第2引数以降にモックオブジェ クトを渡すことができる ▸ instance()から実行することもできる
“
“
ReduxFormのprops inputとmetaオブジェクトを渡すだけ!
“
“
時間がからむ処理 ▸ 時間を上書きしてザ・ワールド ▸ testdouble.jsではtestdouble-timersを使用 してテストする
“
“
グローバルオブジェクトへのアクセス ▸ jsdomのuserAgentやlocation等は直接書き 換えることができない ▸ windowを明示するかどうかでテストの仕方 (と難易度)が変わる ▸ console, userAgent,
locationだけ説明しま す!
console 実行されるけど、確認の方法がわからん!! シェルに出力したくない!!
“
userAgent
None
setterがない・・・!! ないなら作り変えてしまおう!!
“
“
“
location
ただし・・・
元コードの中で location.search = ‘’; location.href = ‘’; が実行されると無事死亡
作り変えることもできない・・・
知っている人いたら 教えてください