Skip to content

Commit 52dc373

Browse files
feat: added tests
1 parent cdb5619 commit 52dc373

File tree

5 files changed

+222
-60
lines changed

5 files changed

+222
-60
lines changed

test/html.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ function request(path: string) {
77
}
88

99
function handler() {
10-
return (
11-
`
10+
return `
1211
<!DOCTYPE html>
1312
<html lang="en">
1413
<head>
@@ -19,7 +18,6 @@ function handler() {
1918
</body>
2019
</html>
2120
`
22-
)
2321
}
2422

2523
describe('Jsx html', () => {

test/index.test.ts

Lines changed: 95 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,99 @@ const page = `<!DOCTYPE HTML>
1616
</html>`
1717

1818
describe('HTML', () => {
19-
it('auto return html', async () => {
20-
const app = new Elysia().use(html()).get('/', () => page)
21-
22-
const res = await app.handle(req('/'))
23-
expect(await res.text()).toBe(page)
24-
expect(res.headers.get('Content-Type')).toContain('text/html')
25-
})
26-
27-
it('manual return html', async () => {
28-
const app = new Elysia().use(html()).get('/', ({ html }) => html(page))
29-
30-
const res = await app.handle(req('/'))
31-
expect(await res.text()).toBe(page)
32-
expect(res.headers.get('Content-Type')).toContain('text/html')
33-
})
34-
35-
it('inherits header', async () => {
36-
const app = new Elysia().use(html()).get('/', ({ html, set }) => {
37-
set.headers.Server = 'Elysia'
38-
39-
return html("<h1>Hi</h1>")
40-
})
41-
42-
const res = await app.handle(req('/'))
43-
expect(res.headers.get('Server')).toBe('Elysia')
44-
})
45-
46-
it('return any html tag', async () => {
47-
const app = new Elysia().use(html()).get('/', () => `<html>Hello World</html>`)
48-
49-
const res = await app.handle(req('/'))
50-
expect(res.headers.get('Content-type')).toContain('text/html; charset=utf8')
51-
})
52-
53-
it('consistently identifies html content', async () => {
54-
const app = new Elysia().use(html()).get('/', () => `<h1>Hi</h1>`)
55-
56-
let res = await app.handle(req('/'))
57-
expect(res.headers.get('Content-type')).toContain('text/html; charset=utf8')
58-
res = await app.handle(req('/'))
59-
expect(res.headers.get('Content-type')).toContain('text/html; charset=utf8')
60-
res = await app.handle(req('/'))
61-
expect(res.headers.get('Content-type')).toContain('text/html; charset=utf8')
62-
})
19+
it('auto return html', async () => {
20+
const app = new Elysia().use(html()).get('/', () => page)
21+
22+
const res = await app.handle(req('/'))
23+
expect(await res.text()).toBe(page)
24+
expect(res.headers.get('Content-Type')).toContain('text/html')
25+
})
26+
27+
it('manual return html', async () => {
28+
const app = new Elysia().use(html()).get('/', ({ html }) => html(page))
29+
30+
const res = await app.handle(req('/'))
31+
expect(await res.text()).toBe(page)
32+
expect(res.headers.get('Content-Type')).toContain('text/html')
33+
})
34+
35+
it('inherits header', async () => {
36+
const app = new Elysia().use(html()).get('/', ({ html, set }) => {
37+
set.headers.Server = 'Elysia'
38+
return html('<h1>Hi</h1>')
39+
})
40+
41+
const res = await app.handle(req('/'))
42+
expect(res.headers.get('Server')).toBe('Elysia')
43+
})
44+
45+
it('return any html tag', async () => {
46+
const app = new Elysia()
47+
.use(html())
48+
.get('/', () => `<html>Hello World</html>`)
49+
50+
const res = await app.handle(req('/'))
51+
expect(res.headers.get('Content-type')).toContain(
52+
'text/html; charset=utf8'
53+
)
54+
})
55+
56+
it('consistently identifies html content', async () => {
57+
const app = new Elysia().use(html()).get('/', () => `<h1>Hi</h1>`)
58+
59+
let res = await app.handle(req('/'))
60+
expect(res.headers.get('Content-type')).toContain(
61+
'text/html; charset=utf8'
62+
)
63+
res = await app.handle(req('/'))
64+
expect(res.headers.get('Content-type')).toContain(
65+
'text/html; charset=utf8'
66+
)
67+
res = await app.handle(req('/'))
68+
expect(res.headers.get('Content-type')).toContain(
69+
'text/html; charset=utf8'
70+
)
71+
})
72+
})
73+
74+
describe('HTML vs No html - header', () => {
75+
it('inherits header plain response when using the html plugin', async () => {
76+
const app = new Elysia().use(html()).get('/', ({ set }) => {
77+
set.headers.Server = 'Elysia'
78+
return 'Hello'
79+
})
80+
const res = await app.handle(req('/'))
81+
expect(res.headers.get('Server')).toBe('Elysia')
82+
})
83+
84+
it('inherits header plain response not using the html plugin', async () => {
85+
const app = new Elysia().get('/', ({ set }) => {
86+
set.headers.Server = 'Elysia'
87+
return 'Hello'
88+
})
89+
const res = await app.handle(req('/'))
90+
expect(res.headers.get('Server')).toBe('Elysia')
91+
expect(res.headers.get('Content-Type')).toBe(null)
92+
})
93+
94+
it('inherits header json response when using the html plugin', async () => {
95+
const app = new Elysia().use(html()).get('/', ({ set }) => {
96+
set.headers.Server = 'Elysia'
97+
return { Hello: 1 }
98+
})
99+
const res = await app.handle(req('/'))
100+
expect(res.headers.get('Server')).toBe('Elysia')
101+
})
102+
103+
it('inherits header json response not using the html plugin', async () => {
104+
const app = new Elysia().get('/', ({ set }) => {
105+
set.headers.Server = 'Elysia'
106+
return { Hello: 1 }
107+
})
108+
const res = await app.handle(req('/'))
109+
expect(res.headers.get('Server')).toBe('Elysia')
110+
expect(res.headers.get('Content-Type')).toBe(
111+
'application/json;charset=utf-8'
112+
)
113+
})
63114
})

test/jsx-stream.test.tsx

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { describe, expect, it } from 'bun:test'
2+
import { Elysia } from 'elysia'
3+
import { html } from '../src'
4+
import { Suspense, renderToStream, SuspenseScript } from '@kitajs/html/suspense'
5+
6+
function request(path: string) {
7+
return new Request(`http://localhost${path}`)
8+
}
9+
10+
function handler() {
11+
return renderToStream(() => htmlContent)
12+
}
13+
14+
const htmlContent = (
15+
<>
16+
{'<!DOCTYPE HTML>'}
17+
<html lang="en">
18+
<head>
19+
<title>Hello World</title>
20+
</head>
21+
<body>
22+
<h1>Hello World</h1>
23+
</body>
24+
</html>
25+
</>
26+
)
27+
28+
describe('Jsx html', () => {
29+
it('auto return html', async () => {
30+
const app = new Elysia().use(html()).get('/', handler)
31+
const res = await app.handle(request('/'))
32+
33+
expect(await res.text()).toBe(htmlContent)
34+
expect(res.headers.get('Content-Type')).toContain('text/html')
35+
})
36+
37+
it('auto return html with built in handler', async () => {
38+
const app = new Elysia()
39+
.use(html())
40+
.get('/', ({ html }) => html(() => htmlContent))
41+
42+
const res = await app.handle(request('/'))
43+
44+
expect(await res.text()).toBe(htmlContent)
45+
expect(res.headers.get('Content-Type')).toContain('text/html')
46+
})
47+
48+
it('works with async suspense', async () => {
49+
const app = new Elysia().use(html()).get('/', ({ html }) =>
50+
html((rid) => (
51+
<div>
52+
<Suspense rid={rid} fallback={<div>1</div>}>
53+
{Promise.resolve(<div>2</div>)}
54+
</Suspense>
55+
</div>
56+
))
57+
)
58+
59+
const res = await app.handle(request('/'))
60+
61+
expect(res.headers.get('Content-Type')).toContain('text/html')
62+
expect(await res.text()).toBe(
63+
<>
64+
<div>
65+
<div id="B:1" data-sf>
66+
<div>1</div>
67+
</div>
68+
</div>
69+
{SuspenseScript}
70+
<template id="N:1" data-sr>
71+
<div>2</div>
72+
</template>
73+
<script id="S:1" data-ss>
74+
$RC(1)
75+
</script>
76+
</>
77+
)
78+
})
79+
})
80+
81+
describe('HTML', () => {
82+
it('manual return html', async () => {
83+
const app = new Elysia()
84+
.use(html())
85+
.get('/', ({ html }) => html(handler()))
86+
87+
const res = await app.handle(request('/'))
88+
expect(await res.text()).toBe(htmlContent)
89+
expect(res.headers.get('Content-Type')).toContain('text/html')
90+
})
91+
92+
it('inherits header', async () => {
93+
const app = new Elysia().use(html()).get('/', ({ html, set }) => {
94+
set.headers.Server = 'Elysia'
95+
return html(<h1>Hi</h1>)
96+
})
97+
98+
const res = await app.handle(request('/'))
99+
expect(res.headers.get('Server')).toBe('Elysia')
100+
})
101+
102+
it('return any html tag', async () => {
103+
const app = new Elysia()
104+
.use(html())
105+
.get('/', () => <html>Hello World</html>)
106+
107+
const res = await app.handle(request('/'))
108+
expect(res.headers.get('Content-type')).toBe('text/html; charset=utf8')
109+
})
110+
111+
it('consistently identifies html content', async () => {
112+
const app = new Elysia().use(html()).get('/', () => <h1></h1>)
113+
114+
let res = await app.handle(request('/'))
115+
expect(res.headers.get('Content-type')).toBe('text/html; charset=utf8')
116+
res = await app.handle(request('/'))
117+
expect(res.headers.get('Content-type')).toBe('text/html; charset=utf8')
118+
res = await app.handle(request('/'))
119+
expect(res.headers.get('Content-type')).toBe('text/html; charset=utf8')
120+
})
121+
})

test/jsx.test.tsx

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,17 @@ describe('HTML', () => {
5858
.get('/', () => <html>Hello World</html>)
5959

6060
const res = await app.handle(request('/'))
61-
expect(res.headers.get('Content-type')).toContain(
62-
'text/html; charset=utf8'
63-
)
61+
expect(res.headers.get('Content-type')).toBe('text/html; charset=utf8')
6462
})
6563

6664
it('consistently identifies html content', async () => {
6765
const app = new Elysia().use(html()).get('/', () => <h1></h1>)
6866

6967
let res = await app.handle(request('/'))
70-
expect(res.headers.get('Content-type')).toContain(
71-
'text/html; charset=utf8'
72-
)
68+
expect(res.headers.get('Content-type')).toBe('text/html; charset=utf8')
7369
res = await app.handle(request('/'))
74-
expect(res.headers.get('Content-type')).toContain(
75-
'text/html; charset=utf8'
76-
)
70+
expect(res.headers.get('Content-type')).toBe('text/html; charset=utf8')
7771
res = await app.handle(request('/'))
78-
expect(res.headers.get('Content-type')).toContain(
79-
'text/html; charset=utf8'
80-
)
72+
expect(res.headers.get('Content-type')).toBe('text/html; charset=utf8')
8173
})
8274
})

test/options.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'bun:test'
22
import { Elysia } from 'elysia'
3-
import { html, isHtml, isTagHtml } from '../src'
3+
import { html } from '../src'
44

55
function request(path: string) {
66
return new Request(`http://localhost${path}`)

0 commit comments

Comments
 (0)