File tree Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Expand file tree Collapse file tree 2 files changed +54
-0
lines changed Original file line number Diff line number Diff line change 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 >
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments