Skip to content

Instantly share code, notes, and snippets.

@dasDaniel
Last active March 26, 2019 17:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dasDaniel/d4fb8f0eb6f36acdbe7b1ce66c034949 to your computer and use it in GitHub Desktop.
Save dasDaniel/d4fb8f0eb6f36acdbe7b1ce66c034949 to your computer and use it in GitHub Desktop.
helper functions for vue-test-utils
function withWrapperArray(wrapperArray) {
return {
childSelectorHasText: (selector, str) => wrapperArray.filter(i => i.find(selector).text().match(str)),
hasText: (str) => wrapperArray.filter(i => i.text().match(str)),
areVisible: () => wrapperArray.wrappers.filter(w => w.isVisible()).length,
areHidden: () => wrapperArray.wrappers.filter(w => !w.isVisible()).length,
areAllVisible: () => wrapperArray.wrappers.every(w => w.isVisible()),
areAllHidden: () => wrapperArray.wrappers.every(w => !w.isVisible()),
}
}
// find first element that that has class name `.filter__name` with a textvalue of `Vehicles`
withWrapperArray(wrapper.findAll('.filter__name')).hasText('Vehicles').at(0);
// find an element that has a child with matching text content
withWrapperArray(wrapper.findAll('.filter')).childSelectorHasText('label', 'Saab').at(0)
// get the input that's a sibling of the matched label
withWrapperArray(wrapper.findAll('.filter')).childSelectorHasText('label', 'Saab').at(0).find('input')
// are all matched elements hidden?
expect(withWrapperArray(wrapper.findAll('.filter')).areAllHidden()).toBeTruthy();
// are all matched elements visible?
expect(withWrapperArray(wrapper.findAll('.filter')).areAllVisible()).toBeTruthy();
// are some matched elements hidden?
expect(withWrapperArray(wrapper.findAll('.filter')).areHidden()).toBeTruthy();
// are some matched elements visible?
expect(withWrapperArray(wrapper.findAll('.filter')).areVisible()).toBeTruthy();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment