Skip to content

Commit 20465ab

Browse files
authored
fix: find functional components in shallow (#408)
1 parent f8ce240 commit 20465ab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+490
-474
lines changed

dist/vue-test-utils.js

Lines changed: 127 additions & 108 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"setup": "node build/install-hooks.js",
2323
"test": "npm run lint && npm run lint:docs && npm run flow && npm run test:types && npm run test:unit && npm run test:unit:karma",
2424
"test:compat": "test/test.sh",
25-
"test:unit": "npm run build:test && cross-env BABEL_ENV=test && mocha-webpack --webpack-config build/webpack.test.config.js test/unit/specs --recursive --require test/unit/setup/mocha.setup.js",
26-
"test:unit:karma": "npm run build:test && cross-env BABEL_ENV=test TARGET=browser karma start test/unit/setup/karma.conf.js --single-run",
25+
"test:unit": "npm run build:test && cross-env BABEL_ENV=test && mocha-webpack --webpack-config build/webpack.test.config.js test/specs --recursive --require test/setup/mocha.setup.js",
26+
"test:unit:karma": "npm run build:test && cross-env BABEL_ENV=test TARGET=browser karma start test/setup/karma.conf.js --single-run",
2727
"test:types": "tsc -p types",
2828
"release": "bash build/release.sh",
2929
"release:note": "node build/gen-release-note.js"

src/lib/find-vue-components.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export function vmFunctionalCtorMatchesSelector (component: VNode, Ctor: Object)
7070
throwError('find for functional components is not support in Vue < 2.3')
7171
}
7272

73+
if (!Ctor) {
74+
return false
75+
}
76+
7377
if (!component[FUNCTIONAL_OPTIONS]) {
7478
return false
7579
}
@@ -86,7 +90,10 @@ export default function findVueComponents (
8690
const nodes = root._vnode
8791
? findAllFunctionalComponentsFromVnode(root._vnode)
8892
: findAllFunctionalComponentsFromVnode(root)
89-
return nodes.filter(node => vmFunctionalCtorMatchesSelector(node, selector._Ctor))
93+
return nodes.filter(node =>
94+
vmFunctionalCtorMatchesSelector(node, selector._Ctor) ||
95+
node[FUNCTIONAL_OPTIONS].name === selector.name
96+
)
9097
}
9198
const components = root._isVue
9299
? findAllVueComponentsFromVm(root)

src/lib/stub-components.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ function getCoreProperties (component: Component): Object {
3535
staticStyle: component.staticStyle,
3636
style: component.style,
3737
normalizedStyle: component.normalizedStyle,
38-
nativeOn: component.nativeOn
38+
nativeOn: component.nativeOn,
39+
functional: component.functional
3940
}
4041
}
4142
function createStubFromString (templateString: string, originalComponent: Component): Object {
@@ -51,7 +52,7 @@ function createStubFromString (templateString: string, originalComponent: Compon
5152
function createBlankStub (originalComponent: Component) {
5253
return {
5354
...getCoreProperties(originalComponent),
54-
render: () => {}
55+
render: h => h('')
5556
}
5657
}
5758

@@ -126,6 +127,7 @@ function stubComponents (components: Object, stubbedComponents: Object) {
126127
Object.keys(components).forEach(component => {
127128
// Remove cached constructor
128129
delete components[component]._Ctor
130+
console.log(components[component].name)
129131
if (!components[component].name) {
130132
components[component].name = component
131133
}

test/resources/test-utils.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
/* global describe, it*/
2+
13
import Vue from 'vue'
4+
import { shallow, mount } from '~vue-test-utils'
25

36
export const vueVersion = Number(`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`)
47

@@ -17,3 +20,29 @@ export function listenersSupported () {
1720
export function functionalSFCsSupported () {
1821
return vueVersion >= 2.5
1922
}
23+
24+
export function describeWithShallowAndMount (spec, cb) {
25+
;[mount, shallow].forEach(method => {
26+
describe(`${spec} with ${method.name}`, () => cb(method))
27+
})
28+
}
29+
30+
describeWithShallowAndMount.skip = function (spec, cb) {
31+
;[mount, shallow].forEach(method => {
32+
describe.skip(`${spec} with ${method.name}`, () => cb(method))
33+
})
34+
}
35+
36+
describeWithShallowAndMount.only = function (spec, cb) {
37+
;[mount, shallow].forEach(method => {
38+
describe.only(`${spec} with ${method.name}`, () => cb(method))
39+
})
40+
}
41+
42+
export function itSkipIf (predicate, spec, cb) {
43+
if (predicate) {
44+
it.skip(spec, cb)
45+
} else {
46+
it(spec, cb)
47+
}
48+
}

test/unit/setup/karma.conf.js renamed to test/setup/karma.conf.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
const webpackConfig = require('../../../build/webpack.test.config.js')
1+
const webpackConfig = require('../../build/webpack.test.config.js')
22

33
module.exports = function (config) {
44
config.set({
55
browsers: ['ChromeHeadless'],
66
frameworks: ['mocha', 'sinon-chai'],
77
reporters: ['spec'],
88
files: [
9-
'../../../node_modules/babel-polyfill/dist/polyfill.js',
9+
'../../node_modules/babel-polyfill/dist/polyfill.js',
1010
'../specs/**/*.+(vue|js)'
1111
],
1212
preprocessors: {
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)