Skip to content

fix(plugins): allow setting vuex/router and custom plugins #200

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 3 commits into from
Jan 8, 2021
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,7 @@
"dependencies": {
"@babel/runtime": "^7.12.1",
"@testing-library/dom": "^7.28.1",
"@vue/test-utils": "^2.0.0-beta.12",
"lodash.merge": "^4.6.2"
"@vue/test-utils": "^2.0.0-beta.12"
},
"devDependencies": {
"@apollo/client": "3.3.6",
Expand All @@ -70,6 +69,7 @@
"isomorphic-unfetch": "^3.1.0",
"jest-serializer-vue": "^2.0.2",
"kcd-scripts": "^7.5.1",
"lodash.merge": "^4.6.2",
"msw": "^0.21.3",
"typescript": "^4.1.2",
"vee-validate": "^4.0.2",
Expand Down
49 changes: 49 additions & 0 deletions src/__tests__/plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import '@testing-library/jest-dom'
import ElementPlus from 'element-plus'
import userEvent from '@testing-library/user-event'
import {defineComponent} from 'vue'
import {render, screen, fireEvent, waitFor} from '..'
import {store} from './components/Store/store'

test('can set global options and custom options such as a store', async () => {
const ComponentWithStore = defineComponent({
template: `
<el-popover trigger="hover" content="this is content">
<template #reference>
<el-button @click="$store.dispatch('increment')">
Hover to activate
</el-button>
</template>
</el-popover>
<span data-testid="count-value">{{ $store.state.count }}</span>
<directive />
`,
})

const DirectiveStub = {
template: '<p>Search now</p>',
}

render(ComponentWithStore, {
store,
global: {
plugins: [ElementPlus],
stubs: {
Directive: DirectiveStub,
},
},
})

expect(screen.getByText('Search now')).toBeInTheDocument()

const button = screen.getByText('Hover to activate')
userEvent.hover(button)

await waitFor(() => expect(screen.getByText('this is content')).toBeVisible())

expect(screen.getByTestId('count-value')).toHaveTextContent('0')

await fireEvent.click(button)

expect(screen.getByTestId('count-value')).toHaveTextContent('1')
})
16 changes: 6 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable testing-library/no-wait-for-empty-callback */
import {mount} from '@vue/test-utils'
import merge from 'lodash.merge'

import {
getQueriesForElement,
Expand All @@ -25,7 +24,7 @@ function render(
const baseElement = customBaseElement || customContainer || document.body
const container = customContainer || baseElement.appendChild(div)

const plugins = []
const plugins = mountOptions.global?.plugins || []

if (store) {
const {createStore} = require('vuex')
Expand All @@ -41,14 +40,11 @@ function render(
plugins.push(routerPlugin)
}

const wrapper = mount(
Component,
merge({
attachTo: container,
global: {plugins},
...mountOptions,
}),
)
const wrapper = mount(Component, {
...mountOptions,
attachTo: container,
global: {...mountOptions.global, plugins},
})

// this removes the additional "data-v-app" div node from VTU:
// https://github.com/vuejs/vue-test-utils-next/blob/master/src/mount.ts#L196-L213
Expand Down