Skip to content

Commit e299c91

Browse files
committed
typos and flow
1 parent 2edabda commit e299c91

File tree

1 file changed

+12
-12
lines changed
  • exercises/03.best-practices/03.problem.network-mocking

1 file changed

+12
-12
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
# Network mocking
22

3-
We've got quite far testing the `<DiscountCodeForm />` component but there's just one problem. The component has been a bit naive. It simply took the entered discount code and put it in internal state. That's not how things work in real life. In practice, it would likely send that code to some server to validate and apply. It would make a _network call_.
3+
So far, our `<DiscountCodeForm />` component has been handling discount codes in a simplified way — just storing them in internal state. In a real application, the component would need to validate and apply these codes through a server, making a _network call_.
44

5-
In fact, I went and refactored the discount code component to do just that! Now, submitting the form dispatches a `POST https://api.example.com/discount/code` request with the provided code. Only once the server sends back the confirmation will the component display the applied discount in the UI.
5+
In fact, I went and refactored the discount code component to do just that! Now, submitting the form dispatches a `POST https://api.example.com/discount/code` request with the provided code. Only when the server sends back the confirmation will the component display the applied discount in the UI.
66

77
This poses a new challenge: that `POST` request _will actually happen during the test run_. That may be acceptable in end-to-end testing but never on the integration level.
88

99
<callout-success>Always mock HTTP requests in integration tests.</callout-success>
1010

11-
Leaving that request to fire during tests means that we are allowing the server's operability and behavior to influence the outcome of the test. This is a no-go as it violates :scroll: [The Golden Rule of Assertions](https://www.epicweb.dev/the-golden-rule-of-assertions):
11+
Leaving that request to fire during tests means that we are allowing the server's operability and behavior to influence the outcome of the test. This is a problem, as it violates :scroll: [The Golden Rule of Assertions](https://www.epicweb.dev/the-golden-rule-of-assertions):
1212

1313
<callout-info>_A test must fail if, and only if, the intention behind the system is not met._</callout-info>
1414

15-
In other words, our component can be functioning perfectly but the test may still fail if the server is down or having trouble processing the request. The responsibility of the integration test is to assert the client-side code we've written, and never the server-side one.
15+
In other words, even if our component works perfectly, the test could fail due to server issues. Integration tests should focus solely on client-side behavior, not server responses.
1616

17-
You have to draw the line. You have to draw a :scroll: [Test boundary](https://www.epicweb.dev/what-is-a-test-boundary).
17+
You have to draw a line between these responsibilities. You have to draw a :scroll: [Test boundary](https://www.epicweb.dev/what-is-a-test-boundary).
1818

19-
When it comes to the network, you employ API mocking tools to establish that boundary and help your tests focus on the right thing, turning the dynamic and unpredictable thing such as network to fixed and given.
19+
When it comes to the network, you employ API mocking tools to establish this boundary and help your tests focus on the right thing, turning the dynamic and unpredictable thing such as network to fixed and given.
2020

21-
In this exercise, we would have toto intercept and mock the `POST https://api.example.com/discount/code` that our component makes during tests. We will use the library called [Mock Service Worker](https://mswjs.io/) (MSW) to do that.
21+
In this exercise, we have to intercept and mock the `POST https://api.example.com/discount/code` request that our component makes during tests. We will use the library called [Mock Service Worker](https://mswjs.io/) (MSW) to do that.
2222

2323
## Using MSW with Vitest Browser Mode
2424

25-
Integrating MSW with Vitest Browser Mode is a little different from the usual browser integration so let's go throuigh it together. Follow the instructions below.
25+
Integrating MSW with Vitest Browser Mode is a little different from the usual browser integration so let's go through it together. Follow the instructions below.
2626

2727
### Installation and setup
2828

@@ -190,7 +190,7 @@ export const test = testBase.extend({
190190
})
191191
```
192192
193-
> :owl: Passing `auto: true` to a fixture will make Vitest initialize it _automatically_, even if it's not explicitly referenced in the test.
193+
> :owl: Passing `auto: true` to a fixture will make Vitest initialize it _automatically_, even if it's not explicitly referenced in a test.
194194
195195
With the setup finally done, let's move on to describing the network.
196196
@@ -202,7 +202,7 @@ With the setup finally done, let's move on to describing the network.
202202
import { http, HttpResponse } from 'msw'
203203
```
204204
205-
Now is the turn to _describe the network_ you want. In MSW, you do that using functions called [Request handlers](https://mswjs.io/docs/concepts/request-handler) that are responsible for two things: intercepting requests and deciding how to handle them.
205+
Now is your turn to _describe the network_ you want. In MSW, you do that using functions called [Request handlers](https://mswjs.io/docs/concepts/request-handler) that are responsible for two things: intercepting requests and deciding how to handle them.
206206
207207
🐨 In `src/mocks/handlers.ts`, export an array called `handlers` and declare your first request handler for the `POST https://api.example.com/discount/code` request in it:
208208

@@ -214,7 +214,7 @@ export const handlers = [
214214
]
215215
```
216216

217-
This request handler will intercept any matching requests and execute the callback function you've provided as the second argument. That function—also called a _response resolver_—doesn't do anything thought! Let's fix that.
217+
This request handler will intercept any matching requests and execute the callback function you've provided as the second argument. That function—also called a _response resolver_—doesn't do anything, though! Let's fix that.
218218

219219
Our component expects a response from the server to have the following shape:
220220

@@ -230,7 +230,7 @@ Our component expects a response from the server to have the following shape:
230230
231231
Since the `code` data comes from the client, let's read the intercepted request's body to retrieve the sent discount code.
232232
233-
🐨 In the response resolver function, access the `request` property on the argument object to the response resolver and read the reqest's body ...
233+
🐨 In the response resolver function, access the `request` property on the argument object to the response resolver and read the request's body:
234234
235235
```ts filename=src/mocks/handlers.ts add=7-8
236236
import { http, HttpResponse } from 'msw'

0 commit comments

Comments
 (0)