forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
179 lines (144 loc) · 4.55 KB
/
app.js
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
/* global window, document, fetch */
/* eslint-disable no-console */
function updateFavoriteFruits (contents) {
if (typeof contents !== 'string') {
contents = contents.map((item) => `<li>${item}</li>`).join('')
}
document.querySelector('.favorite-fruits').innerHTML = contents
}
function getFavoriteFruits () {
const favFruits = document.querySelector('.favorite-fruits')
if (!favFruits) {
return
}
favFruits.innerHTML = '<div class="loader"></div>'
fetch('/favorite-fruits')
.then((response) => {
/* eslint-disable-next-line no-console */
console.log('fetch response', response)
if (response.ok) {
return response.json()
}
const errorMessage = response.headers.get('status-text') || response.statusText
throw new Error(errorMessage)
})
.then((response) => {
/* eslint-disable-next-line no-console */
console.log('server response', response)
updateFavoriteFruits(response.length ? response : 'No favorites')
})
.catch((error) => {
updateFavoriteFruits(`Failed loading favorite fruits: ${error.message}`)
})
}
getFavoriteFruits()
setInterval(getFavoriteFruits, 30000)
const loadUsers = (nUsers = 3) => {
return () => {
// using delay parameter to make sure our tests
// are solid and can handle the Ajax request firing after a delay
setTimeout(() => {
console.log('loading %d users', nUsers)
document.querySelector('#users').innerText = ''
fetch(`https://jsonplaceholder.cypress.io/users?_limit=${nUsers}`)
.then((r) => r.json())
.then((users) => {
console.table(users)
const usersHtml = users.map((user) => {
return `<li class="user">${user.id} - ${user.email}</li>`
}).join('\n')
document.querySelector('#users').innerHTML = usersHtml
})
.catch((e) => {
console.error('problem fetching users', e)
document.querySelector('#users').innerText = `Problem fetching users ${e.message}`
})
}, 200)
}
}
const loadUser = (id) => {
return () => {
console.log('loading user #%d', id)
document.querySelector('#users').innerText = ''
fetch(`https://jsonplaceholder.cypress.io/users/${id}`)
.then((r) => r.json())
.then((user) => {
const users = [user]
console.table(users)
const usersHtml = users.map((user) => {
return `<li class="user">${user.id} - ${user.email}</li>`
}).join('\n')
document.querySelector('#users').innerHTML = usersHtml
})
.catch((e) => {
console.error('problem fetching users', e)
document.querySelector('#users').innerText = `Problem fetching users ${e.message}`
})
}
}
function postUser () {
const user = {
id: 101,
name: 'Joe Smith',
}
return fetch('https://jsonplaceholder.cypress.io/users', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(user),
})
}
function putUser () {
const user = {
id: 101,
name: 'Joe Smith',
}
return fetch('https://jsonplaceholder.cypress.io/users/1', {
method: 'PUT',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(user),
})
}
if (document.getElementById('load-users')) {
document.getElementById('load-users').addEventListener('click', loadUsers(3))
}
if (document.getElementById('load-five-users')) {
document.getElementById('load-five-users').addEventListener('click', loadUsers(5))
}
if (document.getElementById('load-second-user')) {
document.getElementById('load-second-user').addEventListener('click', loadUser(2))
}
const postUserButton = document.getElementById('post-user')
if (postUserButton) {
postUserButton.addEventListener('click', postUser)
}
const updateUserButton = document.getElementById('put-user')
if (updateUserButton) {
updateUserButton.addEventListener('click', putUser)
}
const updateNetworkStatus = () => {
const el = document.getElementById('network-status')
if (!el) {
return
}
const text = window.navigator.onLine ? '🟢 online' : '🟥 offline'
el.innerText = text
}
updateNetworkStatus()
window.addEventListener('offline', updateNetworkStatus)
window.addEventListener('online', updateNetworkStatus)
if (document.getElementById('get-headers')) {
document.getElementById('get-headers').addEventListener('click', () => {
fetch('/req-headers')
.then((r) => r.json())
.then((headers) => {
const output = document.getElementById('output')
output.innerText = JSON.stringify(headers, null, 2)
})
})
}