Skip to content

Commit dbbbb75

Browse files
committed
Snapshot while we figure out how to push to a new remote.
1 parent e63f936 commit dbbbb75

19 files changed

+26551
-10352
lines changed

package-lock.json

Lines changed: 15664 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/App.css

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,6 @@
22
text-align: center;
33
}
44

5-
.App-logo {
6-
height: 40vmin;
7-
pointer-events: none;
8-
}
9-
10-
@media (prefers-reduced-motion: no-preference) {
11-
.App-logo {
12-
animation: App-logo-spin infinite 20s linear;
13-
}
14-
}
155

166
.App-header {
177
background-color: #282c34;

src/App.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import React from 'react';
2+
import './App.css';
3+
import Counter from './Counter/Counter'
4+
5+
6+
function App() {
7+
return (
8+
<div className="App">
9+
<Counter />
10+
</div>
11+
);
12+
}
13+
14+
export default App;

src/App.test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import { render, screen } from '@testing-library/react';
3+
import App from './App';
4+
5+
test('renders header correctly', () => {
6+
render(<App />);
7+
const headerElement = screen.getByText("My Counter");
8+
expect(headerElement).toBeInTheDocument();
9+
});

src/App.test.tsx

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/App.tsx

Lines changed: 0 additions & 26 deletions
This file was deleted.

src/Counter/Counter.css

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.red {
2+
color: red;
3+
}
4+
5+
.green {
6+
color: green;
7+
}

src/Counter/Counter.jsx

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React, {useState} from 'react';
2+
import "./Counter.css";
3+
4+
function Counter(){
5+
const [count, setCount] = useState(0)
6+
const [step, setStep] = useState(1);
7+
return(
8+
<div>
9+
<h2 data-testid="header">My Counter</h2>
10+
<h1 data-testid="counter"
11+
className={`${count} >=100 ? green : ${count} < 0 ? "red" : ""}`}
12+
>{count}</h1>
13+
14+
<button data-testid="subtract-button"
15+
onClick={()=>setCount(count-step)}>-</button>
16+
<input
17+
style={{width:"25%", textAlign:"center"}}
18+
type="number"
19+
data-testid="input"
20+
value= {step}
21+
onChange={(element)=>setStep(parseInt(element.target.value))} />
22+
<button data-testid="add-button"
23+
24+
onClick={()=>setCount(count + step)}>+</button>
25+
<h3 data-testid="step">{step}</h3>
26+
27+
</div>
28+
)
29+
}
30+
31+
export default Counter

src/Counter/Counter.test.js

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
import React from 'react';
2+
import { render, fireEvent } from '@testing-library/react';
3+
import Counter from './Counter';
4+
import "@testing-library/jest-dom/extend-expect"
5+
6+
let getByTestId
7+
beforeEach(()=>{
8+
const component = render(<Counter />)
9+
getByTestId = component.getByTestId
10+
})
11+
12+
13+
test("header renders with correct test", ()=> {
14+
15+
const headerElement = getByTestId("header");
16+
17+
expect(headerElement.textContent).toBe("My Counter");
18+
});
19+
20+
21+
test("counter element initially shows 0",()=>{
22+
23+
const counterElement = getByTestId("counter");
24+
25+
expect(counterElement.textContent).toBe("0");
26+
});
27+
28+
// test("input element initially shows 1",()=>{
29+
30+
// const inputElement = getByTestId("input");
31+
32+
// expect(inputElement.value).toBe("1");
33+
// });
34+
35+
test("add button renders +",()=>{
36+
37+
const addButton = getByTestId("add-button");
38+
39+
expect(addButton.textContent).toBe("+");
40+
});
41+
test("subtract button renders - ",()=>{
42+
43+
const subtractButton = getByTestId("subtract-button");
44+
45+
expect(subtractButton.textContent).toBe("-");
46+
});
47+
48+
// test("input element value can be changed and used",()=>{
49+
50+
// const inputElement = getByTestId("input");
51+
// fireEvent.change(inputElement,{target:{value: "5"}})
52+
// expect(inputElement.value).toBe("5");
53+
// });
54+
test("input element value can be changed and used",()=>{
55+
56+
const inputElement = getByTestId("input");
57+
const stepElement = getByTestId("step")
58+
59+
expect(inputElement.value).toBe("1");
60+
61+
fireEvent.change(inputElement,{target:{value: "5"}})
62+
expect(inputElement.value).toBe("5");
63+
expect(stepElement.textContent).toBe("5");
64+
});
65+
66+
test("clicking on + button adds 1 to counter initially",()=>{
67+
68+
const addButton = getByTestId("add-button");
69+
const counterElement = getByTestId("counter");
70+
expect(counterElement.textContent).toBe("0")
71+
fireEvent.click(addButton);
72+
73+
expect(counterElement.textContent).toBe("1")
74+
});
75+
76+
test("changing input value then clicking add works",()=>{
77+
78+
const addButton = getByTestId("add-button");
79+
const counterElement = getByTestId("counter");
80+
const inputElement = getByTestId("input")
81+
82+
fireEvent.change(inputElement,{target:{value: "5"}})
83+
fireEvent.click(addButton);
84+
85+
expect(counterElement.textContent).toBe("5")
86+
fireEvent.click(addButton);
87+
88+
expect(counterElement.textContent).toBe("10")
89+
});
90+
91+
test("changing input value then clicking subtract works",()=>{
92+
93+
const subtractButton = getByTestId("subtract-button");
94+
const counterElement = getByTestId("counter");
95+
const inputElement = getByTestId("input")
96+
97+
fireEvent.change(inputElement,{target:{value: "5"}})
98+
fireEvent.click(subtractButton);
99+
100+
expect(counterElement.textContent).toBe("-5")
101+
});
102+
103+
test("add then subtract should cancel back out to 0",()=>{
104+
105+
const addButton = getByTestId("add-button");
106+
const subtractButton = getByTestId("subtract-button")
107+
const counterElement = getByTestId("counter");
108+
const inputElement = getByTestId("input")
109+
110+
fireEvent.change(inputElement,{target:{value: "5"}})
111+
fireEvent.click(addButton);
112+
113+
expect(counterElement.textContent).toBe("5")
114+
fireEvent.click(subtractButton);
115+
116+
expect(counterElement.textContent).toBe("0")
117+
});
118+
119+
test("counter changes to green at value over 100",()=>{
120+
121+
const counterElement = getByTestId("counter");
122+
const inputElement = getByTestId("input");
123+
const addButton = getByTestId("add-button")
124+
fireEvent.change(inputElement,{target:{value: "50"}})
125+
fireEvent.click(addButton);
126+
fireEvent.click(addButton);
127+
128+
129+
expect(counterElement.className).toContain("green")
130+
});
131+
132+
test("counter changes to red at negative values",()=>{
133+
134+
const counterElement = getByTestId("counter");
135+
const inputElement = getByTestId("input");
136+
const subtractButton = getByTestId("subtract-button")
137+
fireEvent.change(inputElement,{target:{value: "50"}})
138+
fireEvent.click(subtractButton);
139+
fireEvent.click(subtractButton);
140+
141+
142+
expect(counterElement.className).toContain("red")
143+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "../.."
5+
}
6+
],
7+
"settings": {}
8+
}

0 commit comments

Comments
 (0)