-
Notifications
You must be signed in to change notification settings - Fork 113
chore: Cleanup structure #209
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| /* eslint-disable testing-library/no-wait-for-empty-callback */ | ||
| import {waitFor, fireEvent as dtlFireEvent} from '@testing-library/dom' | ||
|
|
||
| // Vue Testing Lib's version of fireEvent will call DOM Testing Lib's | ||
| // version of fireEvent. The reason is because we need to wait another | ||
| // event loop tick to allow Vue to flush and update the DOM | ||
| // More info: https://vuejs.org/v2/guide/reactivity.html#Async-Update-Queue | ||
|
|
||
| async function fireEvent(...args) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question out of curiosity. when using VTL, should we always use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hi! Using user-event is advisable, as it is closer to the user. We even showcase some examples: https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/user-event.js |
||
| dtlFireEvent(...args) | ||
| await waitFor(() => {}) | ||
| } | ||
|
|
||
| Object.keys(dtlFireEvent).forEach(key => { | ||
| fireEvent[key] = async (...args) => { | ||
| warnOnChangeOrInputEventCalledDirectly(args[1], key) | ||
|
|
||
| dtlFireEvent[key](...args) | ||
| await waitFor(() => {}) | ||
| } | ||
| }) | ||
|
|
||
| fireEvent.touch = async elem => { | ||
| await fireEvent.focus(elem) | ||
| await fireEvent.blur(elem) | ||
| } | ||
|
|
||
| // fireEvent.update is a small utility to provide a better experience when | ||
| // working with v-model. | ||
| // Related upstream issue: https://github.com/vuejs/vue-test-utils/issues/345#issuecomment-380588199 | ||
| // Examples: https://github.com/testing-library/vue-testing-library/blob/master/src/__tests__/form.js | ||
| fireEvent.update = (elem, value) => { | ||
| const tagName = elem.tagName | ||
| const type = elem.type | ||
|
|
||
| switch (tagName) { | ||
| case 'OPTION': { | ||
| elem.selected = true | ||
|
|
||
| const parentSelectElement = | ||
| elem.parentElement.tagName === 'OPTGROUP' | ||
| ? elem.parentElement.parentElement | ||
| : elem.parentElement | ||
|
|
||
| return fireEvent.change(parentSelectElement) | ||
| } | ||
|
|
||
| case 'INPUT': { | ||
| if (['checkbox', 'radio'].includes(type)) { | ||
| elem.checked = true | ||
| return fireEvent.change(elem) | ||
| } else if (type === 'file') { | ||
| return fireEvent.change(elem) | ||
| } else { | ||
| elem.value = value | ||
| if (elem._vModifiers?.lazy) { | ||
| return fireEvent.change(elem) | ||
| } | ||
| return fireEvent.input(elem) | ||
| } | ||
| } | ||
|
|
||
| case 'TEXTAREA': { | ||
| elem.value = value | ||
| if (elem._vModifiers?.lazy) { | ||
| return fireEvent.change(elem) | ||
| } | ||
| return fireEvent.input(elem) | ||
| } | ||
|
|
||
| case 'SELECT': { | ||
| elem.value = value | ||
| return fireEvent.change(elem) | ||
| } | ||
|
|
||
| default: | ||
| // do nothing | ||
| } | ||
|
|
||
| return null | ||
| } | ||
|
|
||
| function warnOnChangeOrInputEventCalledDirectly(eventValue, eventKey) { | ||
| if (process.env.VTL_SKIP_WARN_EVENT_UPDATE) return | ||
|
|
||
| if (eventValue && (eventKey === 'change' || eventKey === 'input')) { | ||
| console.warn( | ||
| `Using "fireEvent.${eventKey}" may lead to unexpected results. Please use fireEvent.update() instead.`, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export {fireEvent} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import {cleanup} from './render' | ||
|
|
||
| // If we're running in a test runner that supports afterEach then we'll | ||
| // automatically run cleanup after each test. | ||
| // This ensures that tests run in isolation from each other. | ||
| // If you don't like this, set the VTL_SKIP_AUTO_CLEANUP variable to 'true'. | ||
| if (typeof afterEach === 'function' && !process.env.VTL_SKIP_AUTO_CLEANUP) { | ||
| afterEach(() => { | ||
| cleanup() | ||
| }) | ||
| } | ||
|
|
||
| export * from '@testing-library/dom' | ||
| export {cleanup, render} from './render' | ||
| export {fireEvent} from './fire-event' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import {createLocalVue, mount} from '@vue/test-utils' | ||
|
|
||
| import {getQueriesForElement, prettyDOM} from '@testing-library/dom' | ||
|
|
||
| const mountedWrappers = new Set() | ||
|
|
||
| function render( | ||
| Component, | ||
| { | ||
| store = null, | ||
| routes = null, | ||
| container: customContainer, | ||
| baseElement: customBaseElement, | ||
| ...mountOptions | ||
| } = {}, | ||
| configurationCb, | ||
| ) { | ||
| const div = document.createElement('div') | ||
| const baseElement = customBaseElement || customContainer || document.body | ||
| const container = customContainer || baseElement.appendChild(div) | ||
|
|
||
| const attachTo = document.createElement('div') | ||
| container.appendChild(attachTo) | ||
|
|
||
| const localVue = createLocalVue() | ||
| let vuexStore = null | ||
| let router = null | ||
| let callbackOptions = {} | ||
|
|
||
| if (store) { | ||
| const Vuex = require('vuex') | ||
| localVue.use(Vuex) | ||
|
|
||
| vuexStore = new Vuex.Store(store) | ||
| } | ||
|
|
||
| if (routes) { | ||
| const requiredRouter = require('vue-router') | ||
| const VueRouter = requiredRouter.default || requiredRouter | ||
| localVue.use(VueRouter) | ||
|
|
||
| router = new VueRouter({routes}) | ||
| } | ||
|
|
||
| if (configurationCb && typeof configurationCb === 'function') { | ||
| callbackOptions = configurationCb(localVue, vuexStore, router) | ||
| } | ||
|
|
||
| if (!mountOptions.propsData && !!mountOptions.props) { | ||
| mountOptions.propsData = mountOptions.props | ||
| delete mountOptions.props | ||
| } | ||
|
|
||
| const wrapper = mount(Component, { | ||
| attachTo, | ||
| localVue, | ||
| router, | ||
| store: vuexStore, | ||
| ...mountOptions, | ||
| ...callbackOptions, | ||
| }) | ||
|
|
||
| mountedWrappers.add(wrapper) | ||
| container.appendChild(wrapper.element) | ||
|
|
||
| return { | ||
| container, | ||
| baseElement, | ||
| debug: (el = baseElement, ...args) => | ||
| Array.isArray(el) | ||
| ? el.forEach(e => console.log(prettyDOM(e, ...args))) | ||
| : console.log(prettyDOM(el, ...args)), | ||
| unmount: () => wrapper.destroy(), | ||
| isUnmounted: () => wrapper.vm._isDestroyed, | ||
| html: () => wrapper.html(), | ||
| emitted: () => wrapper.emitted(), | ||
| updateProps: _ => wrapper.setProps(_), | ||
| ...getQueriesForElement(baseElement), | ||
| } | ||
| } | ||
|
|
||
| function cleanup() { | ||
| mountedWrappers.forEach(cleanupAtWrapper) | ||
| } | ||
|
|
||
| function cleanupAtWrapper(wrapper) { | ||
| if ( | ||
| wrapper.element.parentNode && | ||
| wrapper.element.parentNode.parentNode === document.body | ||
| ) { | ||
| document.body.removeChild(wrapper.element.parentNode) | ||
| } | ||
|
|
||
| try { | ||
| wrapper.destroy() | ||
| } finally { | ||
| mountedWrappers.delete(wrapper) | ||
| } | ||
| } | ||
|
|
||
| export {cleanup, render} |