Skip to content
This repository has been archived by the owner on Jun 27, 2024. It is now read-only.

Commit

Permalink
test: add test suite for onPlugIn event handler (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
boywithkeyboard committed Aug 6, 2023
1 parent b0852c5 commit d711d3a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cheetah.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ export class cheetah extends base<cheetah>() {
let body: Response | void = undefined

for (const e of this.#extensions.values()) {
if (this.#onPlugIn === true && e[1].onPlugIn !== undefined) {
if (!this.#onPlugIn && e[1].onPlugIn !== undefined) {
await e[1].onPlugIn({
env: __app.env,
routes: this.#routes,
Expand Down Expand Up @@ -290,8 +290,8 @@ export class cheetah extends base<cheetah>() {
}
}

if (this.#onPlugIn) {
this.#onPlugIn = false
if (!this.#onPlugIn) {
this.#onPlugIn = true
}

if (body !== undefined) {
Expand Down
20 changes: 20 additions & 0 deletions test/extensions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import cheetah, { createExtension } from '../mod.ts'
import { assertEquals } from './deps.ts'

Deno.test('Extensions', async (t) => {
let count = 0

const a = createExtension({
onPlugIn({ setRoute }) {
setRoute('get', '/cookie', () => '🍪')

count++
},
onRequest({ req }) {
req.headers.set('custom', 'test')
},
Expand Down Expand Up @@ -39,6 +46,12 @@ Deno.test('Extensions', async (t) => {
return 'test'
})

await t.step('onPlugIn (part 1)', async () => {
const result = await app.fetch(new Request('http://localhost/cookie'))

assertEquals(await result.text(), '🍪')
})

await t.step('onRequest', async () => {
const result = await app.fetch(new Request('http://localhost/a'))

Expand All @@ -54,4 +67,11 @@ Deno.test('Extensions', async (t) => {

assertEquals(await result2.text(), 'hello')
})

await t.step('onPlugIn (part 2)', async () => {
const result = await app.fetch(new Request('http://localhost/cookie'))

assertEquals(await result.text(), '🍪')
assertEquals(count, 1)
})
})

0 comments on commit d711d3a

Please sign in to comment.