test: add test suites for extends features (#3862)

This commit is contained in:
Kevin Marrec 2022-03-24 13:33:42 +01:00 committed by GitHub
parent f6b7c97e9a
commit cd8dbdc01c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 118 additions and 33 deletions

View File

@ -1,6 +1,6 @@
import { fileURLToPath } from 'url'
import { describe, expect, it } from 'vitest'
import { setup, $fetch, startServer } from '@nuxt/test-utils'
import { setup, fetch, $fetch, startServer } from '@nuxt/test-utils'
await setup({
rootDir: fileURLToPath(new URL('./fixtures/basic', import.meta.url)),
@ -157,27 +157,66 @@ describe('reactivity transform', () => {
})
describe('extends support', () => {
describe('pages', () => {
it('extends foo/pages/index.vue', async () => {
describe('layouts & pages', () => {
it('extends foo/layouts/default & foo/pages/index', async () => {
const html = await $fetch('/foo')
expect(html).toContain('Hello from extended page of foo!')
expect(html).toContain('Extended layout from foo')
expect(html).toContain('Extended page from foo')
})
it('extends bar/pages/override.vue over foo/pages/override.vue', async () => {
it('extends [bar/layouts/override & bar/pages/override] over [foo/layouts/override & foo/pages/override]', async () => {
const html = await $fetch('/override')
expect(html).toContain('Extended layout from bar')
expect(html).toContain('Extended page from bar')
})
})
describe('components', () => {
it('extends foo/components/ExtendsFoo', async () => {
const html = await $fetch('/foo')
expect(html).toContain('Extended component from foo')
})
it('extends bar/components/ExtendsOverride over foo/components/ExtendsOverride', async () => {
const html = await $fetch('/override')
expect(html).toContain('Extended component from bar')
})
})
describe('middlewares', () => {
it('extends foo/middleware/foo', async () => {
const html = await $fetch('/with-middleware')
expect(html).toContain('Injected by extended middleware')
const html = await $fetch('/foo')
expect(html).toContain('Middleware | foo: Injected by extended middleware from foo')
})
it('extends bar/middleware/override.vue over foo/middleware/override.vue', async () => {
const html = await $fetch('/with-middleware-override')
expect(html).toContain('Injected by extended middleware from bar')
it('extends bar/middleware/override over foo/middleware/override', async () => {
const html = await $fetch('/override')
expect(html).toContain('Middleware | override: Injected by extended middleware from bar')
})
})
describe('composables', () => {
it('extends foo/composables/foo', async () => {
const html = await $fetch('/foo')
expect(html).toContain('Composable | useExtendsFoo: foo')
})
})
describe('plugins', () => {
it('extends foo/plugins/foo', async () => {
const html = await $fetch('/foo')
expect(html).toContain('Plugin | foo: String generated from foo plugin!')
})
})
describe('server', () => {
it('extends foo/server/api/foo', async () => {
expect(await $fetch('/api/foo')).toBe('foo')
})
it('extends foo/server/middleware/foo', async () => {
const { headers } = await fetch('/')
expect(headers.get('injected-header')).toEqual('foo')
})
})
})

View File

@ -0,0 +1,3 @@
<template>
<div>Extended component from bar</div>
</template>

View File

@ -0,0 +1,6 @@
<template>
<div>
<div>Extended layout from bar</div>
<NuxtPage />
</div>
</template>

View File

@ -1,3 +1,14 @@
<script setup>
definePageMeta({
layout: 'override',
middleware: 'override'
})
</script>
<template>
<div>Extended page from bar</div>
<div>
<div>Extended page from bar</div>
<div>Middleware | override: {{ $route.meta.override }}</div>
<ExtendsOverride />
</div>
</template>

View File

@ -0,0 +1,3 @@
<template>
<div>Extended component from foo</div>
</template>

View File

@ -0,0 +1,3 @@
<template>
<div>This component should be overriden by bar</div>
</template>

View File

@ -0,0 +1 @@
export const useExtendsFoo = () => 'foo'

View File

@ -0,0 +1,6 @@
<template>
<div>
<div>Extended layout from foo</div>
<NuxtPage />
</div>
</template>

View File

@ -0,0 +1,3 @@
<template>
<div>This layout should be overriden by bar</div>
</template>

View File

@ -1,3 +1,3 @@
export default defineNuxtRouteMiddleware((to) => {
to.meta.foo = 'Injected by extended middleware'
to.meta.foo = 'Injected by extended middleware from foo'
})

View File

@ -1,3 +1,3 @@
export default defineNuxtRouteMiddleware((to) => {
to.meta.override = 'Injected by extended middleware from foo'
to.meta.override = 'This middleware should be overriden by bar'
})

View File

@ -1,3 +1,17 @@
<script setup>
definePageMeta({
middleware: 'foo'
})
const foo = useExtendsFoo()
</script>
<template>
<div>Hello from extended page of foo!</div>
<div>
<div>Extended page from foo</div>
<div>Middleware | foo: {{ $route.meta.foo }}</div>
<div>Composable | useExtendsFoo: {{ foo }}</div>
<div>Plugin | foo: {{ $foo() }}</div>
<ExtendsFoo />
</div>
</template>

View File

@ -1,3 +1,3 @@
<template>
<div>Extended page from foo</div>
<div>This page should be overriden by bar</div>
</template>

View File

@ -1,9 +0,0 @@
<script setup>
definePageMeta({
middleware: 'override'
})
</script>
<template>
<div>{{ $route.meta.override }}</div>
</template>

View File

@ -1,9 +0,0 @@
<script setup>
definePageMeta({
middleware: 'foo'
})
</script>
<template>
<div>{{ $route.meta.foo }}</div>
</template>

View File

@ -0,0 +1,7 @@
export default defineNuxtPlugin(() => {
return {
provide: {
foo: () => 'String generated from foo plugin!'
}
}
})

View File

@ -0,0 +1 @@
export default () => 'foo'

View File

@ -0,0 +1,6 @@
import type { ServerResponse } from 'http'
export default (_, res: ServerResponse, next) => {
res.setHeader('injected-header', 'foo')
next()
}