Skip to content

Commit 62108fd

Browse files
committed
made the first acceptance test go green
1 parent d9bdf18 commit 62108fd

File tree

3 files changed

+86
-17
lines changed

3 files changed

+86
-17
lines changed

README.md

Lines changed: 79 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Then a list of two notes are displayed
2525
```
2626
### Prerequisites
2727
- [Visual Studio Code](https://code.visualstudio.com/)
28-
- [Node.js](Node.jshttps://nodejs.org)
28+
- [Node.js](https://nodejs.org)
2929

3030
### Red - Acceptance Test
3131
The user story and acceptance criteria above describe a desired customer outcome. The user acceptance test will link this narrative with a high level how. For this tutorial our first application will be a [web application](https://en.wikipedia.org/wiki/Web_application) in [React](https://reactjs.org). The testing framework we will use to test this will be [Cypress](https://www.cypress.io)
@@ -73,4 +73,82 @@ Timed out retrying after 4000ms: Expected to find element: [data-testid=note-nam
7373
[Code for this section](https://github.com/pairing4good/tdd-amplify-react-/commit/998cf7a3da2af3b30aed14ccea18e6d546e85e61)
7474

7575
### Green - Acceptance Test
76+
Before we proceed let's add a script to run cypress into the `package.json` file in the `scripts` section.
77+
```js
78+
"scripts": {
79+
"start": "react-scripts start",
80+
"build": "react-scripts build",
81+
"test": "react-scripts test",
82+
"eject": "react-scripts eject",
83+
"cypress:open": "cypress open"
84+
}
85+
```
86+
- Now you can run `npm run cypress:open` to open cypress
87+
88+
The first step to making this failing test go green is adding an element with one of the `data-testid`'s to the `src/App.js` file.
89+
```js
90+
import './App.css';
91+
92+
function App() {
93+
return (
94+
<div className="App">
95+
<input data-testid="note-name-field"/>
96+
</div>
97+
);
98+
}
99+
100+
export default App;
101+
```
102+
- Now the Cypress test fails on the second field
103+
```
104+
Timed out retrying after 4000ms: Expected to find element: [data-testid=note-description-field], but never found it.
105+
```
106+
- Add the next `input` field and rerun the test
107+
- Now the Cypress test fails on the submit button
108+
```
109+
Timed out retrying after 4000ms: Expected to find element: [data-testid=note-form-submit], but never found it.
110+
```
111+
- Add the `button` element with the expected `data-testid`
112+
```js
113+
<input data-testid="note-name-field"/>
114+
<input data-testid="note-description-field"/>
115+
<button data-testid="note-form-submit"/>
116+
```
117+
- Now the Cypress test fails on the missing list of created notes
118+
```
119+
Timed out retrying after 4000ms: Expected to find element: [data-testid=test-name-0], but never found it.
120+
```
121+
In test driven development we do the simplest thing possible to make a test go green. Once it is green then and only then do we go back and refactor it. In this case, the simplest thing that we can do is hard-code the expected values on the screen.
122+
```js
123+
<input data-testid="note-name-field"/>
124+
<input data-testid="note-description-field"/>
125+
<button data-testid="note-form-submit"/>
126+
<p data-testid="test-name-0">test note</p>
127+
```
128+
- Now the Cypresss test fails on the note description
129+
```
130+
Timed out retrying after 4000ms: Expected to find element: [data-testid=test-description-0], but never found it.
131+
```
132+
- Add the final element for `test-description-0`
133+
```js
134+
import './App.css';
135+
136+
function App() {
137+
return (
138+
<div className="App">
139+
<input data-testid="note-name-field"/>
140+
<input data-testid="note-description-field"/>
141+
<button data-testid="note-form-submit"/>
142+
<p data-testid="test-name-0">test note</p>
143+
<p data-testid="test-description-0">test note description</p>
144+
</div>
145+
);
146+
}
147+
148+
export default App;
149+
```
150+
- While this is far from a useful application, this application can be:
151+
1. refactored on green
152+
1. used to get feedback from the customer
76153

154+
[Code for this section]()

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
"start": "react-scripts start",
1616
"build": "react-scripts build",
1717
"test": "react-scripts test",
18-
"eject": "react-scripts eject"
18+
"eject": "react-scripts eject",
19+
"cypress:open": "cypress open"
1920
},
2021
"eslintConfig": {
2122
"extends": [

src/App.js

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,13 @@
1-
import logo from './logo.svg';
21
import './App.css';
32

43
function App() {
54
return (
65
<div className="App">
7-
<header className="App-header">
8-
<img src={logo} className="App-logo" alt="logo" />
9-
<p>
10-
Edit <code>src/App.js</code> and save to reload.
11-
</p>
12-
<a
13-
className="App-link"
14-
href="https://reactjs.org"
15-
target="_blank"
16-
rel="noopener noreferrer"
17-
>
18-
Learn React
19-
</a>
20-
</header>
6+
<input data-testid="note-name-field"/>
7+
<input data-testid="note-description-field"/>
8+
<button data-testid="note-form-submit"/>
9+
<p data-testid="test-name-0">test note</p>
10+
<p data-testid="test-description-0">test note description</p>
2111
</div>
2212
);
2313
}

0 commit comments

Comments
 (0)