diff --git a/src/__tests__/compose-configuration-callbacks.js b/src/__tests__/compose-configuration-callbacks.js new file mode 100644 index 00000000..33cde9d9 --- /dev/null +++ b/src/__tests__/compose-configuration-callbacks.js @@ -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: ``, + }, + {}, + [ + installGlobalComponents, + createVuei18nInstall({ + messages: { + en: { + welcome: 'Hello', + }, + }, + }), + ], + ) + + expect(screen.getByRole('button')).toHaveTextContent('Hello') +}) diff --git a/src/vue-testing-library.js b/src/vue-testing-library.js index 5de7f5fb..e4b3247c 100644 --- a/src/vue-testing-library.js +++ b/src/vue-testing-library.js @@ -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) } @@ -85,6 +89,14 @@ function render( } } +function composeConfigurationCallbacks(callbacks) { + return (...args) => + callbacks.reduce( + (mergedReturns, cb) => ({...mergedReturns, ...cb(...args)}), + {}, + ) +} + function cleanup() { mountedWrappers.forEach(cleanupAtWrapper) }