Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit fc9a3d9

Browse files
authored
Merge pull request #71 from mb200/slots-example
#68 add example for slots and scoped slots
2 parents 09bad0c + b84b782 commit fc9a3d9

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

‎tests/__tests__/components/Card.vue

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<template>
2+
<div class="card">
3+
<slot name="header" />
4+
<slot :content="content">
5+
<!-- Fallback content if no default slot is given -->
6+
<p>Nothing used the {{ content }}</p>
7+
</slot>
8+
<slot name="footer" />
9+
</div>
10+
</template>
11+
12+
<script>
13+
// For the sake of demoing scopedSlots, this Card component
14+
// passes a simple string down to its default slot.
15+
export default {
16+
data() {
17+
return {
18+
content: 'Scoped content!'
19+
}
20+
}
21+
}
22+
</script>

‎tests/__tests__/slots.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import '@testing-library/jest-dom/extend-expect'
2+
import { render } from '@testing-library/vue'
3+
import Card from './components/Card'
4+
5+
// In this test file we demo how to test a component with slots and a scoped slot.
6+
7+
// Usage is the same as Vue Test Utils, since slots and scopedSlots
8+
// in the render options are directly passed through to the Utils mount().
9+
// For more, see: https://vue-test-utils.vuejs.org/api/options.html#slots
10+
test('Card component', () => {
11+
const { getByText, queryByText } = render(Card, {
12+
slots: {
13+
header: '<h1>HEADER</h1>',
14+
footer: '<div>FOOTER</div>'
15+
},
16+
scopedSlots: {
17+
default: '<p>Yay! {{props.content}}</p>'
18+
}
19+
})
20+
21+
// The default slot should render the template above with the scoped prop "content".
22+
getByText('Yay! Scoped content!')
23+
24+
// Instead of the default slot's fallback content.
25+
expect(
26+
queryByText('Nothing used the Scoped content!')
27+
).not.toBeInTheDocument()
28+
29+
// And the header and footer slots should be rendered with the given templates.
30+
getByText('HEADER')
31+
getByText('FOOTER')
32+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /