Skip to content

Commit 216bf34

Browse files
committed
add last tests
1 parent 54b3169 commit 216bf34

File tree

9 files changed

+144
-9
lines changed

9 files changed

+144
-9
lines changed

exercises/03.use-effect/01.problem.callback/README.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
```tsx
88
useEffect(() => {
9-
console.log('consider yourself effective!')
9+
console.info('consider yourself effective!')
1010
})
1111
```
1212

exercises/03.use-effect/01.problem.callback/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export function useState<State>(initialState: State) {
3131

3232
// 🐨 create a useEffect function here that accepts an "EffectCallback" callback,
3333
// and adds the callback to the effects array at the index "hookIndex++"
34+
// 🚨 make sure to export this function so I can test it
3435

3536
function Counter() {
3637
const [count, setCount] = useState(0)
@@ -40,7 +41,7 @@ function Counter() {
4041
const toggle = () => setEnabled(!enabled)
4142

4243
useEffect(() => {
43-
console.log('consider yourself effective!')
44+
console.info('consider yourself effective!')
4445
})
4546

4647
return (
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { expect, testStep } from '@epic-web/workshop-utils/test'
2+
import { screen, waitFor } from '@testing-library/dom'
3+
import { userEvent } from '@testing-library/user-event'
4+
5+
const originalConsoleInfo = console.info
6+
7+
function info(...args: Array<any>) {
8+
info.calls.push(args)
9+
return originalConsoleInfo(...args)
10+
}
11+
info.calls = [] as Array<Array<any>>
12+
13+
console.info = info
14+
15+
await import('.')
16+
17+
await testStep(
18+
({ type }) =>
19+
type === 'pass'
20+
? 'The effect callback was called on the initial render'
21+
: 'The effect callback was not called on the initial render. Did you call it after rendering and using flushSync?',
22+
() => {
23+
expect(info.calls.length).to.equal(1)
24+
},
25+
)
26+
27+
const counterButton = await testStep(
28+
({ type }) =>
29+
type === 'fail'
30+
? 'Could not find the counter button. It should start at 0. Did you forget to return the initial state from your useState?'
31+
: 'Found the counter button that starts at 0',
32+
() => screen.findByRole('button', { name: /0/i }),
33+
)
34+
await userEvent.click(counterButton)
35+
await testStep(`The button text should be 1 after clicking`, () =>
36+
waitFor(() => expect(counterButton).to.have.text('1')),
37+
)
38+
39+
await testStep(
40+
({ type }) =>
41+
type === 'pass'
42+
? 'The effect callback was called after the initial render'
43+
: 'The effect callback was not called after the initial render. Did you call it after rendering and using flushSync?',
44+
() => {
45+
expect(info.calls.length).to.equal(2)
46+
},
47+
)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { expect, testStep } from '@epic-web/workshop-utils/test'
2+
3+
const { useEffect } = await import('./index.tsx')
4+
5+
await testStep(
6+
({ type }) =>
7+
type === 'fail'
8+
? 'Did you forget to export your `useEffect`? I need you to export it so I can test it.'
9+
: 'useEffect is exported correctly',
10+
() => expect(useEffect).to.be.a('function'),
11+
)
12+
13+
await testStep(
14+
({ type }) =>
15+
type === 'pass'
16+
? 'useEffect is returning undefined'
17+
: 'useEffect is not returning undefined',
18+
() => {
19+
expect(useEffect(() => {})).to.be.undefined
20+
},
21+
)

exercises/03.use-effect/01.solution.callback/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ function Counter() {
3939
const toggle = () => setEnabled(!enabled)
4040

4141
useEffect(() => {
42-
console.log('consider yourself effective!')
42+
console.info('consider yourself effective!')
4343
})
4444

4545
return (

exercises/03.use-effect/02.problem.dependencies/README.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
```tsx
88
useEffect(() => {
99
if (enabled) {
10-
console.log('consider yourself effective!')
10+
console.info('consider yourself effective!')
1111
} else {
12-
console.log('consider yourself ineffective!')
12+
console.info('consider yourself ineffective!')
1313
}
1414
}, [enabled])
1515
```

exercises/03.use-effect/02.problem.dependencies/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ function Counter() {
4343

4444
useEffect(() => {
4545
if (enabled) {
46-
console.log('consider yourself effective!')
46+
console.info('consider yourself effective!')
4747
} else {
48-
console.log('consider yourself ineffective!')
48+
console.info('consider yourself ineffective!')
4949
}
5050
// @ts-expect-error 💣 delete this comment
5151
}, [enabled])
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { expect, testStep } from '@epic-web/workshop-utils/test'
2+
import { screen, waitFor } from '@testing-library/dom'
3+
import { userEvent } from '@testing-library/user-event'
4+
5+
const originalConsoleInfo = console.info
6+
7+
function info(...args: Array<any>) {
8+
info.calls.push(args)
9+
return originalConsoleInfo(...args)
10+
}
11+
info.calls = [] as Array<Array<any>>
12+
13+
console.info = info
14+
15+
await import('.')
16+
17+
await testStep(
18+
({ type }) =>
19+
type === 'pass'
20+
? 'The effect callback was called on the initial render'
21+
: 'The effect callback was not called on the initial render. Did you call it after rendering and using flushSync?',
22+
() => {
23+
expect(info.calls.length).to.equal(1)
24+
},
25+
)
26+
27+
const counterButton = await testStep(
28+
({ type }) =>
29+
type === 'fail'
30+
? 'Could not find the counter button. It should start at 0. Did you forget to return the initial state from your useState?'
31+
: 'Found the counter button that starts at 0',
32+
() => screen.findByRole('button', { name: /0/i }),
33+
)
34+
await userEvent.click(counterButton)
35+
await testStep(`The button text should be 1 after clicking`, () =>
36+
waitFor(() => expect(counterButton).to.have.text('1')),
37+
)
38+
39+
await testStep(
40+
({ type }) =>
41+
type === 'pass'
42+
? 'The effect callback was not called when dependencies are unchanged'
43+
: 'The effect callback was called when dependencies are unchanged. Did you remember to not call it when the dependencies are unchanged?',
44+
() => {
45+
expect(info.calls.length).to.equal(1)
46+
},
47+
)
48+
49+
const disableButton = await testStep(
50+
({ type }) =>
51+
type === 'fail'
52+
? 'Could not find the disable button. It should start with the text "disable". Did you forget to return the initial state from your useState?'
53+
: 'Found the disable button that starts with "disable" text',
54+
() => screen.findByRole('button', { name: /Disable/i }),
55+
)
56+
await userEvent.click(disableButton)
57+
58+
await testStep(
59+
({ type }) =>
60+
type === 'pass'
61+
? 'The effect callback was called when dependencies change'
62+
: 'The effect callback was not called when dependencies change. Did you remember to call it when the dependencies change?',
63+
() => {
64+
expect(info.calls.length).to.equal(2)
65+
},
66+
)

exercises/03.use-effect/02.solution.dependencies/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ function Counter() {
4242

4343
useEffect(() => {
4444
if (enabled) {
45-
console.log('consider yourself effective!')
45+
console.info('consider yourself effective!')
4646
} else {
47-
console.log('consider yourself ineffective!')
47+
console.info('consider yourself ineffective!')
4848
}
4949
}, [enabled])
5050

0 commit comments

Comments
 (0)