Skip to content

Commit

Permalink
feat(utils): add method option to registerEndpoint (#346)
Browse files Browse the repository at this point in the history
  • Loading branch information
yassilah authored Sep 27, 2023
1 parent 91723ac commit 03ba07c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
35 changes: 29 additions & 6 deletions src/vitest-environment-nuxt/runtime/mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineEventHandler } from 'h3'
import type { EventHandler } from 'h3'
import type { EventHandler, App, HTTPMethod } from 'h3'
import type {
ComponentInjectOptions,
ComponentOptionsMixin,
Expand All @@ -21,7 +21,7 @@ export type OptionalFunction<T> = T | (() => Awaitable<T>)
* `registerEndpoint` allows you create Nitro endpoint that returns mocked data. It can come in handy if you want to test a component that makes requests to API to display some data.
*
* @param url - endpoint name (e.g. `/test/`).
* @param handler - factory function that returns the mocked data.
* @param options - factory function that returns the mocked data or an object containing both the `handler` and the `method` properties.
* @example
* ```ts
* import { registerEndpoint } from 'nuxt-vitest/utils'
Expand All @@ -32,11 +32,34 @@ export type OptionalFunction<T> = T | (() => Awaitable<T>)
* ```
* @see https://github.com/danielroe/nuxt-vitest#registerendpoint
*/
export function registerEndpoint(url: string, handler: EventHandler) {
export function registerEndpoint(
url: string,
options:
| EventHandler
| {
handler: EventHandler
method: HTTPMethod
}
) {
// @ts-expect-error private property
if (!window.__app) return
// @ts-expect-error private property
window.__app.use('/_' + url, defineEventHandler(handler))
const app: App = window.__app

if (!app) return

const config =
typeof options === 'function'
? {
handler: options,
method: undefined,
}
: options

app.use('/_' + url, defineEventHandler(config.handler), {
match(_, event) {
return config.method ? event?.method === config.method : true
},
})

// @ts-expect-error private property
window.__registry.add(url)
}
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/nuxt-vitest/tests/nuxt/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,21 @@ describe('test utils', () => {
)
})

it('can mock fetch requests with explicit methods', async () => {
registerEndpoint('/method', {
method: 'POST',
handler: () => ({ method: 'POST' }),
})
registerEndpoint('/method', {
method: 'GET',
handler: () => ({ method: 'GET' }),
})
expect(await $fetch<unknown>('/method', { method: 'POST' })).toMatchObject({
method: 'POST',
})
expect(await $fetch<unknown>('/method')).toMatchObject({ method: 'GET' })
})

// TODO: reenable when merging Nuxt 3.7
it.skip('handles nuxt routing', async () => {
const component = await mountSuspended(App, { route: '/test' })
Expand Down

0 comments on commit 03ba07c

Please sign in to comment.