Skip to content
Closed
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
45 changes: 45 additions & 0 deletions src/__tests__/compose-configuration-callbacks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import '@testing-library/jest-dom'
import {render, screen} from '@testing-library/vue'
import Vuei18n from 'vue-i18n'
import Button from './components/Button'

/**
* configuration callbacks may be shared shared between tests / test suites
* e.g. for common plugin installation
*/
const installGlobalComponents = vue => {
vue.component('CustomButton', Button)
}

const createVuei18nInstall = overrides => vue => {
vue.use(Vuei18n)

return {
i18n: new Vuei18n({
locale: 'en',
fallbackLocale: 'en',
...overrides,
}),
}
}

test('can merge multiple configuration callbacks', () => {
render(
{
template: `<custom-button :text="$t('welcome')" />`,
},
{},
[
installGlobalComponents,
createVuei18nInstall({
messages: {
en: {
welcome: 'Hello',
},
},
}),
],
)

expect(screen.getByRole('button')).toHaveTextContent('Hello')
})
14 changes: 13 additions & 1 deletion src/vue-testing-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ function render(
})
}

if (configurationCb && typeof configurationCb === 'function') {
if (configurationCb) {
if (Array.isArray(configurationCb)) {
configurationCb = composeConfigurationCallbacks(configurationCb)
}

additionalOptions = configurationCb(localVue, vuexStore, router)
}

Expand Down Expand Up @@ -85,6 +89,14 @@ function render(
}
}

function composeConfigurationCallbacks(callbacks) {
return (...args) =>
callbacks.reduce(
(mergedReturns, cb) => ({...mergedReturns, ...cb(...args)}),
{},
)
}

function cleanup() {
mountedWrappers.forEach(cleanupAtWrapper)
}
Expand Down