Skip to content

Commit 487ff6e

Browse files
committed
03: a bunch of fixes
1 parent 23c1b6b commit 487ff6e

File tree

15 files changed

+24
-21
lines changed

15 files changed

+24
-21
lines changed

Diff for: exercises/03.best-practices/03.problem.network-mocking/README.mdx

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export const handlers = []
4848

4949
```ts filename=src/mocks/browser.ts
5050
import { setupWorker } from 'msw/browser'
51-
import { handlers } from './handlers.js'
51+
import { handlers } from './handlers'
5252

5353
export const worker = setupWorker(...handlers)
5454
```
@@ -96,7 +96,7 @@ import { test as testBase } from 'vitest'
9696
```ts filename=test-extend.ts add=3
9797
import { test as testBase } from 'vitest'
9898

99-
export const test = testBase.extend()
99+
export const test = testBase.extend({})
100100
```
101101

102102
> :owl: Calling `.extend()` allows you to put _custom properties_ you can then access on the `test()` function.
@@ -117,7 +117,7 @@ Right now, the `worker` property has an array of two elements: `fixture` and `op
117117

118118
```ts filename=test-extend.ts add=2,6-14
119119
import { test as testBase } from 'vitest'
120-
import { worker } from './src/mocks/browser.js'
120+
import { worker } from './src/mocks/browser'
121121

