-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test.js
183 lines (167 loc) · 4.11 KB
/
index.test.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
180
181
182
183
import process from 'node:process'
import test from 'ava'
import {useTemporaryDirectory} from 'ava-patterns'
import install from 'quick-install'
import {closeBrowser, openTab, findElement, closeTab} from 'puppet-strings'
import {openChrome} from 'puppet-strings-chrome'
import serveUi, {teardown} from 'passing-notes-ui'
import {Logger, startServer, stopServer, compose} from './index.js'
test.before(async () => {
await install(process.cwd(), process.cwd())
})
test.after(async () => {
await teardown()
})
test('running in the browser', async (t) => {
const directory = await useTemporaryDirectory(t)
await directory.writeFile(
'index.html',
`
<!doctype html>
<meta charset="utf-8">
<script type="module" src="/index.js"></script>
`,
)
await directory.writeFile(
'index.js',
`
import {sendRequest} from 'passing-notes'
async function run() {
document.body.textContent = 'Hello World!'
const response = await sendRequest({
method: 'GET',
url: '/message'
})
document.body.textContent = JSON.stringify(response, null, 2)
}
run()
`,
)
const logger = new Logger()
logger.on('log', (event, line) => t.log(line))
const server = await startServer(
{port: 10_020},
compose(
(next) => (request) => {
if (request.url !== '/message') {
return next(request)
}
return {
status: 200,
headers: {
'Content-Type': 'text/plain',
},
body: 'Hello World!',
}
},
serveUi({path: directory.path, logger}),
() => () => ({status: 404}),
),
)
t.teardown(async () => {
await stopServer(server)
})
const browser = await openChrome()
t.teardown(async () => {
await closeBrowser(browser)
})
const tab = await openTab(browser, 'http://localhost:10020')
t.teardown(async () => {
await closeTab(tab)
})
const body = await findElement(tab, 'body', 'Hello World!')
t.like(JSON.parse(body.textContent), {
status: 200,
headers: {
'content-type': 'text/plain',
},
body: 'Hello World!',
})
})
test('supporting streaming server-sent events in the browser', async (t) => {
const directory = await useTemporaryDirectory(t)
await directory.writeFile(
'index.html',
`
<!doctype html>
<meta charset="utf-8">
<script type="module" src="/index.js"></script>
`,
)
await directory.writeFile(
'index.js',
`
import {sendRequest} from 'passing-notes'
async function run() {
const response = await sendRequest({
method: 'GET',
url: '/events'
})
response.body
.pipeThrough(new TextDecoderStream())
.pipeTo(new WritableStream({
write(chunk) {
document.body.textContent += chunk
}
}))
}
run()
`,
)
const logger = new Logger()
logger.on('log', (event, line) => t.log(line))
const server = await startServer(
{port: 10_021},
compose(
(next) => (request) => {
if (request.url !== '/events') {
return next(request)
}
return {
status: 200,
headers: {
'Content-Type': 'text/event-stream',
},
body: [
': comment\n',
'\n',
'data: some text\n',
'\n',
'data: multiple\n',
'data: lines\n',
'\n',
'event: foo\n',
'data: bar\n',
'\n',
].join(''),
}
},
serveUi({path: directory.path, logger}),
() => () => ({status: 404}),
),
)
t.teardown(async () => {
await stopServer(server)
})
const browser = await openChrome()
t.teardown(async () => {
await closeBrowser(browser)
})
const tab = await openTab(browser, 'http://localhost:10021')
const body = await findElement(tab, 'body', 'bar')
t.is(
body.textContent,
[
': comment\n',
'\n',
'data: some text\n',
'\n',
'data: multiple\n',
'data: lines\n',
'\n',
'event: foo\n',
'data: bar\n',
'\n',
].join(''),
)
})