Skip to content

Commit e5dbbac

Browse files
author
Dobromir Hristov
committed
refactor: expose a default slot for content in Card.vue
1 parent 36b9fca commit e5dbbac

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

src/components/Card.vue

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,9 @@
3838
>
3939
{{ title }}
4040
</div>
41-
<ContentNode
42-
v-if="content"
43-
:content="content"
44-
:id="contentId"
45-
/>
41+
<div v-if="$slots.default" class="card-content" :id="contentId">
42+
<slot />
43+
</div>
4644
<component
4745
v-if="linkText"
4846
:is="hasButton ? 'ButtonLink': 'div'"
@@ -70,7 +68,6 @@ export default {
7068
Reference,
7169
DiagonalArrowIcon,
7270
InlineChevronRightIcon,
73-
ContentNode: () => import('docc-render/components/ContentNode.vue'),
7471
CardCover,
7572
ButtonLink,
7673
},
@@ -108,13 +105,10 @@ export default {
108105
type: String,
109106
required: false,
110107
},
111-
content: {
112-
type: Array,
113-
required: false,
114-
},
115108
url: {
116109
type: String,
117110
required: false,
111+
default: '',
118112
},
119113
eyebrow: {
120114
type: String,
@@ -126,7 +120,7 @@ export default {
126120
},
127121
size: {
128122
type: String,
129-
validator: s => s in CardSize,
123+
validator: s => Object.prototype.hasOwnProperty.call(CardSize, s),
130124
},
131125
title: {
132126
type: String,
@@ -264,7 +258,7 @@ $content-margin: 4px;
264258
}
265259
}
266260
267-
.content {
261+
.card-content {
268262
color: var(--color-card-content-text);
269263
margin-top: $content-margin;
270264
}

tests/unit/components/Card.spec.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import { shallowMount } from '@vue/test-utils';
1212
import Card from 'docc-render/components/Card.vue';
1313
import CardSize from 'docc-render/constants/CardSize';
1414
import Reference from 'docc-render/components/ContentNode/Reference.vue';
15-
import ContentNode from 'docc-render/components/ContentNode.vue';
1615
import { flushPromises } from '../../../test-utils';
1716

1817
const {
@@ -44,9 +43,6 @@ describe('Card', () => {
4443
const mountCard = options => shallowMount(Card, {
4544
propsData,
4645
provide,
47-
stubs: {
48-
ContentNode,
49-
},
5046
...options,
5147
});
5248

@@ -188,20 +184,24 @@ describe('Card', () => {
188184
expect(h3.text()).toBe(propsData.title);
189185
});
190186

191-
it('renders a `ContentNode` when content is provided', async () => {
192-
const content = [
193-
{
194-
type: 'paragraph',
195-
inlineContent: [{ type: 'text', text: 'hello world' }],
187+
it('renders content in the default slot', async () => {
188+
const content = 'Foo bar baz';
189+
const wrapper = mountCard({
190+
slots: {
191+
default: content,
196192
},
197-
];
198-
const propsWithContent = { ...propsData, content };
199-
const wrapper = mountCard({ propsData: propsWithContent });
193+
});
194+
await flushPromises();
195+
const contentDiv = wrapper.find('.card-content');
196+
expect(contentDiv.exists()).toBe(true);
197+
expect(contentDiv.text()).toEqual(content);
198+
expect(contentDiv.attributes('id')).toMatch(cardContentIdRE);
199+
});
200+
201+
it('does not render the card-content, if no default slot provided', async () => {
202+
const wrapper = mountCard();
200203
await flushPromises();
201-
const node = wrapper.find(ContentNode);
202-
expect(node.exists()).toBe(true);
203-
expect(node.props('content')).toEqual(content);
204-
expect(node.attributes('id')).toMatch(cardContentIdRE);
204+
expect(wrapper.find('.card-content').exists()).toBe(false);
205205
});
206206

207207
it('renders card as a `floatingStyle`', () => {

0 commit comments

Comments
 (0)