-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Split index into focused files * Update deps * Add missing quotes
- Loading branch information
Showing
7 changed files
with
216 additions
and
210 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
Oops, something went wrong.