-
Notifications
You must be signed in to change notification settings - Fork 111
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question out of curiosity. when using VTL, should we always use the
fireEvent
exported by this library or instead should we useuser-event
? with RTL, user-event is suggested https://kentcdodds.com/blog/common-mistakes-with-react-testing-library#not-using-testing-libraryuser-eventThere was a problem hiding this comment.
Choose a reason for hiding this comment
The 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