Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@types/sanitize-html": "^2.16.0",
"@vitest/coverage-v8": "4.0.18",
"diffable-html": "^6.0.1",
"happy-dom": "^20.7.0",
"oxfmt": "^0.35.0",
"oxlint": "^1.42.0",
"playwright": "^1.58.2",
Expand Down
62 changes: 57 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

141 changes: 141 additions & 0 deletions src/client/custom-element.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// @vitest-environment happy-dom
import { describe, expect, test, vi, beforeEach } from 'vitest'
import { createSSRApp } from 'vue'

import { VueIsland } from './custom-element.ts'

vi.mock('vue', () => ({
createSSRApp: vi.fn(() => ({
mount: vi.fn(),
unmount: vi.fn(),
})),
}))

vi.mock('./load-module.ts', () => ({
loadModule: vi.fn(() => Promise.resolve({ default: { name: 'TestComponent' } })),
}))

function createIsland(attrs?: Record<string, string>): VueIsland {
const el = new VueIsland()
if (attrs) {
for (const [key, value] of Object.entries(attrs)) {
el.setAttribute(key, value)
}
}
return el
}

function flushMicrotasks(): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, 0))
}

describe('VueIsland custom element', () => {
beforeEach(() => {
vi.mocked(createSSRApp).mockClear()
document.body.innerHTML = ''
})

describe('constructor', () => {
test('creates shadow root with style and slot', () => {
const el = createIsland()
expect(el.shadowRoot).not.toBeNull()
expect(el.shadowRoot!.querySelector('style')?.textContent).toBe(':host{display:contents}')
expect(el.shadowRoot!.querySelector('slot')).not.toBeNull()
})
})

describe('connectedCallback', () => {
test('does not mount when entry attribute is missing', async () => {
const el = createIsland()
document.body.appendChild(el)
await flushMicrotasks()
expect(createSSRApp).not.toHaveBeenCalled()
})

test('imports entry and mounts app', async () => {
const el = createIsland({ entry: './test-entry' })
document.body.appendChild(el)
await flushMicrotasks()

expect(createSSRApp).toHaveBeenCalledWith({ name: 'TestComponent' }, {})
const app = vi.mocked(createSSRApp).mock.results[0]!.value
expect(app.mount).toHaveBeenCalledWith(el)
})

test('passes parsed props from serialized-props attribute', async () => {
const el = createIsland({
entry: './test-entry',
'serialized-props': '{"msg":"hello","count":42}',
})
document.body.appendChild(el)
await flushMicrotasks()

expect(createSSRApp).toHaveBeenCalledWith(
{ name: 'TestComponent' },
{ msg: 'hello', count: 42 },
)
})

test('falls back to empty object on invalid JSON in serialized-props', async () => {
const el = createIsland({ entry: './test-entry', 'serialized-props': '{invalid' })
document.body.appendChild(el)
await flushMicrotasks()

expect(createSSRApp).toHaveBeenCalledWith({ name: 'TestComponent' }, {})
})

test('falls back to empty object when serialized-props is a non-object JSON value', async () => {
const el = createIsland({ entry: './test-entry', 'serialized-props': '"string"' })
document.body.appendChild(el)
await flushMicrotasks()

expect(createSSRApp).toHaveBeenCalledWith({ name: 'TestComponent' }, {})
})

test('does not mount if disconnected during import', async () => {
const el = createIsland({ entry: './test-entry' })
document.body.appendChild(el)
document.body.removeChild(el)
await flushMicrotasks()

expect(createSSRApp).not.toHaveBeenCalled()
})

test('unmounts previous app when connectedCallback is re-invoked', async () => {
const el = createIsland({ entry: './test-entry' })
document.body.appendChild(el)
await flushMicrotasks()

const firstApp = vi.mocked(createSSRApp).mock.results[0]!.value
vi.mocked(createSSRApp).mockClear()

// Manually re-invoke connectedCallback (simulates re-adoption without prior disconnect)
;(el as any).connectedCallback()
await flushMicrotasks()

expect(firstApp.unmount).toHaveBeenCalled()
expect(createSSRApp).toHaveBeenCalledTimes(1)
})
})

describe('disconnectedCallback', () => {
test('unmounts the app on disconnect', async () => {
const el = createIsland({ entry: './test-entry' })
document.body.appendChild(el)
await flushMicrotasks()

const app = vi.mocked(createSSRApp).mock.results[0]!.value
document.body.removeChild(el)

expect(app.unmount).toHaveBeenCalled()
})

test('does not throw when no app exists', async () => {
const el = createIsland()
document.body.appendChild(el)
await flushMicrotasks()

expect(() => document.body.removeChild(el)).not.toThrow()
})
})
})
Loading