forked from cypress-io/cypress-example-recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local-api.html
42 lines (38 loc) · 1.32 KB
/
local-api.html
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
<body>
<h2>Local API</h2>
<p>This page makes REST calls to the non-existent local API, thus
all network calls should be stubbed
</p>
<ul id="users"></ul>
<button id="load-users">Load users</button>
<button id="load-users-headers">Load users (using headers)</button>
<script>
const loadUsers = (headers) => () => {
document.querySelector('#users').innerText = ''
// there is no localhost API running
// thus the test will have to stub these endpoints
fetch(`http://localhost:3010/users`, {
method: 'get',
headers
})
.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}`
})
}
// no headers for this request
document.getElementById('load-users').addEventListener('click', loadUsers())
// add json header
document.getElementById('load-users-headers').addEventListener('click', loadUsers({
'Content-Type': 'application/json'
}))
</script>
</body>