Skip to content

Commit 2bfcfd9

Browse files
authored
[Changed] Update readme for with network builder (#77)
* [Changed] include how to use withNetwork builder * [Changed] remove withMocks references * [Added] warning console for drepecated withMocks feature * [Changed] improve readme content
1 parent 932137d commit 2bfcfd9

4 files changed

Lines changed: 44 additions & 31 deletions

File tree

README.md

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ and add the previous file in `jest.config.json`
4949
```
5050

5151
## 🏰 Builder API
52-
#### withMocks
53-
By using this you let your components know what `http requests` will respond. It works matching the request url which is `host` + `path`, the request `method` and the `requestBody`. All three need to match, otherwise it will raise an exception to let you know that one of your components is doing an `http request` that is not being handled.
52+
53+
#### withMocks (Deprecated)
54+
55+
It has the same API than the withNetwork builder. The main difference between them is that withMocks will fail if a given request, done by the production code, is not set up in the `responses object`.
56+
57+
#### withNetwork
58+
By using this feature you can configure the responses for your `http requests`. If your component is making a request that is not set up in the `responses object`, it will not be validated and it will return an empty response with code 200.
59+
5460
```
5561
import { wrap } from '@mercadona/mo.library.burrito'
5662
@@ -64,35 +70,18 @@ const responses = {
6470
}
6571
6672
wrap(MyComponent)
67-
.withMocks(responses)
73+
.withNetwork(responses)
6874
.mount()
6975
```
70-
`host`, `method` and `status` will be the same most of the cases, we don't want to specify them every single time.
71-
72-
By default 🌯 burrito is testing the query params in your responses, but if you want to ignore it you must add the `handleQueryParams` param in the config and to test it you can use the `catchParams` property in the request, like this:
73-
74-
```
75-
import { configure } from '@mercadona/mo.library.burrito'
76-
77-
configure({ handleQueryParams: true })
7876

79-
wrap(MyComponentUsingQueryParams)
80-
.withMocks({
81-
path: '/path/with/query/params/?myAwesome=param',
82-
responseBody: '15',
83-
catchParams: true,
84-
})
85-
.mount()
86-
```
87-
88-
While `host` has a default value specified by using the `configure`:
77+
You can specify the default `host` via configuration:
8978
```
9079
import { configure } from '@mercadona/mo.library.burrito'
9180
9281
const { API_HOST, API_VERSION } = process.env
9382
configure({ defaultHost: `${ API_HOST }${ API_VERSION }` })
9483
```
95-
`method` as a default value of `'get'` and `status` is `200`. This means one can use `withMocks` like this:
84+
In addition, Burrito defaults the `method` to `'get'` and `status` to `200`. This means one can use `withNetwork` like this:
9685
```
9786
import { wrap } from '@mercadona/mo.library.burrito'
9887
@@ -102,7 +91,7 @@ const responses = {
10291
}
10392
10493
wrap(MyComponent)
105-
.withMocks(responses)
94+
.withNetwork(responses)
10695
.mount()
10796
```
10897
Now, you might need to mock several `http responses` at the same time and that's why you can also pass an array of responses instead if you wish:
@@ -124,7 +113,7 @@ const responses = [
124113
]
125114
126115
wrap(MyComponent)
127-
.withMocks(responses)
116+
.withNetwork(responses)
128117
.mount()
129118
```
130119

@@ -204,7 +193,7 @@ const responses = {
204193
205194
wrap(PreparationContainer)
206195
.atPath('/products/1')
207-
.withMocks(responses)
196+
.withNetwork(responses)
208197
.withProps()
209198
.withPortalAt('modal-root')
210199
.mount()
@@ -220,7 +209,7 @@ import { wrap, assertions } from '@mercadona/mo.library.burrito'
220209
expect.extend(assertions)
221210
222211
wrap(MyComponentMakingHttpCalls)
223-
.withMocks(responses)
212+
.withNetwork(responses)
224213
.mount()
225214
226215
expect('/some/path').toHaveBeenFetchedWith({
@@ -242,7 +231,7 @@ const responses = [
242231
]
243232
244233
wrap(MyComponentMakingHttpCalls)
245-
.withMocks(responses)
234+
.withNetwork(responses)
246235
.mount()
247236
248237
expect(responses).toMatchNetworkRequests()

src/wrap.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React from 'react'
2+
import { yellow } from 'chalk'
23

34
import { mockFetch } from './mockFetch'
45
import { mockNetwork } from './mockNetwork'
@@ -56,7 +57,10 @@ const wrap = options => {
5657
withProps: props => wrap({ ...options, props }),
5758
withPortalAt: portalRootId =>
5859
wrap({ ...options, portalRootId, hasPortal: true }),
59-
withMocks: responses => wrap({ ...options, responses, hasMocks: true }),
60+
withMocks: responses => {
61+
console.warn(yellow('withMocks is deprecated. Use withNetwork instead.'))
62+
return wrap({ ...options, responses, hasMocks: true })
63+
},
6064
withNetwork: (responses = []) => {
6165
const listOfResponses = Array.isArray(responses) ? responses : [responses]
6266
return wrap({

tests/notUtilizedResponses.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,9 @@ it('should not warn when all the responses are being used', async () => {
8585

8686
await wait(() => {
8787
highlightNotUtilizedResponses()
88-
expect(consoleWarn).not.toHaveBeenCalled()
88+
expect(consoleWarn).not.toHaveBeenCalledWith(
89+
expect.stringContaining('the following responses are not being used')
90+
)
8991
})
9092
})
9193

@@ -103,6 +105,8 @@ it('should not warn when all the multiple responses are being used', async () =>
103105

104106
await wait(() => {
105107
highlightNotUtilizedResponses()
106-
expect(consoleWarn).not.toHaveBeenCalled()
108+
expect(consoleWarn).not.toHaveBeenCalledWith(
109+
expect.stringContaining('the following responses are not being used')
110+
)
107111
})
108-
})
112+
})

tests/withMocks.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ it('should mock different responses given the same request', async () => {
8888
)
8989
})
9090

91+
it('should show the deprecated feature warning', async() => {
92+
console.warn = jest.fn()
93+
configure({ mount: render })
94+
const { container } = wrap(MyComponentMakingHttpCalls)
95+
.withMocks({ method: 'get', path: '/path/to/get/quantity/', host: 'my-host', responseBody: '15', status: 200 })
96+
.mount()
97+
98+
expect(container).toHaveTextContent('quantity: 0')
99+
100+
await wait(() =>
101+
expect(console.warn).toHaveBeenCalledWith(
102+
expect.stringContaining('withMocks is deprecated. Use withNetwork instead.')
103+
)
104+
)
105+
})
106+
91107
it('should not have enough responses specified', async () => {
92108
configure({ defaultHost: 'my-host', mount: render })
93109
console.warn = jest.fn()

0 commit comments

Comments
 (0)