test(nuxt): test remote islands for `NuxtIsland` (#23801)

This commit is contained in:
Julien Huang 2023-10-20 10:38:51 +02:00 committed by GitHub
parent 8c9333a41c
commit b06a559e72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 0 deletions

20
test/fixtures/remote-provider/index.ts vendored Normal file
View File

@ -0,0 +1,20 @@
import http from 'node:http'
export function createSimpleRemoteIslandProvider (port = 3001) {
const server = http.createServer((req, res) => {
const response = {
html: '<div>hello world from another server</div>',
state: {},
head: {
link: [],
style: []
}
}
res.statusCode = 200
res.setHeader('Content-Type', 'application/json')
res.end(JSON.stringify(response))
})
server.listen(port)
return server
}

View File

@ -1,6 +1,17 @@
import { describe, expect, it, vi } from 'vitest'
import { h } from 'vue'
import { mountSuspended } from 'nuxt-vitest/utils'
import { createServerComponent } from '../../packages/nuxt/src/components/runtime/server-component'
import { createSimpleRemoteIslandProvider } from '../fixtures/remote-provider'
import NuxtIsland from '../../packages/nuxt/src/app/components/nuxt-island'
vi.mock('#build/nuxt.config.mjs', async (original) => {
return {
// @ts-expect-error virtual file
...(await original()),
remoteComponentIslands: true
}
})
vi.mock('vue', async (original) => {
const vue = await original<typeof import('vue')>()
@ -37,5 +48,21 @@ describe('runtime server component', () => {
"test": 1,
}
`)
vi.mocked(h).mockRestore()
})
it('expect remote island to be rendered', async () => {
const server = createSimpleRemoteIslandProvider()
const wrapper = await mountSuspended(NuxtIsland, {
props: {
name: 'Test',
source: 'http://localhost:3001'
}
})
expect(wrapper.html()).toMatchInlineSnapshot('"<div>hello world from another server</div>"')
await server.close()
})
})