import { describe, expect, it, vi } from 'vitest' import type { Plugin } from 'vite' import type { Component } from '@nuxt/schema' import type { UnpluginOptions } from 'unplugin' import { islandsTransform } from '../src/components/islandsTransform' import { normalizeLineEndings } from './utils' const getComponents = () => [{ filePath: '/root/hello.server.vue', mode: 'server', pascalName: 'HelloWorld', island: true, kebabName: 'hello-world', chunkName: 'components/hello-world', export: 'default', shortPath: '', prefetch: false, preload: false, }] as Component[] const pluginWebpack = islandsTransform.raw({ getComponents, selectiveClient: true, }, { framework: 'webpack', webpack: { compiler: {} as any } }) const viteTransform = async (source: string, id: string, selectiveClient = false) => { const vitePlugin = islandsTransform.raw({ getComponents, selectiveClient, }, { framework: 'vite' }) as Plugin const result = await (vitePlugin.transform! as Function)(source, id) return typeof result === 'string' ? result : result?.code } const webpackTransform = async (source: string, id: string) => { const result = await ((pluginWebpack as UnpluginOptions).transform! as Function)(source, id) return typeof result === 'string' ? result : result?.code } describe('islandTransform - server and island components', () => { describe('slots', () => { it('expect slot transform to match inline snapshot', async () => { const result = await viteTransform(` ` , 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('generates bindings when props are needed to be merged', async () => { const result = await viteTransform(` `, 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('expect slot fallback transform to match inline snapshot', async () => { const result = await viteTransform(` ` , 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('expect slot transform with fallback and no name to match inline snapshot #23209', async () => { const result = await viteTransform(` ` , 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('expect v-if/v-else/v-else-if to be set in teleport component wrapper', async () => { const result = await viteTransform(` `, 'WithVif.vue', true) expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) }) describe('nuxt-client', () => { describe('vite', () => { it('test transform with vite', async () => { const result = await viteTransform(` `, 'hello.server.vue', true) expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('test dynamic nuxt-client', async () => { const result = await viteTransform(` `, 'hello.server.vue', true) expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('should not transform if disabled', async () => { const result = await viteTransform(` `, 'hello.server.vue', false) expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) }) it('should add import if there is no scripts in the SFC', async () => { const result = await viteTransform(` `, 'hello.server.vue', true) expect(result).toMatchInlineSnapshot(` " " `) expect(result).toContain('import NuxtTeleportIslandComponent from \'#app/components/nuxt-teleport-island-component\'') }) it('should move v-if to the wrapper component', async () => { const result = await viteTransform(` `, 'hello.server.vue', true) expect(result).toMatchInlineSnapshot(` " " `) }) }) describe('webpack', () => { it('test transform with webpack', async () => { const spyOnWarn = vi.spyOn(console, 'warn') const result = await webpackTransform(` `, 'hello.server.vue') expect(normalizeLineEndings(result)).toMatchInlineSnapshot(` " " `) expect(spyOnWarn).toHaveBeenCalledWith('nuxt-client attribute and client components within islands is only supported with Vite. file: hello.server.vue') }) }) }) })