|
| 1 | +import { expect, fixture, html } from '@open-wc/testing' |
| 2 | +import '../index' |
| 3 | + |
| 4 | +describe('<q-card />', async () => { |
| 5 | + // 测试卡片基本渲染功能,包括默认属性 |
| 6 | + it('renders with default properties', async () => { |
| 7 | + const el = await fixture(html`<q-card>Card Content</q-card>`) |
| 8 | + |
| 9 | + expect(el.title).to.equal('') |
| 10 | + expect(el.shadow).to.equal('light') |
| 11 | + expect(el.bordered).to.be.true |
| 12 | + expect(el.radius).to.equal(8) |
| 13 | + expect(el.loading).to.be.false |
| 14 | + expect(el.headerImage).to.equal('') |
| 15 | + |
| 16 | + const card = el.shadowRoot.querySelector('.q-card') |
| 17 | + expect(card).to.exist |
| 18 | + expect(card.classList.contains('q-card')).to.be.true |
| 19 | + expect(card.classList.contains('q-card-shadow-light')).to.be.true |
| 20 | + expect(card.classList.contains('q-card-bordered')).to.be.true |
| 21 | + |
| 22 | + // 验证内容区域 |
| 23 | + const content = el.shadowRoot.querySelector('.q-card-content') |
| 24 | + expect(content).to.exist |
| 25 | + expect(content.textContent).to.include('Card Content') |
| 26 | + }) |
| 27 | + |
| 28 | + // 测试标题属性是否正常工作 |
| 29 | + it('renders with title', async () => { |
| 30 | + const title = 'Card Title' |
| 31 | + const el = await fixture(html`<q-card title=${title}>Card Content</q-card>`) |
| 32 | + const cardTitle = el.shadowRoot.querySelector('.q-card-title') |
| 33 | + |
| 34 | + expect(el.title).to.equal(title) |
| 35 | + expect(cardTitle).to.exist |
| 36 | + expect(cardTitle.textContent).to.equal(title) |
| 37 | + }) |
| 38 | + |
| 39 | + // 测试不同阴影程度(light、medium、dark、none)的卡片样式是否正确 |
| 40 | + it('renders with different shadow values', async () => { |
| 41 | + const shadows = ['light', 'medium', 'dark', 'none'] |
| 42 | + |
| 43 | + for (const shadowType of shadows) { |
| 44 | + const el = await fixture(html`<q-card shadow=${shadowType}>Card Content</q-card>`) |
| 45 | + const card = el.shadowRoot.querySelector('.q-card') |
| 46 | + |
| 47 | + expect(el.shadow).to.equal(shadowType) |
| 48 | + expect(card.classList.contains(`q-card-shadow-${shadowType}`)).to.be.true |
| 49 | + } |
| 50 | + }) |
| 51 | + |
| 52 | + // 测试边框属性是否正常工作 |
| 53 | + it('supports bordered property', async () => { |
| 54 | + // 有边框的卡片(默认) |
| 55 | + const borderedCard = await fixture(html`<q-card>Card Content</q-card>`) |
| 56 | + expect(borderedCard.bordered).to.be.true |
| 57 | + expect(borderedCard.shadowRoot.querySelector('.q-card').classList.contains('q-card-bordered')).to.be.true |
| 58 | + |
| 59 | + // 无边框的卡片 |
| 60 | + const borderlessCard = await fixture(html`<q-card bordered=${false}>Card Content</q-card>`) |
| 61 | + expect(borderlessCard.bordered).to.be.false |
| 62 | + expect(borderlessCard.shadowRoot.querySelector('.q-card').classList.contains('q-card-bordered')).to.be.false |
| 63 | + }) |
| 64 | + |
| 65 | + // 测试圆角属性是否正常工作 |
| 66 | + it('supports radius property', async () => { |
| 67 | + const radius = 16 |
| 68 | + const el = await fixture(html`<q-card radius=${radius}>Card Content</q-card>`) |
| 69 | + const card = el.shadowRoot.querySelector('.q-card') |
| 70 | + |
| 71 | + expect(el.radius).to.equal(radius) |
| 72 | + expect(card.style.borderRadius).to.equal(`${radius}px`) |
| 73 | + }) |
| 74 | + |
| 75 | + // 测试加载状态是否正常工作 |
| 76 | + it('supports loading state', async () => { |
| 77 | + const el = await fixture(html`<q-card loading>Card Content</q-card>`) |
| 78 | + const card = el.shadowRoot.querySelector('.q-card') |
| 79 | + |
| 80 | + expect(el.loading).to.be.true |
| 81 | + expect(card.classList.contains('q-card-is-loading')).to.be.true |
| 82 | + |
| 83 | + // 检查是否有加载遮罩和加载图标 |
| 84 | + const loadingMask = el.shadowRoot.querySelector('.q-card-loading-mask') |
| 85 | + expect(loadingMask).to.exist |
| 86 | + const loadingSpinner = el.shadowRoot.querySelector('.q-card-loading-spinner') |
| 87 | + expect(loadingSpinner).to.exist |
| 88 | + }) |
| 89 | + |
| 90 | + // 测试头部图片是否正常显示 |
| 91 | + it('renders with header image', async () => { |
| 92 | + const imageUrl = 'https://example.com/image.jpg' |
| 93 | + const el = await fixture(html`<q-card headerImage=${imageUrl}>Card Content</q-card>`) |
| 94 | + |
| 95 | + expect(el.headerImage).to.equal(imageUrl) |
| 96 | + |
| 97 | + const headerImage = el.shadowRoot.querySelector('.q-card-header-image') |
| 98 | + expect(headerImage).to.exist |
| 99 | + |
| 100 | + const img = headerImage.querySelector('img') |
| 101 | + expect(img).to.exist |
| 102 | + expect(img.getAttribute('src')).to.equal(imageUrl) |
| 103 | + expect(img.getAttribute('alt')).to.equal('card header') |
| 104 | + }) |
| 105 | + |
| 106 | + // 测试内容区域的slot功能 |
| 107 | + it('renders default slot content', async () => { |
| 108 | + const el = await fixture(html`<q-card><span class="test-content">Custom Content</span></q-card>`) |
| 109 | + const contentSlot = el.shadowRoot.querySelector('.q-card-content slot') |
| 110 | + |
| 111 | + expect(contentSlot).to.exist |
| 112 | + const assignedNodes = contentSlot.assignedNodes() |
| 113 | + expect(assignedNodes.length).to.be.greaterThan(0) |
| 114 | + expect(assignedNodes[0].textContent).to.include('Custom Content') |
| 115 | + }) |
| 116 | + |
| 117 | + // 测试底部操作区域的slot功能 |
| 118 | + it('renders footer slot content', async () => { |
| 119 | + const el = await fixture(html` |
| 120 | + <q-card> |
| 121 | + Card Content |
| 122 | + <div slot="footer" class="test-footer">Footer Content</div> |
| 123 | + </q-card> |
| 124 | + `) |
| 125 | + const footerSlot = el.shadowRoot.querySelector('.q-card-footer slot[name="footer"]') |
| 126 | + |
| 127 | + expect(footerSlot).to.exist |
| 128 | + const assignedNodes = footerSlot.assignedNodes() |
| 129 | + expect(assignedNodes.length).to.be.greaterThan(0) |
| 130 | + expect(assignedNodes[0].textContent).to.include('Footer Content') |
| 131 | + }) |
| 132 | + |
| 133 | + // 测试没有标题和头部图片时,头部不渲染 |
| 134 | + it('does not render header when no title and header image', async () => { |
| 135 | + const el = await fixture(html`<q-card>Card Content</q-card>`) |
| 136 | + const header = el.shadowRoot.querySelector('.q-card-header') |
| 137 | + |
| 138 | + expect(header).to.be.null |
| 139 | + }) |
| 140 | +}) |
0 commit comments