Skip to content

Commit 6c2e301

Browse files
committed
add initial tests
1 parent 485406f commit 6c2e301

File tree

23 files changed

+4978
-282
lines changed

23 files changed

+4978
-282
lines changed

epicshop/package-lock.json

+56-53
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

epicshop/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"type": "module",
33
"dependencies": {
4-
"@epic-web/workshop-app": "^4.12.5",
5-
"@epic-web/workshop-utils": "^4.12.5",
4+
"@epic-web/workshop-app": "^4.13.3",
5+
"@epic-web/workshop-utils": "^4.13.3",
66
"chokidar": "^3.6.0",
77
"execa": "^9.3.0",
88
"fs-extra": "^11.2.0"

exercises/01.use-state/01.problem.initial-state/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { createRoot } from 'react-dom/client'
77
// an array of the state and a no-op function: () => {}
88
// 🦺 note you may need to ignore some typescript errors here. We'll fix them later.
99
// Feel free to make the `useState` a generic though!
10+
// ⚠️ don't forget to `export` your `useState` function so the tests can find it
1011

1112
function Counter() {
1213
const [count, setCount] = useState(0)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
await import('.')
6+
7+
const button = await testStep(
8+
({ type }) =>
9+
type === 'fail'
10+
? 'Could not find the counter button. It should start at 0. Did you forget to return the initial state from your useState?'
11+
: 'Found the counter button that starts at 0',
12+
() => screen.findByRole('button', { name: /0/i }),
13+
)
14+
await userEvent.click(button)
15+
await testStep(
16+
`The button text should still be 0 because our useState isn't working yet`,
17+
() => waitFor(() => expect(button).to.have.text('0')),
18+
)

exercises/01.use-state/01.solution.initial-state/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRoot } from 'react-dom/client'
22

3-
function useState<State>(initialState: State) {
3+
export function useState<State>(initialState: State) {
44
const state = initialState
55
const setState = () => {}
66
return [state, setState] as const
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { expect, testStep } from '@epic-web/workshop-utils/test'
2+
3+
const { useState } = await import('./index.tsx')
4+
5+
await testStep(
6+
({ type }) =>
7+
type === 'fail'
8+
? 'Did you forget to export your `useState`? I need you to export it so I can test it.'
9+
: 'useState is exported correctly',
10+
() => expect(useState).to.be.a('function'),
11+
)
12+
13+
// eslint-disable-next-line react-hooks/rules-of-hooks
14+
const [state, setState] = useState(5)
15+
16+
await testStep(
17+
({ type }) =>
18+
type === 'pass'
19+
? 'useState is returning the initial state'
20+
: 'useState is not returning the initial state',
21+
() => {
22+
expect(state).to.equal(5)
23+
},
24+
)
25+
26+
await testStep(
27+
({ type }) =>
28+
type === 'pass'
29+
? 'useState is returning a function'
30+
: 'useState is not returning a function',
31+
() => {
32+
expect(setState).to.be.a('function')
33+
},
34+
)

exercises/01.use-state/02.problem.update-state/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRoot } from 'react-dom/client'
22

3-
function useState<State>(initialState: State) {
3+
export function useState<State>(initialState: State) {
44
// 🐨 change this to let
55
const state = initialState
66
// 🐨 update this to accept newState and assign state to that
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
await import('.')
6+
7+
const button = await testStep(
8+
({ type }) =>
9+
type === 'fail'
10+
? 'Could not find the counter button. It should start at 0. Did you forget to return the initial state from your useState?'
11+
: 'Found the counter button that starts at 0',
12+
() => screen.findByRole('button', { name: /0/i }),
13+
)
14+
await userEvent.click(button)
15+
await testStep(
16+
`The button text should still be 0 because our useState isn't working yet`,
17+
() => waitFor(() => expect(button).to.have.text('0')),
18+
)

exercises/01.use-state/02.solution.update-state/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRoot } from 'react-dom/client'
22

3-
function useState<State>(initialState: State) {
3+
export function useState<State>(initialState: State) {
44
let state = initialState
55
const setState = (newState: State) => (state = newState)
66
return [state, setState] as const

exercises/01.use-state/03.problem.re-render/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRoot } from 'react-dom/client'
22

3-
function useState<State>(initialState: State) {
3+
export function useState<State>(initialState: State) {
44
let state = initialState
55
// 🐨 update this function to call render after setting the state
66
const setState = (newState: State) => (state = newState)

exercises/01.use-state/03.solution.re-render/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createRoot } from 'react-dom/client'
22

3-
function useState<State>(initialState: State) {
3+
export function useState<State>(initialState: State) {
44
let state = initialState
55
const setState = (newState: State) => {
66
state = newState

exercises/01.use-state/04.problem.preserve-state/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createRoot } from 'react-dom/client'
33
// 🐨 create state and setState variables here using let
44
// 🦺 set their type to "any"
55

6-
function useState<State>(initialState: State) {
6+
export function useState<State>(initialState: State) {
77
// 🐨 remove the "let" and "const" here so this function references the
88
// variables declared above
99
// 🐨 Next, change this so we only do these assignments if the state is undefined

exercises/01.use-state/04.solution.preserve-state/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { createRoot } from 'react-dom/client'
22

33
let state: any, setState: any
44

5-
function useState<State>(initialState: State) {
5+
export function useState<State>(initialState: State) {
66
if (state === undefined) {
77
state = initialState
88
setState = (newState: State) => {

exercises/02.multiple-hooks/01.problem.phase/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { createRoot } from 'react-dom/client'
99

1010
let state: any, setState: any
1111

12-
function useState<State>(initialState: State) {
12+
export function useState<State>(initialState: State) {
1313
// 🐨 change this to check whether the phase is INITIALIZATION
1414
if (state === undefined) {
1515
state = initialState

0 commit comments

Comments
 (0)