Skip to content

#68 add example for slots and scoped slots #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions tests/__tests__/components/Card.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<template>
<div class="card">
<slot name="header" />
<slot :content="content">
<!-- Fallback content if no default slot is given -->
<p>Nothing used the {{ content }}</p>
</slot>
<slot name="footer" />
</div>
</template>

<script>
// For the sake of demoing scopedSlots, this Card component
// passes a simple string down to its default slot.
export default {
data() {
return {
content: 'Scoped content!'
}
}
}
</script>
32 changes: 32 additions & 0 deletions tests/__tests__/slots.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import '@testing-library/jest-dom/extend-expect'
import { render } from '@testing-library/vue'
import Card from './components/Card'

// In this test file we demo how to test a component with slots and a scoped slot.

// Usage is the same as Vue Test Utils, since slots and scopedSlots
// in the render options are directly passed through to the Utils mount().
// For more, see: https://vue-test-utils.vuejs.org/api/options.html#slots
test('Card component', () => {
const { getByText, queryByText } = render(Card, {
slots: {
header: '<h1>HEADER</h1>',
footer: '<div>FOOTER</div>'
},
scopedSlots: {
default: '<p>Yay! {{props.content}}</p>'
}
})

// The default slot should render the template above with the scoped prop "content".
getByText('Yay! Scoped content!')

// Instead of the default slot's fallback content.
expect(
queryByText('Nothing used the Scoped content!')
).not.toBeInTheDocument()

// And the header and footer slots should be rendered with the given templates.
getByText('HEADER')
getByText('FOOTER')
})