|
| 1 | +import { fileURLToPath } from 'node:url' |
| 2 | +import { join } from 'node:path' |
| 3 | +import { describe, it, expect } from 'vitest' |
| 4 | +import { setup, createPage } from '@nuxt/test-utils/e2e' |
| 5 | + |
| 6 | +describe('preview DOM element slots', async () => { |
| 7 | + await setup({ |
| 8 | + rootDir: join(fileURLToPath(import.meta.url), '../../playground'), |
| 9 | + server: true, |
| 10 | + browser: true, |
| 11 | + dev: true, |
| 12 | + }) |
| 13 | + |
| 14 | + it('renders components with DOM element slots', async () => { |
| 15 | + const page = await createPage('/preview-test-dom-slots.html') |
| 16 | + |
| 17 | + // Wait for first test to render |
| 18 | + await page.waitForFunction(() => { |
| 19 | + const target = document.getElementById('test-dom-slot-simple') |
| 20 | + return target && target.innerHTML.includes('DOM Element Content') |
| 21 | + }, { timeout: 15000 }) |
| 22 | + |
| 23 | + const hasContent = await page.evaluate(() => { |
| 24 | + const target = document.getElementById('test-dom-slot-simple') |
| 25 | + return target?.innerHTML.includes('This content is a real DOM element') |
| 26 | + }) |
| 27 | + |
| 28 | + expect(hasContent).toBe(true) |
| 29 | + await page.close() |
| 30 | + }) |
| 31 | + |
| 32 | + it('preserves DOM element identity when moving to slots', async () => { |
| 33 | + const page = await createPage('/preview-test-dom-slots.html') |
| 34 | + |
| 35 | + // Wait for component to render |
| 36 | + await page.waitForFunction(() => { |
| 37 | + return document.getElementById('test-element') !== null |
| 38 | + }, { timeout: 15000 }) |
| 39 | + |
| 40 | + // Verify the element still has its ID (wasn't cloned) |
| 41 | + const elementStillExists = await page.evaluate(() => { |
| 42 | + const el = document.getElementById('test-element') |
| 43 | + return el !== null && el.textContent?.includes('DOM element') |
| 44 | + }) |
| 45 | + |
| 46 | + expect(elementStillExists).toBe(true) |
| 47 | + await page.close() |
| 48 | + }) |
| 49 | + |
| 50 | + it('preserves event listeners on moved DOM elements', async () => { |
| 51 | + const page = await createPage('/preview-test-dom-slots.html') |
| 52 | + |
| 53 | + // Wait for button to be rendered |
| 54 | + await page.waitForFunction(() => { |
| 55 | + return document.getElementById('test-button') !== null |
| 56 | + }, { timeout: 15000 }) |
| 57 | + |
| 58 | + // Set up dialog handler before clicking |
| 59 | + page.once('dialog', async (dialog) => { |
| 60 | + expect(dialog.message()).toContain('Button clicked 1 time(s)') |
| 61 | + await dialog.accept() |
| 62 | + }) |
| 63 | + |
| 64 | + // Click the button |
| 65 | + await page.click('#test-button') |
| 66 | + |
| 67 | + await page.close() |
| 68 | + }) |
| 69 | + |
| 70 | + it('handles nested components in DOM element slots', async () => { |
| 71 | + const page = await createPage('/preview-test-dom-slots.html') |
| 72 | + |
| 73 | + // Wait for nested components to render |
| 74 | + await page.waitForFunction(() => { |
| 75 | + const nested1 = document.getElementById('nested-button-dom') |
| 76 | + const nested2 = document.getElementById('nested-card-dom') |
| 77 | + return nested1 && nested1.children.length > 0 |
| 78 | + && nested2 && nested2.children.length > 0 |
| 79 | + }, { timeout: 20000 }) |
| 80 | + |
| 81 | + const nestedContent = await page.evaluate(() => { |
| 82 | + const button = document.getElementById('nested-button-dom') |
| 83 | + const card = document.getElementById('nested-card-dom') |
| 84 | + return { |
| 85 | + hasButton: button?.innerHTML.includes('Nested Button in DOM Slot') |
| 86 | + || button?.querySelector('button')?.textContent?.includes('Nested Button'), |
| 87 | + hasCard: card?.innerHTML.includes('Nested Card'), |
| 88 | + } |
| 89 | + }) |
| 90 | + |
| 91 | + expect(nestedContent.hasButton).toBe(true) |
| 92 | + expect(nestedContent.hasCard).toBe(true) |
| 93 | + await page.close() |
| 94 | + }) |
| 95 | + |
| 96 | + it('supports mixing DOM element and string slots', async () => { |
| 97 | + const page = await createPage('/preview-test-dom-slots.html') |
| 98 | + |
| 99 | + // Wait for mixed slot component to render |
| 100 | + await page.waitForFunction(() => { |
| 101 | + const target = document.getElementById('test-mixed-slots') |
| 102 | + return target && target.innerHTML.includes('DOM slot content') |
| 103 | + && target.innerHTML.includes('String Slot') |
| 104 | + }, { timeout: 15000 }) |
| 105 | + |
| 106 | + const mixedContent = await page.evaluate(() => { |
| 107 | + const target = document.getElementById('test-mixed-slots') |
| 108 | + return { |
| 109 | + hasDOMSlot: target?.innerHTML.includes('DOM slot content'), |
| 110 | + hasStringSlot: target?.innerHTML.includes('String Slot'), |
| 111 | + } |
| 112 | + }) |
| 113 | + |
| 114 | + expect(mixedContent.hasDOMSlot).toBe(true) |
| 115 | + expect(mixedContent.hasStringSlot).toBe(true) |
| 116 | + await page.close() |
| 117 | + }) |
| 118 | + |
| 119 | + it('removes visually-hidden class from slot containers', async () => { |
| 120 | + const page = await createPage('/preview-test-dom-slots.html') |
| 121 | + |
| 122 | + // Wait for rendering |
| 123 | + await page.waitForFunction(() => { |
| 124 | + return document.getElementById('test-element') !== null |
| 125 | + }, { timeout: 15000 }) |
| 126 | + |
| 127 | + // Check that slot containers with visually-hidden were removed |
| 128 | + const hiddenContainersRemaining = await page.evaluate(() => { |
| 129 | + // After slots are moved, the original containers should be removed |
| 130 | + const container = document.getElementById('test-dom-slot-simple') |
| 131 | + const hiddenDivs = container?.querySelectorAll('.visually-hidden[data-slot]') |
| 132 | + return hiddenDivs?.length || 0 |
| 133 | + }) |
| 134 | + |
| 135 | + // Should be 0 because containers are removed after moving children |
| 136 | + expect(hiddenContainersRemaining).toBe(0) |
| 137 | + await page.close() |
| 138 | + }) |
| 139 | +}) |
0 commit comments