From 03ba07caf67844e00aaec4f51d86cc4b5b743161 Mon Sep 17 00:00:00 2001 From: Yasser Lahbibi Date: Wed, 27 Sep 2023 22:35:56 +0200 Subject: [PATCH] feat(utils): add method option to `registerEndpoint` (#346) --- src/vitest-environment-nuxt/runtime/mock.ts | 35 +++++++++++++++---- .../nuxt-vitest/tests/nuxt/index.spec.ts | 15 ++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/vitest-environment-nuxt/runtime/mock.ts b/src/vitest-environment-nuxt/runtime/mock.ts index e699c850a..521c369f3 100644 --- a/src/vitest-environment-nuxt/runtime/mock.ts +++ b/src/vitest-environment-nuxt/runtime/mock.ts @@ -1,5 +1,5 @@ import { defineEventHandler } from 'h3' -import type { EventHandler } from 'h3' +import type { EventHandler, App, HTTPMethod } from 'h3' import type { ComponentInjectOptions, ComponentOptionsMixin, @@ -21,7 +21,7 @@ export type OptionalFunction = T | (() => Awaitable) * `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' @@ -32,11 +32,34 @@ export type OptionalFunction = T | (() => Awaitable) * ``` * @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) } diff --git a/test/fixtures/nuxt-vitest/tests/nuxt/index.spec.ts b/test/fixtures/nuxt-vitest/tests/nuxt/index.spec.ts index 73c79d2fe..eddca2617 100644 --- a/test/fixtures/nuxt-vitest/tests/nuxt/index.spec.ts +++ b/test/fixtures/nuxt-vitest/tests/nuxt/index.spec.ts @@ -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('/method', { method: 'POST' })).toMatchObject({ + method: 'POST', + }) + expect(await $fetch('/method')).toMatchObject({ method: 'GET' }) + }) + // TODO: reenable when merging Nuxt 3.7 it.skip('handles nuxt routing', async () => { const component = await mountSuspended(App, { route: '/test' })