122122
export const test = testBase.extend({
123123
worker: [
@@ -144,7 +144,7 @@ await worker.start({
144144
})
145145
```
146146

147-
Here, you are starting the worker that enables request mocking in MSW. You are referencing the same `worker` object you've configured previously in `src/mocks/browser.js`.
147+
Here, you are starting the worker that enables request mocking in MSW. You are referencing the same `worker` object you've configured previously in `src/mocks/browser.ts`.
148148

149149
```ts nonumber
150150
await use(worker)
@@ -162,7 +162,7 @@ worker.resetHandlers()
162162

163163
This line makes sure that any mocks you add in individual tests are _reset_ between them. This achieves scope isolation and prevents network overrides from leaking to irrelevant tests.
164164

165-
```ts
165+
```ts nonumber
166166
worker.stop()
167167
```
168168

Diff for: exercises/03.best-practices/03.problem.network-mocking/src/discount-code-form.browser.test.tsx

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { page } from '@vitest/browser/context'
22
import { render } from 'vitest-browser-react'
33
import { http, HttpResponse } from 'msw'
4-
// 🐨 Import the `test` function from `test-extend.js`.
4+
// 🐨 Import the `test` function from `test-extend`.
55
// This custom `test` function exposes the `worker` object
66
// you will use to access and use MSW in tests.
7-
// 💰 import { test } from '../test-extend.js'
7+
// 💰 import { test } from '../test-extend'
88
import { DiscountCodeForm } from './discount-code-form'
99

1010
test('applies a discount code', async () => {
@@ -50,6 +50,8 @@ test('displays a warning for legacy discount codes', async ({
5050

5151
// 🐨 Write an assertion that expects the text element
5252
// with the applied discount code to be visible on the page.
53+
// 💰 "Discount: LEGA2000 (-10%)"
54+
// 💰 await expect.element(locator).toBeVisible()
5355
//
5456
// 🐨 Write another assertion that expected the warning
5557
// to appear, notifying the user about the legacy discount code.

Diff for: exercises/03.best-practices/03.solution.network-mocking/README.mdx

+2-2
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ import { App } from './app.jsx'
108108

109109
async function enableMocking() {
110110
if (process.env.NODE_ENV === 'development') {
111-
const { worker } = await import('./mocks/browser.js')
111+
const { worker } = await import('./mocks/browser')
112112
await worker.start()
113113
}
114114
}
@@ -126,7 +126,7 @@ enableMocking().then(() => {
126126
127127
This functions does a couple of things:
128128
129-
1. Imports the `./mocks/browser.js` module _conditionally_ to enable MSW only in development;
129+
1. Imports the `./mocks/browser.ts` module _conditionally_ to enable MSW only in development;
130130
1. Calls `await worker.start()` to actually register and start the Service Worker.
131131
132132
## Related materials

Diff for: exercises/03.best-practices/03.solution.network-mocking/src/discount-code-form.browser.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page } from '@vitest/browser/context'
22
import { render } from 'vitest-browser-react'
33
import { http, HttpResponse } from 'msw'
4-
import { test } from '../test-extend.js'
4+
import { test } from '../test-extend'
55
import { DiscountCodeForm, type Discount } from './discount-code-form'
66

77
test('applies a discount code', async () => {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { setupWorker } from 'msw/browser'
2-
import { handlers } from './handlers.js'
2+
import { handlers } from './handlers'
33

44
export const worker = setupWorker(...handlers)

Diff for: exercises/03.best-practices/03.solution.network-mocking/src/mocks/handlers.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { http, HttpResponse } from 'msw'
2-
import type { Discount } from '../discount-code-form.js'
2+
import type { Discount } from '../discount-code-form'
33

44
export const handlers = [
55
http.post<never, string, Discount>(

Diff for: exercises/03.best-practices/04.problem.element-presence/src/discount-code-form.browser.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page } from '@vitest/browser/context'
22
import { render } from 'vitest-browser-react'
33
import { http, HttpResponse } from 'msw'
4-
import { test } from '../test-extend.js'
4+
import { test } from '../test-extend'
55
import { DiscountCodeForm, type Discount } from './discount-code-form'
66

77
test('applies a discount code', async () => {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { setupWorker } from 'msw/browser'
2-
import { handlers } from './handlers.js'
2+
import { handlers } from './handlers'
33

44
export const worker = setupWorker(...handlers)

Diff for: exercises/03.best-practices/04.solution.element-presence/src/discount-code-form.browser.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page } from '@vitest/browser/context'
22
import { render } from 'vitest-browser-react'
33
import { http, HttpResponse } from 'msw'
4-
import { test } from '../test-extend.js'
4+
import { test } from '../test-extend'
55
import { DiscountCodeForm, type Discount } from './discount-code-form'
66

77
test('applies a discount code', async () => {
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { setupWorker } from 'msw/browser'
2-
import { handlers } from './handlers.js'
2+
import { handlers } from './handlers'
33

44
export const worker = setupWorker(...handlers)

Diff for: exercises/03.best-practices/05.problem.page-navigation/src/discount-code-form.browser.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { page } from '@vitest/browser/context'
22
import { render } from 'vitest-browser-react'
33
import { http, HttpResponse } from 'msw'
4-
import { test } from '../test-extend.js'
4+
import { test } from '../test-extend'
55
import { DiscountCodeForm, type Discount } from './discount-code-form'
66

77
// 🐨 Declare a new variable called `wrapper`.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { setupWorker } from 'msw/browser'
2-
import { handlers } from './handlers.js'
2+
import { handlers } from './handlers'
33

44
export const worker = setupWorker(...handlers)

Diff for: exercises/03.best-practices/05.solution.page-navigation/src/discount-code-form.browser.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { page } from '@vitest/browser/context'
22
import { render } from 'vitest-browser-react'
33
import { http, HttpResponse } from 'msw'
44
import { MemoryRouter } from 'react-router'
5-
import { test } from '../test-extend.js'
5+
import { test } from '../test-extend'
66
import { DiscountCodeForm, type Discount } from './discount-code-form.js'
77

88
const wrapper: React.JSXElementConstructor<{
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
import { setupWorker } from 'msw/browser'
2-
import { handlers } from './handlers.js'
2+
import { handlers } from './handlers'
33

44
export const worker = setupWorker(...handlers)

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
"lint": "eslint .",
2626
"format": "prettier --write .",
2727
"typecheck": "tsc -b",
28-
"validate:all": "npm-run-all --parallel --print-label --print-name --continue-on-error test:all lint typecheck"
28+
"validate:all": "npm-run-all --parallel --print-label --print-name --continue-on-error test:all lint typecheck",
29+
"clean": "rm -rf ./exercises/**/__screenshots__ && rm -f ./exercises/**/*.tsbuildinfo"
2930
},
3031
"keywords": [],
3132
"author": "Artem Zakharchenko <[email protected]> (https://kettanaito.com/)",

0 commit comments

Comments
 (0)