2022-02-15 09:50:11 +00:00
|
|
|
import { expectTypeOf } from 'expect-type'
|
|
|
|
import { describe, it } from 'vitest'
|
|
|
|
import type { Ref } from 'vue'
|
|
|
|
|
|
|
|
import { useRouter as vueUseRouter } from 'vue-router'
|
2022-02-18 18:14:57 +00:00
|
|
|
import { defineNuxtConfig } from '~~/../../packages/nuxt3/src'
|
2022-02-15 09:50:11 +00:00
|
|
|
import { useRouter } from '#imports'
|
|
|
|
import { isVue3 } from '#app'
|
|
|
|
|
2022-02-16 20:50:19 +00:00
|
|
|
interface TestResponse { message: string }
|
|
|
|
|
2022-02-15 09:50:11 +00:00
|
|
|
describe('API routes', () => {
|
|
|
|
it('generates types for routes', () => {
|
|
|
|
expectTypeOf($fetch('/api/hello')).toMatchTypeOf<Promise<string>>()
|
|
|
|
expectTypeOf($fetch('/api/hey')).toMatchTypeOf<Promise<{ foo:string, baz: string }>>()
|
|
|
|
expectTypeOf($fetch('/api/other')).toMatchTypeOf<Promise<unknown>>()
|
2022-02-16 20:50:19 +00:00
|
|
|
expectTypeOf($fetch<TestResponse>('/test')).toMatchTypeOf<Promise<TestResponse>>()
|
2022-02-15 09:50:11 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
it('works with useFetch', () => {
|
|
|
|
expectTypeOf(useFetch('/api/hello').data).toMatchTypeOf<Ref<string>>()
|
|
|
|
expectTypeOf(useFetch('/api/hey').data).toMatchTypeOf<Ref<{ foo:string, baz: string }>>()
|
|
|
|
expectTypeOf(useFetch('/api/hey', { pick: ['baz'] }).data).toMatchTypeOf<Ref<{ baz: string }>>()
|
|
|
|
expectTypeOf(useFetch('/api/other').data).toMatchTypeOf<Ref<unknown>>()
|
2022-02-16 20:50:19 +00:00
|
|
|
expectTypeOf(useFetch<TestResponse>('/test').data).toMatchTypeOf<Ref<TestResponse>>()
|
2022-02-15 09:50:11 +00:00
|
|
|
expectTypeOf(useLazyFetch('/api/hello').data).toMatchTypeOf<Ref<string>>()
|
|
|
|
expectTypeOf(useLazyFetch('/api/hey').data).toMatchTypeOf<Ref<{ foo:string, baz: string }>>()
|
|
|
|
expectTypeOf(useLazyFetch('/api/hey', { pick: ['baz'] }).data).toMatchTypeOf<Ref<{ baz: string }>>()
|
|
|
|
expectTypeOf(useLazyFetch('/api/other').data).toMatchTypeOf<Ref<unknown>>()
|
2022-02-16 20:50:19 +00:00
|
|
|
expectTypeOf(useLazyFetch('/api/other').data).toMatchTypeOf<Ref<unknown>>()
|
|
|
|
expectTypeOf(useLazyFetch<TestResponse>('/test').data).toMatchTypeOf<Ref<TestResponse>>()
|
2022-02-15 09:50:11 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('aliases', () => {
|
|
|
|
it('allows importing from path aliases', () => {
|
|
|
|
expectTypeOf(useRouter).toMatchTypeOf<typeof vueUseRouter>()
|
|
|
|
expectTypeOf(isVue3).toMatchTypeOf<boolean>()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('middleware', () => {
|
|
|
|
it('recognises named middleware', () => {
|
|
|
|
definePageMeta({ middleware: 'test-middleware' })
|
|
|
|
definePageMeta({ middleware: 'pascal-case' })
|
|
|
|
// @ts-expect-error Invalid middleware
|
|
|
|
definePageMeta({ middleware: 'invalid-middleware' })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('layouts', () => {
|
|
|
|
it('recognises named layouts', () => {
|
|
|
|
definePageMeta({ layout: 'test-layout' })
|
|
|
|
definePageMeta({ layout: 'pascal-case' })
|
|
|
|
// @ts-expect-error Invalid layout
|
|
|
|
definePageMeta({ layout: 'invalid-layout' })
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe('modules', () => {
|
|
|
|
it('augments schema automatically', () => {
|
|
|
|
defineNuxtConfig({ sampleModule: { enabled: false } })
|
|
|
|
// @ts-expect-error
|
|
|
|
defineNuxtConfig({ sampleModule: { other: false } })
|
|
|
|
// @ts-expect-error
|
|
|
|
defineNuxtConfig({ undeclaredKey: { other: false } })
|
|
|
|
})
|
|
|
|
})
|