-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcomponents.mock.js
More file actions
300 lines (255 loc) · 6.98 KB
/
Copy pathcomponents.mock.js
File metadata and controls
300 lines (255 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
import axios from 'axios/lib/axios'
import React, { Component, Fragment, useState, useEffect } from 'react'
import { createPortal } from 'react-dom'
import { Provider, useDispatch, useSelector } from 'react-redux'
import { Router, BrowserRouter, Switch, Route } from 'react-router-dom'
import { createBrowserHistory } from 'history'
import { createStore, applyMiddleware } from 'redux'
import thunk from 'redux-thunk'
export const MyComponent = () => <div>Foo</div>
export const MyComponentWithProps = props => (
<div>{props && Object.entries(props).map(prop => prop)}</div>
)
export const MyComponentWithPortal = ({ children }) =>
createPortal(children, document.getElementById('portal-root-id'))
export class MyComponentMakingHttpCalls extends Component {
state = {
quantity: 0,
isSaved: false,
}
componentDidMount = async () => {
const request = new Request('my-host/path/to/get/quantity/')
const quantityResponse = await fetch(request)
if (!quantityResponse) return
if (quantityResponse.status !== 200) {
this.setState({ quantity: 'error' })
return
}
const quantity = await quantityResponse.json()
this.setState({ quantity })
}
saveQuantity = async () => {
const request = new Request('my-host/path/to/save/quantity/', {
method: 'POST',
body: JSON.stringify({ quantity: this.state.quantity }),
})
try {
await fetch(request)
this.setState({ isSaved: true })
} catch (e) {
this.setState({ isSaved: false })
}
}
render = () => (
<div data-testid="quantity" onClick={ this.saveQuantity }>
<span>quantity: {this.state.quantity}</span>
{this.state.isSaved && <i aria-label="quantity saved" />}
</div>
)
}
export const myFakeModule = {
myFakeFunction: () => null,
}
const Home = ({ history }) => {
const goToCategories = () => history.push('/categories')
myFakeModule.myFakeFunction('HOME')
return (
<div>
Home
<button onClick={ goToCategories }>Go to categories</button>
</div>
)
}
const Categories = () => {
return <div>Categories</div>
}
export const history = createBrowserHistory()
export const MyAppWithRouting = () => {
return (
<Router history={ history }>
<Switch>
<Route key="home" path="/" component={ Home } exact={ true } />
<Route
key="categories"
path="/categories"
component={ Categories }
exact={ true }
/>
</Switch>
</Router>
)
}
export const MyAppWithBrowserRouting = () => {
return (
<BrowserRouter>
<Switch>
<Route key="home" path="/" component={ Home } exact={ true } />
<Route
key="categories"
path="/categories"
component={ Categories }
exact={ true }
/>
</Switch>
</BrowserRouter>
)
}
const ACTION_TYPES = {
ADD: 'ADD',
REMOVE: 'REMOVE',
}
const add = () => ({ type: ACTION_TYPES.ADD })
const remove = () => dispatch => dispatch({ type: ACTION_TYPES.REMOVE })
function reducer(state = { products: 10 }, action) {
switch (action.type) {
case ACTION_TYPES.ADD:
return {
products: state.products + 1,
}
case ACTION_TYPES.REMOVE:
return {
products: state.products - 1,
}
default:
return state
}
}
const Cart = () => {
const { products } = useSelector(state => state)
const dispatch = useDispatch()
return (
<div>
<p>{products}</p>
<button onClick={ () => dispatch(add()) }>+</button>
<button onClick={ () => dispatch(remove()) }>-</button>
</div>
)
}
export const MyAppWithStore = () => {
return (
<Provider
store={ createStore(reducer, { products: 10 }, applyMiddleware(thunk)) }
>
<Cart />
</Provider>
)
}
export const MyComponentMakingHttpCallsWithQueryParams = () => {
const [quantity, setQuantity] = useState(0)
useEffect(() => {
getQuantity()
}, [])
const getQuantity = async () => {
const request = new Request('/path/with/query/params/?myAwesome=param')
const response = await fetch(request)
if (!response) return
const quantity = await response.json()
setQuantity(quantity)
}
return <span>quantity: {quantity}</span>
}
export const MyComponentWithNetwork = () => {
const [status, setStatus] = useState(null)
const [quantity, setQuantity] = useState(null)
useEffect(() => {
doRequest()
doRequestWithResponse()
}, [])
const doRequest = async () => {
const request = new Request('my-host/path/')
const response = await fetch(request)
if (response) {
setStatus('SUCCESS')
}
}
const doRequestWithResponse = async () => {
const request = new Request('my-host/path/with/response/')
const response = await fetch(request)
const quantity = await response.json()
setQuantity(quantity)
}
return (
<div>
<div>MyComponentWithNetwork</div>
<div>{status}</div>
<div>{quantity}</div>
</div>
)
}
export const MyComponentWithLogin = () => {
const [username, setUser] = useState(null)
const [status, setStatus] = useState(null)
useEffect(() => {
login()
}, [])
const login = async () => {
const request = new Request('my-host/path/to/login/', { method: 'POST' })
const response = await fetch(request)
const username = await response.json()
setUser(username)
setStatus('LOGIN')
}
const logout = async () => {
const request = new Request('my-host/path/to/logout/', { method: 'POST' })
const response = await fetch(request)
const username = await response.json()
setUser(username)
setStatus('LOGOUT')
}
if (status === 'LOGIN') {
return (
<div>
<span>Logged in as {username}</span>
<button onClick={ logout }>Logout</button>
</div>
)
}
if (status === 'LOGOUT') return <span>Logged out as {username}</span>
return <span>Not logged</span>
}
export const MyComponentWithPost = () => {
const [username, setUser] = useState(null)
const [status, setStatus] = useState(null)
useEffect(() => {
login()
}, [])
const login = async () => {
const request = new Request('my-host/path/to/login/', {
method: 'POST',
body: JSON.stringify({
bar: 'bar',
foo: 'foo',
user: {
username: 'Fran',
password: 'secret',
},
}),
})
const response = await fetch(request)
const username = await response.json()
setUser(username)
setStatus('LOGIN')
}
if (status === 'LOGIN') {
return <span>Logged in as {username}</span>
}
return <span>Not logged</span>
}
export const MyComponentWithFeedback = () => {
const [feedback, setFeedback] = useState('DEFAULT')
const save = async () => {
const request = new Request('my-host/path/to/save/', {
method: 'POST',
})
const response = await fetch(request)
if(!response) return
const { name } = await response.json()
setFeedback(name)
}
return (
<div>
<button onClick={ save }>save</button>
<span>{feedback}</span>
</div>
)
}