Skip to content

Add custom Vuetify render wrapper to Vuetify test #114

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 1 commit into from
Nov 18, 2019
Merged
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
31 changes: 22 additions & 9 deletions src/__tests__/vuetify.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,34 @@ import VuetifyDemoComponent from './components/Vuetify'

// We need to use a global Vue instance, otherwise Vuetify will complain about
// read-only attributes.
// This could also be done in a custom Jest-test-setup file to execute for all tests.
// More info: https://github.com/vuetifyjs/vuetify/issues/4068
// https://vuetifyjs.com/en/getting-started/unit-testing
Vue.use(Vuetify)

// Vuetify requires you to wrap you app with a v-app component that provides
// a <div data-app="true"> node. So you can do that, or you can also set the
// attribute to the DOM.
document.body.setAttribute('data-app', true)
// Another solution is to create a custom renderer that provides all the
// environment required by Vuetify.
// Custom render wrapper to integrate Vuetify with Vue Testing Library.
// Vuetify requires you to wrap your app with a v-app component that provides
// a <div data-app="true"> node.
export const renderWithVuetify = (component, options, callback) => {
return render(
// anonymous component
{
// Vue's render function
render(createElement) {
// wrap the component with a <div data-app="true"> node and render the test component
return createElement('div', {attrs: {'data-app': true}}, [
createElement(component),
])
},
},
// for Vuetify components that use the $vuetify instance property
{vuetify: new Vuetify(), ...options},
callback,
)
}

test('renders a Vuetify-powered component', async () => {
const {getByText} = render(VuetifyDemoComponent, {
vuetify: new Vuetify(),
})
const {getByText} = renderWithVuetify(VuetifyDemoComponent)

await fireEvent.click(getByText('open'))

Expand Down