Skip to content

Update find and findAll to be more robust #681

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions flow/modules.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ declare module 'vue-server-renderer' {
declare module 'cheerio' {
declare module.exports: any;
}

declare module 'html-tags' {
declare module.exports: any;
}

declare module 'svg-elements' {
declare module.exports: any;
}
24 changes: 19 additions & 5 deletions packages/shared/validators.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// @flow

import htmlTags from 'html-tags'
import svgElements from 'svg-elements'
import {
throwError,
capitalize,
Expand All @@ -18,10 +21,17 @@ export function isDomSelector (selector: any) {
} catch (error) {
throwError('mount must be run in a browser environment like PhantomJS, jsdom or chrome')
}

try {
document.querySelector(selector)
return true

const pseudoSelectors = ['.', '#', '[', ':', '>', ' ']
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Quite ugly but needed to cover all existing test cases:

  • find("#id")
  • find(".class")
  • find("div") html element
  • find("text") svg element
  • `find("div > p") pseudo selector, although this is prob bad practise in tests anyway
  • find("p:first-of-type")

if (htmlTags.includes(selector) ||
svgElements.includes(selector) ||
selector.split('').some(char => pseudoSelectors.includes(char))) {
return true
} else {
return false
}
} catch (error) {
return false
}
Expand Down Expand Up @@ -61,11 +71,15 @@ export function isRefSelector (refOptionsObject: any) {
}

export function isNameSelector (nameOptionsObject: any) {
if (typeof nameOptionsObject !== 'object' || nameOptionsObject === null) {
if (nameOptionsObject === null) {
return false
}

return !!nameOptionsObject.name
if (typeof nameOptionsObject === 'object') {
return !!nameOptionsObject.name
}
if (typeof nameOptionsObject === 'string' && !/[^A-Za-z0-9\-_]/.exec(nameOptionsObject)) {
return !!nameOptionsObject
}
}

export function templateContainsComponent (template: string, name: string) {
Expand Down
4 changes: 3 additions & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
"vue-template-compiler": "^2.x"
},
"dependencies": {
"lodash": "^4.17.4"
"html-tags": "^2.0.0",
"lodash": "^4.17.4",
"svg-elements": "^1.0.2"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

both libs are just json lists. an alternative would be just copy paste the json lists into the project

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO,
Since their size is small,
I think it is not necessary to use library.
Vue.js does not use the library as below.
https://github.com/vuejs/vue/blob/f76d16ed9507d4c2a90243ea3d77ccf00df29346/src/platforms/web/util/element.js#L11-L32

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, I agree. Just going to grab the list from vuejs/vue repo.

}
}
14 changes: 11 additions & 3 deletions packages/test-utils/src/find-vue-components.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// @flow
// // name
import {
FUNCTIONAL_OPTIONS,
VUE_VERSION
Expand Down Expand Up @@ -51,8 +52,11 @@ function findAllFunctionalComponentsFromVnode (
}

export function vmCtorMatchesName (vm: Component, name: string): boolean {
return !!((vm.$vnode && vm.$vnode.componentOptions &&
vm.$vnode.componentOptions.Ctor.options.name === name) ||
if (!name) {
return false
}
return !!((vm.$vnode && vm.$vnode.componentOptions && vm.$vnode.componentOptions.Ctor.options.name === name) ||
vm.$vnode.componentOptions.tag === name ||
(vm._vnode &&
vm._vnode.functionalOptions &&
vm._vnode.functionalOptions.name === name) ||
Expand Down Expand Up @@ -102,7 +106,11 @@ export default function findVueComponents (
node[FUNCTIONAL_OPTIONS].name === selector.name
)
}
const nameSelector = typeof selector === 'function' ? selector.options.name : selector.name

const nameSelector = typeof selector === 'function'
? selector.options.name
: typeof selector === 'object' ? selector.name : selector

const components = root._isVue
? findAllVueComponentsFromVm(root)
: findAllVueComponentsFromVnode(root)
Expand Down
5 changes: 5 additions & 0 deletions test/specs/wrapper/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ describeWithShallowAndMount('find', (mountingMethod) => {
expect(wrapper.find(Component).vnode).to.be.an('object')
})

it.only('returns Wrapper of Vue Components matching component', () => {
const wrapper = mountingMethod(ComponentWithChild)
expect(wrapper.find('child-component').vnode).to.be.an('object')
})

itSkipIf(
isRunningPhantomJS,
'returns Wrapper of class component', () => {
Expand Down
8 changes: 8 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4983,6 +4983,10 @@ html-minifier@^3.2.3:
relateurl "0.2.x"
uglify-js "3.3.x"

html-tags@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/html-tags/-/html-tags-2.0.0.tgz#10b30a386085f43cede353cc8fa7cb0deeea668b"

htmlparser2@^3.8.2, htmlparser2@^3.9.1:
version "3.9.2"
resolved "https://registry.yarnpkg.com/htmlparser2/-/htmlparser2-3.9.2.tgz#1bdf87acca0f3f9e53fa4fcceb0f4b4cbb00b338"
Expand Down Expand Up @@ -9004,6 +9008,10 @@ supports-color@^5.3.0, supports-color@^5.4.0:
dependencies:
has-flag "^3.0.0"

svg-elements@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/svg-elements/-/svg-elements-1.0.2.tgz#0d976a499055e26429c136b4bca07c12fac8c8c3"

svgo@^0.7.0:
version "0.7.2"
resolved "https://registry.yarnpkg.com/svgo/-/svgo-0.7.2.tgz#9f5772413952135c6fefbf40afe6a4faa88b4bb5"
Expand Down