0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-24 19:26:56 +01:00
hono/runtime-tests/deno/ssg.test.tsx
Yusuke Wada dfbd717263
chore: rename runtime_tests to runtime-tests (#3419)
* chore: rename `runtime_tests` to `runtime-tests`

* fixed GitHub workflow
2024-09-17 14:51:43 +09:00

53 lines
1.4 KiB
TypeScript

import { assertEquals } from '@std/assert'
import { toSSG } from '../../src/adapter/deno/ssg.ts'
import { Hono } from '../../src/hono.ts'
Deno.test('toSSG function', async () => {
const app = new Hono()
app.get('/', (c) => c.text('Hello, World!'))
app.get('/about', (c) => c.text('About Page'))
app.get('/about/some', (c) => c.text('About Page 2tier'))
app.post('/about/some/thing', (c) => c.text('About Page 3tier'))
app.get('/bravo', (c) => c.html('Bravo Page'))
app.get('/Charlie', async (c, next) => {
c.setRenderer((content) => {
return c.html(
<html>
<body>
<p>{content}</p>
</body>
</html>
)
})
await next()
})
app.get('/Charlie', (c) => {
return c.render('Hello!')
})
const result = await toSSG(app, { dir: './ssg-static' })
assertEquals(result.success, true)
assertEquals(result.error, undefined)
assertEquals(result.files !== undefined, true)
await deleteDirectory('./ssg-static')
})
async function deleteDirectory(dirPath: string): Promise<void> {
try {
const stat = await Deno.stat(dirPath)
if (stat.isDirectory) {
for await (const dirEntry of Deno.readDir(dirPath)) {
const entryPath = `${dirPath}/${dirEntry.name}`
await deleteDirectory(entryPath)
}
await Deno.remove(dirPath)
} else {
await Deno.remove(dirPath)
}
} catch (error) {
console.error(`Error deleting directory: ${error}`)
}
}