Skip to content

Commit ef66c26

Browse files
authored
fix: use correct event type for checkbox v-model handler (#1083)
1 parent 92c3f71 commit ef66c26

File tree

12 files changed

+132
-159
lines changed

12 files changed

+132
-159
lines changed

Diff for: flow/modules.flow.js

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ declare module 'vue-server-renderer' {
2323
declare module 'cheerio' {
2424
declare module.exports: any;
2525
}
26+
27+
declare module 'semver' {
28+
declare module.exports: any;
29+
}

Diff for: packages/create-instance/create-instance.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import { createSlotVNodes } from './create-slot-vnodes'
44
import addMocks from './add-mocks'
55
import { addEventLogger } from './log-events'
66
import { addStubs } from './add-stubs'
7-
import { throwError, vueVersion } from 'shared/util'
7+
import { throwError } from 'shared/util'
8+
import { VUE_VERSION } from 'shared/consts'
89
import {
910
compileTemplate,
1011
compileTemplateForSlots
@@ -42,7 +43,7 @@ export default function createInstance (
4243
_Vue.options._base = _Vue
4344

4445
if (
45-
vueVersion < 2.3 &&
46+
VUE_VERSION < 2.3 &&
4647
typeof component === 'function' &&
4748
component.options
4849
) {
@@ -110,7 +111,7 @@ export default function createInstance (
110111
if (
111112
options.provide &&
112113
typeof options.provide === 'object' &&
113-
vueVersion < 2.5
114+
VUE_VERSION < 2.5
114115
) {
115116
const obj = { ...options.provide }
116117
options.provide = () => obj

Diff for: packages/create-instance/create-scoped-slots.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
// @flow
22

33
import { compileToFunctions } from 'vue-template-compiler'
4-
import { throwError, vueVersion } from 'shared/util'
4+
import { throwError } from 'shared/util'
5+
import { VUE_VERSION } from 'shared/consts'
56

67
function isDestructuringSlotScope (slotScope: string): boolean {
78
return slotScope[0] === '{' && slotScope[slotScope.length - 1] === '}'
@@ -39,7 +40,7 @@ function getVueTemplateCompilerHelpers (
3940
}
4041

4142
function validateEnvironment (): void {
42-
if (vueVersion < 2.1) {
43+
if (VUE_VERSION < 2.1) {
4344
throwError(`the scopedSlots option is only supported in [email protected]+.`)
4445
}
4546
}

Diff for: packages/create-instance/patch-render.js

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
import { createStubFromComponent } from './create-component-stubs'
2-
import { resolveComponent, semVerGreaterThan } from 'shared/util'
2+
import { resolveComponent } from 'shared/util'
33
import { isReservedTag } from 'shared/validators'
4-
import Vue from 'vue'
5-
import { BEFORE_RENDER_LIFECYCLE_HOOK } from 'shared/consts'
4+
import {
5+
BEFORE_RENDER_LIFECYCLE_HOOK,
6+
CREATE_ELEMENT_ALIAS
7+
} from 'shared/consts'
68

79
const isWhitelisted = (el, whitelist) => resolveComponent(el, whitelist)
810
const isAlreadyStubbed = (el, stubs) => stubs.has(el)
911
const isDynamicComponent = cmp => typeof cmp === 'function' && !cmp.cid
1012

11-
const CREATE_ELEMENT_ALIAS = semVerGreaterThan(Vue.version, '2.1.5')
12-
? '_c'
13-
: '_h'
14-
1513
function shouldExtend (component, _Vue) {
1614
return (
1715
(typeof component === 'function' && !isDynamicComponent(component)) ||

Diff for: packages/shared/consts.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import Vue from 'vue'
2-
import { semVerGreaterThan } from './util'
2+
import semver from 'semver'
33

44
export const NAME_SELECTOR = 'NAME_SELECTOR'
55
export const COMPONENT_SELECTOR = 'COMPONENT_SELECTOR'
66
export const REF_SELECTOR = 'REF_SELECTOR'
77
export const DOM_SELECTOR = 'DOM_SELECTOR'
88
export const INVALID_SELECTOR = 'INVALID_SELECTOR'
9+
910
export const VUE_VERSION = Number(
1011
`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`
1112
)
13+
1214
export const FUNCTIONAL_OPTIONS =
1315
VUE_VERSION >= 2.5 ? 'fnOptions' : 'functionalOptions'
1416

1517
export const BEFORE_RENDER_LIFECYCLE_HOOK =
16-
semVerGreaterThan(Vue.version, '2.1.8')
18+
semver.gt(Vue.version, '2.1.8')
1719
? 'beforeCreate'
1820
: 'beforeMount'
21+
22+
export const CREATE_ELEMENT_ALIAS = semver.gt(Vue.version, '2.1.5')
23+
? '_c'
24+
: '_h'

Diff for: packages/shared/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
"name": "shared",
33
"version": "1.0.0-beta.27",
44
"private": true,
5+
"dependencies": {
6+
"semver": "^5.6.0"
7+
},
58
"peerDependencies": {
69
"vue": "2.x",
710
"vue-template-compiler": "^2.x"

Diff for: packages/shared/util.js

+24-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @flow
22
import Vue from 'vue'
3+
import semver from 'semver'
34

45
export function throwError (msg: string): void {
56
throw new Error(`[vue-test-utils]: ${msg}`)
@@ -31,10 +32,6 @@ const hyphenateRE = /\B([A-Z])/g
3132
export const hyphenate = (str: string): string =>
3233
str.replace(hyphenateRE, '-$1').toLowerCase()
3334

34-
export const vueVersion = Number(
35-
`${Vue.version.split('.')[0]}.${Vue.version.split('.')[1]}`
36-
)
37-
3835
function hasOwnProperty (obj, prop) {
3936
return Object.prototype.hasOwnProperty.call(obj, prop)
4037
}
@@ -59,16 +56,28 @@ export function resolveComponent (id: string, components: Object) {
5956
return components[id] || components[camelizedId] || components[PascalCaseId]
6057
}
6158

62-
export function semVerGreaterThan (a: string, b: string) {
63-
const pa = a.split('.')
64-
const pb = b.split('.')
65-
for (let i = 0; i < 3; i++) {
66-
var na = Number(pa[i])
67-
var nb = Number(pb[i])
68-
if (na > nb) return true
69-
if (nb > na) return false
70-
if (!isNaN(na) && isNaN(nb)) return true
71-
if (isNaN(na) && !isNaN(nb)) return false
59+
const UA = typeof window !== 'undefined' &&
60+
'navigator' in window &&
61+
navigator.userAgent.toLowerCase()
62+
63+
export const isPhantomJS = UA && UA.includes &&
64+
UA.match(/phantomjs/i)
65+
66+
export const isEdge = UA && UA.indexOf('edge/') > 0
67+
export const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge
68+
69+
// get the event used to trigger v-model handler that updates bound data
70+
export function getCheckedEvent () {
71+
const version = Vue.version
72+
73+
if (semver.satisfies(version, '2.1.9 - 2.1.10')) {
74+
return 'click'
75+
}
76+
77+
if (semver.satisfies(version, '2.2 - 2.4')) {
78+
return isChrome ? 'click' : 'change'
7279
}
73-
return false
80+
81+
// change is handler for version 2.0 - 2.1.8, and 2.5+
82+
return 'change'
7483
}

Diff for: packages/test-utils/src/find.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import findDOMNodes from './find-dom-nodes'
44
import {
55
DOM_SELECTOR,
66
REF_SELECTOR,
7-
COMPONENT_SELECTOR
7+
COMPONENT_SELECTOR,
8+
VUE_VERSION
89
} from 'shared/consts'
9-
import { throwError, vueVersion } from 'shared/util'
10+
import { throwError } from 'shared/util'
1011
import { matches } from './matches'
1112

1213
export function findAllInstances (rootVm: any) {
@@ -74,7 +75,7 @@ export default function find (
7475
(selector.value.options &&
7576
selector.value.options.functional)
7677
) &&
77-
vueVersion < 2.3
78+
VUE_VERSION < 2.3
7879
) {
7980
throwError(
8081
`find for functional components is not supported ` +

0 commit comments

Comments
 (0)