SlideShare a Scribd company logo
Test-Driven
Development
in React with Cypress
Josh Justice
1 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
@CodingItWrong
I want to help developers
build great apps and go home.
2 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
We're hiring!
bignerdranch.com/careers
3 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
4 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
JavaScript
Testing
Is Getting Big
5 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Test-Driven Development:
Writing your tests
before you write
the code that passes them.
6 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Why
TDD?7 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
1. A Way to
Get Started
Testing8 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
2. Help Avoiding
Testing the
Implementation
9 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
3. Simple
Design10 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Focusing on
Functionality
11 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Focusing on
Functionality
-> Spaghetti
12 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Focusing on
Design
13 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Focusing on
Design
-> Deterioration14 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Focusing on
Flexibility
15 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Focusing on
Flexibility
-> Overdesign
16 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
1. Spaghetti
2. Design Deterioriation
3. Overdesign
17 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
2 Guidelines
18 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
1. Build the
bare minimum
you need right now
19 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
2. Make the code
easy to change
for when you need
something else
20 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Minimal,
Changeable
Code21 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Visualize
the
Payoff22 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
23 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
TDD
24 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Outside-In TDD
25 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
26 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Requirement:
As a user, I want to be able to
send a message.
27 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Write a feature test
that specifies what the
user wants to do.
28 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
describe('Creating a message', () => {
it('Displays the message in the list', () => {
cy.visit('http://localhost:3000');
cy.get('[data-test="messageText"]')
.type('New message');
cy.get('[data-test="saveButton"]')
.click();
cy.get('[data-test="messageText"]')
.should('have.value', '');
cy.contains('New message');
});
});
29 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Run the test and watch
it fail.
30 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
31 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Write only enough code
to fix the current error
or failure.
32 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
import React, { Component } from 'react';
class App extends Component {
render() {
return (
<div>
</div>
);
}
}
33 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Write the code you
wish you had.
34 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
import React, { Component } from 'react';
import NewMessageForm from './NewMessageForm';
class App extends Component {
render() {
return (
<div>
<NewMessageForm />
</div>
);
}
}
35 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
export default class NewMessageForm extends Component {
render() {
return (
<div>
<input
type="text"
data-test="messageText"
/>
</div>
);
}
}
36 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Focusing on fixing the
current error keeps
your code minimal, so
easier to change
37 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Rerun the feature test
and see what
the next failure is
38 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
39 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
return (
<div>
<input
type="text"
data-test="messageText"
/>
<button
data-test="saveButton"
>
Save
</button>
</div>
);
40 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
41 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
When you get a
behavior error,
step down to a
component test.
42 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Component tests keep
your code changeable:
the component
behavior is specified,
so we can reuse it.
43 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Write just enough
component test to
reproduce the feature
test failure.
44 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
describe('<NewMessageForm />', () => {
describe('clicking the save button', () => {
it('clears the text field', () => {
mount(<NewMessageForm />);
cy.get('[data-test="messageText"]').type('New message');
cy.get('[data-test="saveButton"]').click();
cy.get('[data-test="messageText"]').should('have.value', '');
});
});
});
45 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
46 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
render() {
const { inputText } = this.state;
return (
<div>
<input
type="text"
data-test="messageText"
value={inputText}
onChange={this.handleTextChange}
/>
<button
data-test="saveButton"
onClick={this.handleSave}
>
Save
</button>
47 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
export default class NewMessageForm extends Component {
state = { inputText: '' }
handleTextChange = (event) => {
this.setState({ inputText: event.target.value });
}
handleSave = () => {
this.setState({ inputText: '' });
}
render() {
48 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
49 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
When the Component Test Passes,
Step back up to the feature
test to see the next failure
50 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
51 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
describe('<NewMessageForm />', () => {
describe('clicking the save button', () => {
it('clears the text field', () => {
mount(<NewMessageForm />);
cy.get('[data-test="messageText"]').type('New message');
cy.get('[data-test="saveButton"]').click();
cy.get('[data-test="messageText"]').should('have.value', '');
});
});
});
52 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Contract Testing
“Other components can assume the component will fulfill its
contractual promise that it will produce the expected output if given
the correct input.”
-- Edd Yerburgh, Testing Vue.js Applications
53 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Testing the contract
lets you change the
implementation.
54 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Make
One Assertion Per Test
in Unit Tests
55 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
describe('<NewMessageForm />', () => {
describe('clicking the save button', () => {
beforeEach(() => {
mount(<NewMessageForm />);
cy.get('[data-test="messageText"]').type('New message');
cy.get('[data-test="saveButton"]').click();
});
it('clears the text field', () => {
cy.get('[data-test="messageText"]').should('have.value', '');
});
});
});
56 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
describe('clicking the save button', () => {
let saveHandler;
beforeEach(() => {
saveHandler = cy.spy();
mount(<NewMessageForm onSave={saveHandler} />);
cy.get('[data-test="messageText"]').type('New message');
cy.get('[data-test="saveButton"]').click();
});
//...
it('calls the save handler', () => {
expect(saveHandler).to.have.been.calledWith('New message');
});
});
57 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
58 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
handleSave = () => {
const { inputText } = this.state;
const { onSave } = this.props;
onSave(inputText);
this.setState({ inputText: '' });
}
59 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
60 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
61 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
class App extends Component {
render() {
return (
<div>
<NewMessageForm />
</div>
);
}
}
62 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
class App extends Component {
handleSave = (newMessage) => {
}
render() {
return (
<div>
<NewMessageForm onSave={this.handleSave} />
</div>
);
}
}
63 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
64 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
import NewMessageForm from './NewMessageForm';
class App extends Component {
state = { messages: [] };
handleSave = (newMessage) => {
this.setState(state => (
{ messages: [newMessage, ...state.messages] }
));
}
render() {
65 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
import MessageList from './MessageList';
class App extends Component {
//...
render() {
const { messages } = this.state;
return (
<div>
<NewMessageForm onSave={this.handleSave} />
<MessageList data={messages} />
</div>
);
}
66 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
const MessageList = ({ data }) => (
<ul>
{ data.map(message => (
<li key={message}>{message}</li>
)) }
</ul>
);
67 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
68 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
69 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Outside-In TDD
70 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Minimal,
Changeable
Code71 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Imagine…
72 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Imagine…
…when you finish
building your feature
it's already fully
covered by tests.
73 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Imagine…
…always having a
simple next step:
write a test. Or fix
the next test failure.
74 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Imagine…
…delivering useful functionality
every few hours,
instead of working for days
on code that might not end up used.
75 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
learntdd.in/react76 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
twitch.tv/codingitwrong
77 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
78 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
79 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
80 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
Thanks!
Resources: bit.ly/react-tdd
Tweet me at @CodingItWrong!
81 TDD in React with Cypress - @CodingItWrong - connect.tech 2018

More Related Content

What's hot (20)

DOCX
Đề tài: Nghiên cứu và triển khai hệ thống Windown Server 2012
Dịch vụ viết bài trọn gói ZALO 0917193864
 
DOCX
CƠ SỞ LÝ LUẬN CỦA VIỆC PHÁT TRIỂN TƯ DUY SÁNG TẠO CHO HỌC SINH TRONG DẠY HỌC ...
Dịch vụ viết thuê đề tài trọn gói 👍👍 Liên hệ Zalo/Tele: 0917.193.864
 
PPTX
React Native and React JS Advantages, Disadvantages and Features.pptx
InnvonixTechSolution
 
PPTX
Module 1 : Hiểu Biết CNTT Cơ Bản
Long Nguyen
 
PPTX
Bài 1: Phát triển ứng dụng trên thiết bị di động
Tuan Nguyen
 
PPTX
Testes E2E em Cypress com JS
Nàtali Cabral
 
PPT
Xây dựng hệ thống mạng cho Công Ty Cổ Phần Trường Tân trên nền tảng server 2008
laonap166
 
PDF
Phân tích mã độc cơ bản - báo cáo thực tập
Phạm Trung Đức
 
PDF
Báo cáo thực tập Athena - Xây dựng web tin tức bằng WordPress
TranVanVuong
 
PDF
thực tập report eng final
Ili Dileaf
 
DOC
Đề tài: Kế toán vốn bằng tiền tại công ty thương mại-xây dựng
Dịch vụ viết bài trọn gói ZALO 0917193864
 
PPTX
JVM memory management & Diagnostics
Dhaval Shah
 
PDF
Luận văn: Giám sát hàng hóa xuất nhập khẩu tại cảng Hải Phòng
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
PPT
Tìm hiểu về Joomla
Lương Bá Hợp
 
DOCX
[Athena]Nghiên Cứu Và Xây Dựng Website Bằng Wordpress
Cương Trần
 
PDF
Đề tài: Tìm hiểu mạng riêng ảo và ứng dụng, HOT
Dịch vụ viết bài trọn gói ZALO 0917193864
 
PDF
đồ áN tìm hiểu và phát triển hệ thống (idsips) zeek 7972673
jackjohn45
 
DOC
Nâng cao HQKD nghiệp vụ bảo hiểm hàng hoá XNK vận chuyển bằng đường biển ở cô...
Luanvan84
 
PPTX
Cypress for Testing
PoojaSingh1123
 
PDF
Đề tài: Sử dụng trò chơi học tập trong dạy học phân môn Luyện từ và câu lớp 4, 5
Dịch vụ viết thuê Khóa Luận - ZALO 0932091562
 
Đề tài: Nghiên cứu và triển khai hệ thống Windown Server 2012
Dịch vụ viết bài trọn gói ZALO 0917193864
 
CƠ SỞ LÝ LUẬN CỦA VIỆC PHÁT TRIỂN TƯ DUY SÁNG TẠO CHO HỌC SINH TRONG DẠY HỌC ...
Dịch vụ viết thuê đề tài trọn gói 👍👍 Liên hệ Zalo/Tele: 0917.193.864
 
React Native and React JS Advantages, Disadvantages and Features.pptx
InnvonixTechSolution
 
Module 1 : Hiểu Biết CNTT Cơ Bản
Long Nguyen
 
Bài 1: Phát triển ứng dụng trên thiết bị di động
Tuan Nguyen
 
Testes E2E em Cypress com JS
Nàtali Cabral
 
Xây dựng hệ thống mạng cho Công Ty Cổ Phần Trường Tân trên nền tảng server 2008
laonap166
 
Phân tích mã độc cơ bản - báo cáo thực tập
Phạm Trung Đức
 
Báo cáo thực tập Athena - Xây dựng web tin tức bằng WordPress
TranVanVuong
 
thực tập report eng final
Ili Dileaf
 
Đề tài: Kế toán vốn bằng tiền tại công ty thương mại-xây dựng
Dịch vụ viết bài trọn gói ZALO 0917193864
 
JVM memory management & Diagnostics
Dhaval Shah
 
Luận văn: Giám sát hàng hóa xuất nhập khẩu tại cảng Hải Phòng
Dịch Vụ Viết Bài Trọn Gói ZALO 0917193864
 
Tìm hiểu về Joomla
Lương Bá Hợp
 
[Athena]Nghiên Cứu Và Xây Dựng Website Bằng Wordpress
Cương Trần
 
Đề tài: Tìm hiểu mạng riêng ảo và ứng dụng, HOT
Dịch vụ viết bài trọn gói ZALO 0917193864
 
đồ áN tìm hiểu và phát triển hệ thống (idsips) zeek 7972673
jackjohn45
 
Nâng cao HQKD nghiệp vụ bảo hiểm hàng hoá XNK vận chuyển bằng đường biển ở cô...
Luanvan84
 
Cypress for Testing
PoojaSingh1123
 
Đề tài: Sử dụng trò chơi học tập trong dạy học phân môn Luyện từ và câu lớp 4, 5
Dịch vụ viết thuê Khóa Luận - ZALO 0932091562
 

Similar to Test-Driven Development in React with Cypress (20)

PDF
Test-Driven Development in Vue with Cypress
Josh Justice
 
PDF
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Christian Catalan
 
PPTX
Cypress report
Adarsh
 
PPTX
End-to-End Testing in React with Cypress.pptx
ExcelRSEO
 
PDF
Automated testing in javascript
Michael Yagudaev
 
PPTX
Introduction to Integration Testing With Cypress
Erez Cohen
 
PDF
Automated testing with Cypress
Yong Shean Chong
 
PPTX
Test driven development with react
Leon Bezuidenhout
 
PDF
Test driven development and react js application go hand in hand
Katy Slemon
 
PDF
Cypress Best Pratices for Test Automation
Knoldus Inc.
 
PDF
Achievement Unlocked: Drive development, increase velocity, and write blissfu...
All Things Open
 
PPTX
Slides for Automation Testing or End to End testing
SwapnilNarayan
 
PPTX
Cypress workshop for JSFoo 2019
Biswajit Pattanayak
 
PDF
Cypress Testing Demystified: A Practical Guide
Testgrid.io
 
PDF
TDD: Develop, Refactor and Release With Confidence
Mehdi Valikhani
 
PPTX
Introduction to cypress in Angular (Chinese)
Hong Tat Yew
 
PDF
Don't let your tests slow you down
Daniel Irvine
 
PDF
End-to-End Testing with Cypress
kitconcept GmbH
 
PDF
'NO EXCUSES FOR NOT WRITING TESTS!' by ANDRII SHUMADA @OdessaJS'2020
OdessaJS Conf
 
PPTX
Cypress test techniques cucumber bdd framework,tdd,api tests course
Narayanan Palani
 
Test-Driven Development in Vue with Cypress
Josh Justice
 
Testing Vue Apps with Cypress.io (STLJS Meetup April 2018)
Christian Catalan
 
Cypress report
Adarsh
 
End-to-End Testing in React with Cypress.pptx
ExcelRSEO
 
Automated testing in javascript
Michael Yagudaev
 
Introduction to Integration Testing With Cypress
Erez Cohen
 
Automated testing with Cypress
Yong Shean Chong
 
Test driven development with react
Leon Bezuidenhout
 
Test driven development and react js application go hand in hand
Katy Slemon
 
Cypress Best Pratices for Test Automation
Knoldus Inc.
 
Achievement Unlocked: Drive development, increase velocity, and write blissfu...
All Things Open
 
Slides for Automation Testing or End to End testing
SwapnilNarayan
 
Cypress workshop for JSFoo 2019
Biswajit Pattanayak
 
Cypress Testing Demystified: A Practical Guide
Testgrid.io
 
TDD: Develop, Refactor and Release With Confidence
Mehdi Valikhani
 
Introduction to cypress in Angular (Chinese)
Hong Tat Yew
 
Don't let your tests slow you down
Daniel Irvine
 
End-to-End Testing with Cypress
kitconcept GmbH
 
'NO EXCUSES FOR NOT WRITING TESTS!' by ANDRII SHUMADA @OdessaJS'2020
OdessaJS Conf
 
Cypress test techniques cucumber bdd framework,tdd,api tests course
Narayanan Palani
 
Ad

More from Josh Justice (15)

PDF
Effective Detox Testing - React Advanced 2023
Josh Justice
 
PDF
Designing Effective Tests with React Testing Library - React Summit 2023
Josh Justice
 
PDF
Testing React Native Apps - Chain React 2023
Josh Justice
 
PDF
Designing Effective Tests with React Testing Library - React Day Berlin 2022
Josh Justice
 
PDF
Building for Mobile and Web with Expo - React Day Berlin 2022
Josh Justice
 
PDF
Intro to React Native Testing Library
Josh Justice
 
PDF
Building for Mobile and Web with Expo - React Advanced London 2022
Josh Justice
 
PDF
Getting Better All the Time: How to Escape Bad Code
Josh Justice
 
PDF
Sustainable Learning - ReactATL Jan 2022
Josh Justice
 
PDF
Building an App for Mobile and Web with Expo
Josh Justice
 
PPTX
User-Modifiable Software: Smalltalk and HyperCard
Josh Justice
 
PDF
Practical Accessibility (A11y)
Josh Justice
 
PDF
Old Solutions to New Testing Problems
Josh Justice
 
PDF
Newbie's Guide to Contributing to Babel
Josh Justice
 
PPTX
Outside-in Testing in Vue with Cypress
Josh Justice
 
Effective Detox Testing - React Advanced 2023
Josh Justice
 
Designing Effective Tests with React Testing Library - React Summit 2023
Josh Justice
 
Testing React Native Apps - Chain React 2023
Josh Justice
 
Designing Effective Tests with React Testing Library - React Day Berlin 2022
Josh Justice
 
Building for Mobile and Web with Expo - React Day Berlin 2022
Josh Justice
 
Intro to React Native Testing Library
Josh Justice
 
Building for Mobile and Web with Expo - React Advanced London 2022
Josh Justice
 
Getting Better All the Time: How to Escape Bad Code
Josh Justice
 
Sustainable Learning - ReactATL Jan 2022
Josh Justice
 
Building an App for Mobile and Web with Expo
Josh Justice
 
User-Modifiable Software: Smalltalk and HyperCard
Josh Justice
 
Practical Accessibility (A11y)
Josh Justice
 
Old Solutions to New Testing Problems
Josh Justice
 
Newbie's Guide to Contributing to Babel
Josh Justice
 
Outside-in Testing in Vue with Cypress
Josh Justice
 
Ad

Recently uploaded (20)

PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PDF
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
PDF
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PDF
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
Tally software_Introduction_Presentation
AditiBansal54083
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PDF
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
PDF
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
PPTX
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
PDF
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
MiniTool Partition Wizard 12.8 Crack License Key LATEST
hashhshs786
 
Automate Cybersecurity Tasks with Python
VICTOR MAESTRE RAMIREZ
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
vMix Pro 28.0.0.42 Download vMix Registration key Bundle
kulindacore
 
Human Resources Information System (HRIS)
Amity University, Patna
 
Tally software_Introduction_Presentation
AditiBansal54083
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Mobile CMMS Solutions Empowering the Frontline Workforce
CryotosCMMSSoftware
 
Capcut Pro Crack For PC Latest Version {Fully Unlocked} 2025
hashhshs786
 
The Role of a PHP Development Company in Modern Web Development
SEO Company for School in Delhi NCR
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
Understanding the Need for Systemic Change in Open Source Through Intersectio...
Imma Valls Bernaus
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
Thread In Android-Mastering Concurrency for Responsive Apps.pdf
Nabin Dhakal
 
HiHelloHR – Simplify HR Operations for Modern Workplaces
HiHelloHR
 

Test-Driven Development in React with Cypress

  • 1. Test-Driven Development in React with Cypress Josh Justice 1 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 2. @CodingItWrong I want to help developers build great apps and go home. 2 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 3. We're hiring! bignerdranch.com/careers 3 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 4. 4 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 5. JavaScript Testing Is Getting Big 5 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 6. Test-Driven Development: Writing your tests before you write the code that passes them. 6 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 7. Why TDD?7 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 8. 1. A Way to Get Started Testing8 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 9. 2. Help Avoiding Testing the Implementation 9 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 10. 3. Simple Design10 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 11. Focusing on Functionality 11 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 12. Focusing on Functionality -> Spaghetti 12 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 13. Focusing on Design 13 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 14. Focusing on Design -> Deterioration14 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 15. Focusing on Flexibility 15 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 16. Focusing on Flexibility -> Overdesign 16 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 17. 1. Spaghetti 2. Design Deterioriation 3. Overdesign 17 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 18. 2 Guidelines 18 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 19. 1. Build the bare minimum you need right now 19 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 20. 2. Make the code easy to change for when you need something else 20 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 21. Minimal, Changeable Code21 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 22. Visualize the Payoff22 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 23. 23 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 24. TDD 24 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 25. Outside-In TDD 25 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 26. 26 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 27. Requirement: As a user, I want to be able to send a message. 27 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 28. Write a feature test that specifies what the user wants to do. 28 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 29. describe('Creating a message', () => { it('Displays the message in the list', () => { cy.visit('http://localhost:3000'); cy.get('[data-test="messageText"]') .type('New message'); cy.get('[data-test="saveButton"]') .click(); cy.get('[data-test="messageText"]') .should('have.value', ''); cy.contains('New message'); }); }); 29 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 30. Run the test and watch it fail. 30 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 31. 31 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 32. Write only enough code to fix the current error or failure. 32 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 33. import React, { Component } from 'react'; class App extends Component { render() { return ( <div> </div> ); } } 33 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 34. Write the code you wish you had. 34 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 35. import React, { Component } from 'react'; import NewMessageForm from './NewMessageForm'; class App extends Component { render() { return ( <div> <NewMessageForm /> </div> ); } } 35 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 36. export default class NewMessageForm extends Component { render() { return ( <div> <input type="text" data-test="messageText" /> </div> ); } } 36 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 37. Focusing on fixing the current error keeps your code minimal, so easier to change 37 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 38. Rerun the feature test and see what the next failure is 38 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 39. 39 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 41. 41 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 42. When you get a behavior error, step down to a component test. 42 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 43. Component tests keep your code changeable: the component behavior is specified, so we can reuse it. 43 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 44. Write just enough component test to reproduce the feature test failure. 44 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 45. describe('<NewMessageForm />', () => { describe('clicking the save button', () => { it('clears the text field', () => { mount(<NewMessageForm />); cy.get('[data-test="messageText"]').type('New message'); cy.get('[data-test="saveButton"]').click(); cy.get('[data-test="messageText"]').should('have.value', ''); }); }); }); 45 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 46. 46 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 47. render() { const { inputText } = this.state; return ( <div> <input type="text" data-test="messageText" value={inputText} onChange={this.handleTextChange} /> <button data-test="saveButton" onClick={this.handleSave} > Save </button> 47 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 48. export default class NewMessageForm extends Component { state = { inputText: '' } handleTextChange = (event) => { this.setState({ inputText: event.target.value }); } handleSave = () => { this.setState({ inputText: '' }); } render() { 48 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 49. 49 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 50. When the Component Test Passes, Step back up to the feature test to see the next failure 50 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 51. 51 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 52. describe('<NewMessageForm />', () => { describe('clicking the save button', () => { it('clears the text field', () => { mount(<NewMessageForm />); cy.get('[data-test="messageText"]').type('New message'); cy.get('[data-test="saveButton"]').click(); cy.get('[data-test="messageText"]').should('have.value', ''); }); }); }); 52 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 53. Contract Testing “Other components can assume the component will fulfill its contractual promise that it will produce the expected output if given the correct input.” -- Edd Yerburgh, Testing Vue.js Applications 53 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 54. Testing the contract lets you change the implementation. 54 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 55. Make One Assertion Per Test in Unit Tests 55 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 56. describe('<NewMessageForm />', () => { describe('clicking the save button', () => { beforeEach(() => { mount(<NewMessageForm />); cy.get('[data-test="messageText"]').type('New message'); cy.get('[data-test="saveButton"]').click(); }); it('clears the text field', () => { cy.get('[data-test="messageText"]').should('have.value', ''); }); }); }); 56 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 57. describe('clicking the save button', () => { let saveHandler; beforeEach(() => { saveHandler = cy.spy(); mount(<NewMessageForm onSave={saveHandler} />); cy.get('[data-test="messageText"]').type('New message'); cy.get('[data-test="saveButton"]').click(); }); //... it('calls the save handler', () => { expect(saveHandler).to.have.been.calledWith('New message'); }); }); 57 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 58. 58 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 59. handleSave = () => { const { inputText } = this.state; const { onSave } = this.props; onSave(inputText); this.setState({ inputText: '' }); } 59 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 60. 60 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 61. 61 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 62. class App extends Component { render() { return ( <div> <NewMessageForm /> </div> ); } } 62 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 63. class App extends Component { handleSave = (newMessage) => { } render() { return ( <div> <NewMessageForm onSave={this.handleSave} /> </div> ); } } 63 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 64. 64 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 65. import NewMessageForm from './NewMessageForm'; class App extends Component { state = { messages: [] }; handleSave = (newMessage) => { this.setState(state => ( { messages: [newMessage, ...state.messages] } )); } render() { 65 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 66. import MessageList from './MessageList'; class App extends Component { //... render() { const { messages } = this.state; return ( <div> <NewMessageForm onSave={this.handleSave} /> <MessageList data={messages} /> </div> ); } 66 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 67. const MessageList = ({ data }) => ( <ul> { data.map(message => ( <li key={message}>{message}</li> )) } </ul> ); 67 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 68. 68 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 69. 69 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 70. Outside-In TDD 70 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 71. Minimal, Changeable Code71 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 72. Imagine… 72 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 73. Imagine… …when you finish building your feature it's already fully covered by tests. 73 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 74. Imagine… …always having a simple next step: write a test. Or fix the next test failure. 74 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 75. Imagine… …delivering useful functionality every few hours, instead of working for days on code that might not end up used. 75 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 76. learntdd.in/react76 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 77. twitch.tv/codingitwrong 77 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 78. 78 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 79. 79 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 80. 80 TDD in React with Cypress - @CodingItWrong - connect.tech 2018
  • 81. Thanks! Resources: bit.ly/react-tdd Tweet me at @CodingItWrong! 81 TDD in React with Cypress - @CodingItWrong - connect.tech 2018