Skip to content

Commit

Permalink
refactor(types): improve types (#785)
Browse files Browse the repository at this point in the history
  • Loading branch information
38elements authored and eddyerburgh committed Jun 30, 2018
1 parent eb7e854 commit cc3686f
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 31 deletions.
5 changes: 4 additions & 1 deletion packages/create-instance/add-mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@
import $$Vue from 'vue'
import { warn } from 'shared/util'

export default function addMocks (mockedProperties: Object, Vue: Component) {
export default function addMocks (
mockedProperties: Object,
Vue: Component
): void {
Object.keys(mockedProperties).forEach(key => {
try {
Vue.prototype[key] = mockedProperties[key]
Expand Down
4 changes: 2 additions & 2 deletions packages/create-instance/add-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import { compileToFunctions } from 'vue-template-compiler'

function startsWithTag (str) {
return str && str.trim()[0] === '<'
function startsWithTag (str: SlotValue): boolean {
return typeof str === 'string' && str.trim()[0] === '<'
}

function createVNodesForSlot (
Expand Down
2 changes: 1 addition & 1 deletion packages/create-instance/create-functional-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { createSlotVNodes } from './add-slots'
export default function createFunctionalComponent (
component: Component,
mountingOptions: Options
) {
): Component {
if (mountingOptions.context && typeof mountingOptions.context !== 'object') {
throwError('mount.context must be an object')
}
Expand Down
6 changes: 5 additions & 1 deletion packages/create-instance/extract-instance-options.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @flow

const MOUNTING_OPTIONS = [
'attachToDocument',
'mocks',
Expand All @@ -11,7 +13,9 @@ const MOUNTING_OPTIONS = [
'propsData'
]

export default function extractInstanceOptions (options) {
export default function extractInstanceOptions (
options: Object
): Object {
const instanceOptions = { ...options }
MOUNTING_OPTIONS.forEach(mountingOption => {
delete instanceOptions[mountingOption]
Expand Down
4 changes: 2 additions & 2 deletions packages/create-instance/log-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function logEvents (
vm: Component,
emitted: Object,
emittedByOrder: Array<any>
) {
): void {
const emit = vm.$emit
vm.$emit = (name, ...args) => {
(emitted[name] || (emitted[name] = [])).push(args)
Expand All @@ -13,7 +13,7 @@ export function logEvents (
}
}

export function addEventLogger (vue: Component) {
export function addEventLogger (vue: Component): void {
vue.mixin({
beforeCreate: function () {
this.__emitted = Object.create(null)
Expand Down
2 changes: 1 addition & 1 deletion packages/create-instance/validate-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function isValidSlot (slot: any): boolean {
)
}

function requiresTemplateCompiler (slot) {
function requiresTemplateCompiler (slot: any): void {
if (typeof slot === 'string' && !compileToFunctions) {
throwError(
`vueTemplateCompiler is undefined, you must pass ` +
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/compile-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { compileToFunctions } from 'vue-template-compiler'

export function compileTemplate (component: Component) {
export function compileTemplate (component: Component): void {
if (component.template) {
Object.assign(component, compileToFunctions(component.template))
}
Expand Down
10 changes: 5 additions & 5 deletions packages/shared/util.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// @flow
import Vue from 'vue'

export function throwError (msg: string) {
export function throwError (msg: string): void {
throw new Error(`[vue-test-utils]: ${msg}`)
}

export function warn (msg: string) {
export function warn (msg: string): void {
console.error(`[vue-test-utils]: ${msg}`)
}

const camelizeRE = /-(\w)/g
export const camelize = (str: string) => {
export const camelize = (str: string): string => {
const camelizedStr = str.replace(
camelizeRE,
(_, c) => (c ? c.toUpperCase() : '')
Expand All @@ -21,14 +21,14 @@ export const camelize = (str: string) => {
/**
* Capitalize a string.
*/
export const capitalize = (str: string) =>
export const capitalize = (str: string): string =>
str.charAt(0).toUpperCase() + str.slice(1)

/**
* Hyphenate a camelCase string.
*/
const hyphenateRE = /\B([A-Z])/g
export const hyphenate = (str: string) =>
export const hyphenate = (str: string): string =>
str.replace(hyphenateRE, '-$1').toLowerCase()

export const vueVersion = Number(
Expand Down
15 changes: 9 additions & 6 deletions packages/shared/validators.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import { throwError, capitalize, camelize, hyphenate } from './util'

export function isDomSelector (selector: any) {
export function isDomSelector (selector: any): boolean {
if (typeof selector !== 'string') {
return false
}
Expand All @@ -28,7 +28,7 @@ export function isDomSelector (selector: any) {
}
}

export function isVueComponent (component: any) {
export function isVueComponent (component: any): boolean {
if (typeof component === 'function' && component.options) {
return true
}
Expand All @@ -44,7 +44,7 @@ export function isVueComponent (component: any) {
return typeof component.render === 'function'
}

export function componentNeedsCompiling (component: Component) {
export function componentNeedsCompiling (component: Component): boolean {
return (
component &&
!component.render &&
Expand All @@ -53,7 +53,7 @@ export function componentNeedsCompiling (component: Component) {
)
}

export function isRefSelector (refOptionsObject: any) {
export function isRefSelector (refOptionsObject: any): boolean {
if (
typeof refOptionsObject !== 'object' ||
Object.keys(refOptionsObject || {}).length !== 1
Expand All @@ -64,15 +64,18 @@ export function isRefSelector (refOptionsObject: any) {
return typeof refOptionsObject.ref === 'string'
}

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

return !!nameOptionsObject.name
}

export function templateContainsComponent (template: string, name: string) {
export function templateContainsComponent (
template: string,
name: string
): boolean {
return [capitalize, camelize, hyphenate].some(format => {
const re = new RegExp(`<${format(name)}\\s*(\\s|>|(\/>))`, 'g')
return re.test(template)
Expand Down
5 changes: 4 additions & 1 deletion packages/test-utils/src/add-scoped-slots.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ function getVueTemplateCompilerHelpers (proxy: Object): Object {
return helpers
}

export function addScopedSlots (vm: Component, scopedSlots: any) {
export function addScopedSlots (
vm: Component,
scopedSlots: { [name: string]: string }
): void {
if (window.navigator.userAgent.match(/PhantomJS/i)) {
throwError(
`the scopedSlots option does not support PhantomJS. ` +
Expand Down
7 changes: 6 additions & 1 deletion packages/test-utils/src/error-handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export default function errorHandler (errorOrString, vm) {
// @flow

export default function errorHandler (
errorOrString: any,
vm: Component
): void {
const error =
typeof errorOrString === 'object'
? errorOrString
Expand Down
7 changes: 5 additions & 2 deletions packages/test-utils/src/find-vue-components.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export function vmCtorMatchesName (vm: Component, name: string): boolean {
))
}

export function vmCtorMatchesSelector (component: Component, selector: Object) {
export function vmCtorMatchesSelector (
component: Component,
selector: Object
): boolean {
const Ctor = selector._Ctor || (selector.options && selector.options._Ctor)
if (!Ctor) {
return false
Expand All @@ -70,7 +73,7 @@ export function vmCtorMatchesSelector (component: Component, selector: Object) {
export function vmFunctionalCtorMatchesSelector (
component: VNode,
Ctor: Object
) {
): boolean {
if (VUE_VERSION < 2.3) {
throwError(
`find for functional components is not support in ` + `Vue < 2.3`
Expand Down
8 changes: 5 additions & 3 deletions packages/test-utils/src/order-watchers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @flow

let i = 0

function orderDeps (watcher) {
function orderDeps (watcher): void {
watcher.deps.forEach(dep => {
if (dep._sortedId === i) {
return
Expand All @@ -11,7 +13,7 @@ function orderDeps (watcher) {
})
}

function orderVmWatchers (vm) {
function orderVmWatchers (vm: Component): void {
if (vm._watchers) {
vm._watchers.forEach(orderDeps)
}
Expand All @@ -27,7 +29,7 @@ function orderVmWatchers (vm) {
vm.$children.forEach(orderVmWatchers)
}

export function orderWatchers (vm) {
export function orderWatchers (vm: Component): void {
orderVmWatchers(vm)
i++
}
8 changes: 5 additions & 3 deletions packages/test-utils/src/set-watchers-to-sync.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// @flow

import { VUE_VERSION } from './consts'

function setDepsSync (dep) {
function setDepsSync (dep): void {
dep.subs.forEach(setWatcherSync)
}

function setWatcherSync (watcher) {
function setWatcherSync (watcher): void {
if (watcher.sync === true) {
return
}
watcher.sync = true
watcher.deps.forEach(setDepsSync)
}

export function setWatchersToSync (vm) {
export function setWatchersToSync (vm: Component): void {
if (vm._watchers) {
vm._watchers.forEach(setWatcherSync)
}
Expand Down
4 changes: 3 additions & 1 deletion packages/test-utils/src/warn-if-no-window.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @flow

import { throwError } from 'shared/util'

export default function warnIfNoWindow () {
export default function warnIfNoWindow (): void {
if (typeof window === 'undefined') {
throwError(
`window is undefined, vue-test-utils needs to be ` +
Expand Down

0 comments on commit cc3686f

Please sign in to comment.