Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yusukebe committed Feb 4, 2024
1 parent ad3d2cd commit 9a4b690
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 33 deletions.
31 changes: 21 additions & 10 deletions src/server/components/script.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,28 @@
import type { FC } from 'hono/jsx'
import type { Manifest } from 'vite'

export const Script: FC<{ src: string }> = async ({ src }) => {
if (import.meta.env.PROD) {
// @ts-expect-error not typed
const manifest = await import('/dist/.vite/manifest.json')
const manifestDefault: Manifest = manifest['default']
const scriptInManifest = manifestDefault[src.replace(/^\//, '')]
if (scriptInManifest) {
return <script type='module' src={`/${scriptInManifest.file}`}></script>
} else {
return <></>
type Options = {
src: string
prod?: boolean
manifest?: Manifest
}

export const Script: FC<Options> = async (options) => {
const src = options.src
if (options.prod ?? import.meta.env.PROD) {
let manifest: Manifest | undefined = options.manifest
if (!manifest) {
// @ts-expect-error not typed
const manifestFile = await import('/dist/.vite/manifest.json')
manifest = manifestFile['default']
}
if (manifest) {
const scriptInManifest = manifest[src.replace(/^\//, '')]
if (scriptInManifest) {
return <script type='module' src={`/${scriptInManifest.file}`}></script>
}
}
return <></>
} else {
return <script type='module' src={src}></script>
}
Expand Down
19 changes: 19 additions & 0 deletions test/hono-jsx/app-script/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Script } from '../../../../src/server'

export default function Hello() {
return (
<html>
<head>
<Script
src='/app/client.ts'
prod={true}
manifest={{
'app/client.ts': {
file: 'static/client-abc.js',
},
}}
/>
</head>
</html>
)
}
4 changes: 0 additions & 4 deletions test/hono-jsx/app/factory.ts

This file was deleted.

6 changes: 3 additions & 3 deletions test/hono-jsx/app/routes/about/[name].tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { createRoute } from '../../../../../src/factory'
import Badge from '../../components/Badge'
import { createHandlers } from '../../factory'

export const POST = createHandlers((c) => {
export const POST = createRoute((c) => {
return c.text('Created!', 201)
})

export default createHandlers((c) => {
export default createRoute((c) => {
const { name } = c.req.param()
return c.render(
<>
Expand Down
4 changes: 2 additions & 2 deletions test/hono-jsx/app/routes/about/[name]/address.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHandlers } from '../../../factory'
import { createRoute } from '../../../../../../src/factory'

export default createHandlers((c) => {
export default createRoute((c) => {
const { name } = c.req.param()
return c.render(<b>{name}'s address</b>, {
title: `${name}'s address`,
Expand Down
6 changes: 3 additions & 3 deletions test/hono-jsx/app/routes/api.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHandlers } from '../factory'
import { createRoute } from '../../../../src/factory'

export const POST = createHandlers((c) => {
export const POST = createRoute((c) => {
return c.json(
{
message: 'created',
Expand All @@ -10,7 +10,7 @@ export const POST = createHandlers((c) => {
)
})

export default createHandlers((c) => {
export default createRoute((c) => {
c.header('X-Custom', 'Hello')
return c.json({
foo: 'bar',
Expand Down
4 changes: 2 additions & 2 deletions test/hono-jsx/app/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createHandlers } from '../factory'
import { createRoute } from '../../../../src/factory'

export default createHandlers((c) => {
export default createRoute((c) => {
return c.render(<h1>Hello</h1>, {
title: 'This is a title',
})
Expand Down
4 changes: 2 additions & 2 deletions test/hono-jsx/app/routes/throw_error.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createHandlers } from '../factory'
import { createRoute } from '../../../../src/factory'

export default createHandlers(() => {
export default createRoute(() => {
throw new Error('Foo')
})
32 changes: 25 additions & 7 deletions test/hono-jsx/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ describe('Basic', () => {
expect(res.status).toBe(404)
})

it('Should return 200 response /about/me', async () => {
it('Should return 200 response - /about/me', async () => {
const res = await app.request('/about/me')
expect(res.status).toBe(200)
// hono/jsx escape a single quote to &#39;
expect(await res.text()).toBe('<p>It&#39;s me</p><b>My name is me</b>')
})

it('Should return 200 response POST /about/me', async () => {
it('Should return 200 response - POST /about/me', async () => {
const res = await app.request('/about/me', {
method: 'POST',
})
Expand All @@ -126,7 +126,7 @@ describe('Basic', () => {
expect(await res.text()).toBe('<h1>Hello MDX</h1>')
})

it('Should return 500 response /throw_error', async () => {
it('Should return 500 response - /throw_error', async () => {
global.console.trace = vi.fn()
const res = await app.request('/throw_error')
expect(res.status).toBe(500)
Expand Down Expand Up @@ -175,7 +175,7 @@ describe('With preserved', () => {
)
})

it('Should return 200 response /about/me', async () => {
it('Should return 200 response - /about/me', async () => {
const res = await app.request('/about/me')
expect(res.status).toBe(200)
// hono/jsx escape a single quote to &#39;
Expand All @@ -184,7 +184,7 @@ describe('With preserved', () => {
)
})

it('Should return 200 response /about/me/address', async () => {
it('Should return 200 response - /about/me/address', async () => {
const res = await app.request('/about/me/address')
expect(res.status).toBe(200)
// hono/jsx escape a single quote to &#39;
Expand All @@ -193,7 +193,7 @@ describe('With preserved', () => {
)
})

it('Should return 200 response /interaction', async () => {
it('Should return 200 response - /interaction', async () => {
const res = await app.request('/interaction')
expect(res.status).toBe(200)
// hono/jsx escape a single quote to &#39;
Expand All @@ -202,7 +202,7 @@ describe('With preserved', () => {
)
})

it('Should return 500 response /throw_error', async () => {
it('Should return 500 response - /throw_error', async () => {
const res = await app.request('/throw_error')
expect(res.status).toBe(500)
expect(await res.text()).toBe(
Expand Down Expand Up @@ -279,3 +279,21 @@ describe('Nested Layouts', () => {
expect(await res.text()).toBe('<main><nav>foo menu</nav><nav>bar menu</nav><h1>Baz</h1></main>')
})
})

describe('<Script /> component', () => {
const ROUTES = import.meta.glob('./app-script/routes/index.tsx', {
eager: true,
})
const app = createApp({
root: './app-script/routes',
ROUTES: ROUTES as any,
})

it('Should convert the script path correctly', async () => {
const res = await app.request('/')
expect(res.status).toBe(200)
expect(await res.text()).toBe(
'<html><head><script type="module" src="/static/client-abc.js"></script></head></html>'
)
})
})

0 comments on commit 9a4b690

Please sign in to comment.