Skip to content

Commit

Permalink
Started resolving typing issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nandi95 authored and lmiller1990 committed Feb 12, 2021
1 parent dd051cd commit b08cb49
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 29 deletions.
6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ interface Plugin<Instance, O> {
class Pluggable<Instance = DOMWrapper<Element>> {
installedPlugins: Plugin<Instance, any>[] = []

install<O>(handler: (instance: Instance) => Record<string, any>)
install<O>(handler: (instance: Instance) => Record<string, any>): void
install<O>(
handler: (instance: Instance, options: O) => Record<string, any>,
options: O
)
): void
install<O>(
handler: (instance: Instance, options?: O) => Record<string, any>,
options?: O
) {
): void {
if (typeof handler !== 'function') {
console.error('plugin.install must receive a function')
handler = () => ({})
Expand Down
27 changes: 18 additions & 9 deletions src/createDomEvent.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-expect-error No DefinitelyTyped package exists
import eventTypes from 'dom-event-types'

interface TriggerOptions {
Expand All @@ -9,7 +10,7 @@ interface TriggerOptions {

interface EventParams {
eventType: string
modifiers: string[]
modifiers: KeyNameArray
options?: TriggerOptions
}

Expand All @@ -21,8 +22,8 @@ const mouseKeyModifiers = ['left', 'middle', 'right']
/**
* Groups modifiers into lists
*/
function generateModifiers(modifiers: string[], isOnClick: boolean) {
const keyModifiers: string[] = []
function generateModifiers(modifiers: KeyNameArray, isOnClick: boolean) {
const keyModifiers: KeyNameArray = []
const systemModifiers: string[] = []

for (let i = 0; i < modifiers.length; i++) {
Expand Down Expand Up @@ -50,6 +51,7 @@ function generateModifiers(modifiers: string[], isOnClick: boolean) {
}
}

export type KeyNameArray = Array<keyof typeof keyCodesByKeyName>
export const keyCodesByKeyName = {
backspace: 8,
tab: 9,
Expand Down Expand Up @@ -98,10 +100,13 @@ function getEventProperties(eventParams: EventParams) {

// convert `shift, ctrl` to `shiftKey, ctrlKey`
// allows trigger('keydown.shift.ctrl.n') directly
const systemModifiersMeta = systemModifiers.reduce((all, key) => {
all[`${key}Key`] = true
return all
}, {})
const systemModifiersMeta = systemModifiers.reduce(
(all: Record<string, boolean>, key) => {
all[`${key}Key`] = true
return all
},
{}
)

// get the keyCode for backwards compat
const keyCode =
Expand Down Expand Up @@ -148,8 +153,12 @@ function createDOMEvent(eventString: String, options?: TriggerOptions) {
// split eventString like `keydown.ctrl.shift.c` into `keydown` and array of modifiers
const [eventType, ...modifiers] = eventString.split('.')

const eventParams: EventParams = { eventType, modifiers, options }
const event: Event = createEvent(eventParams)
const eventParams: EventParams = {
eventType,
modifiers: modifiers as KeyNameArray,
options
}
const event: Event & TriggerOptions = createEvent(eventParams)
const eventPrototype = Object.getPrototypeOf(event)

// attach custom options to the event, like `relatedTarget` and so on.
Expand Down
4 changes: 2 additions & 2 deletions src/domWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ export class DOMWrapper<ElementType extends Element> {
}

element.selected = true
let parentElement = element.parentElement
let parentElement = element.parentElement!

if (parentElement.tagName === 'OPTGROUP') {
parentElement = parentElement.parentElement
parentElement = parentElement.parentElement!
}

return new DOMWrapper(parentElement).trigger('change')
Expand Down
2 changes: 1 addition & 1 deletion src/stubs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { matchName } from './utils/matchName'
import { ComponentInternalInstance } from '@vue/runtime-core'

interface StubOptions {
name?: string
name: string
props?: any
propsDeclaration?: any
}
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,5 @@ export type GlobalMountOptions = {
*/
renderStubDefaultSlot?: boolean
}

export type VueElement = Element & { __vue_app__?: any }
6 changes: 3 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ export const mergeDeep = (
target: Record<string, any>,
source: Record<string, any>
) => {
const isObject = (obj: unknown): obj is Object =>
obj && typeof obj === 'object'
const isObject = (obj: unknown): obj is Record<string, any> =>
!!obj && typeof obj === 'object'

if (!isObject(target) || !isObject(source)) {
return source
Expand Down Expand Up @@ -101,6 +101,6 @@ export function textContent(element: Element): string {
// we check if the element is a comment first
// to return an empty string in that case, instead of the comment content
return element.nodeType !== Node.COMMENT_NODE
? element.textContent?.trim()
? element.textContent?.trim() ?? ''
: ''
}
7 changes: 4 additions & 3 deletions src/utils/compileSlots.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { compile } from '@vue/compiler-dom'
import * as vue from 'vue'
import type { SetupContext } from 'vue'

export function processSlot(source = '', Vue = vue) {
let template = source.trim()
Expand All @@ -25,14 +26,14 @@ export function processSlot(source = '', Vue = vue) {
components: {
SlotWrapper: {
inheritAttrs: false,
setup(_, { slots, attrs }) {
setup(_: Record<string, any>, ctx: SetupContext) {
return () => {
const names = Object.keys(slots)
const names = Object.keys(ctx.slots)
if (names.length === 0) {
return []
} else {
const slotName = names[0]
return slots[slotName](attrs)
return ctx.slots[slotName]!(ctx.attrs)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/utils/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export function matches(
return true
}

let componentName
let componentName: string | undefined
if ('name' in nodeType || 'displayName' in nodeType) {
// match normal component definitions or functional components
componentName = nodeType.name || nodeType.displayName
Expand Down
2 changes: 1 addition & 1 deletion src/utils/isElementVisible.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function isAttributeVisible<T extends Element>(element: T) {
)
}

export function isElementVisible<T extends Element>(element: T) {
export function isElementVisible<T extends Element>(element: T): boolean {
return (
element.nodeName !== '#comment' &&
isStyleVisible(element) &&
Expand Down
4 changes: 2 additions & 2 deletions src/utils/matchName.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { camelize, capitalize } from './vueShared'

export function matchName(target: string, sourceName: string) {
export function matchName(target: string, sourceName: string): boolean {
const camelized = camelize(target)
const capitalized = capitalize(camelized)

return (
sourceName &&
!!sourceName &&
(sourceName === target ||
sourceName === camelized ||
sourceName === capitalized ||
Expand Down
12 changes: 8 additions & 4 deletions src/vueWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ import { ShapeFlags } from '@vue/shared'

import { config } from './config'
import { DOMWrapper } from './domWrapper'
import { FindAllComponentsSelector, FindComponentSelector } from './types'
import {
FindAllComponentsSelector,
FindComponentSelector,
VueElement
} from './types'
import { createWrapperError } from './errorWrapper'
import { TriggerOptions } from './createDomEvent'
import { find, matches } from './utils/find'
Expand All @@ -12,7 +16,7 @@ import { emitted } from './emit'

export class VueWrapper<T extends ComponentPublicInstance> {
private componentVM: T
private rootVM: ComponentPublicInstance
private rootVM: ComponentPublicInstance | null
private __app: App | null
private __setProps: ((props: Record<string, any>) => void) | undefined

Expand All @@ -34,7 +38,7 @@ export class VueWrapper<T extends ComponentPublicInstance> {
return this.vm.$.subTree.shapeFlag === ShapeFlags.ARRAY_CHILDREN
}

private get parentElement(): Element {
private get parentElement(): VueElement {
return this.vm.$el.parentElement
}

Expand Down Expand Up @@ -148,7 +152,7 @@ export class VueWrapper<T extends ComponentPublicInstance> {
// eg: mount(Comp).findComponent(Comp)
// this is the same as doing `wrapper.vm`, but we keep this behavior for back compat.
if (matches(this.vm.$.vnode, selector)) {
return createWrapper(null, this.vm.$.vnode.component.proxy)
return createWrapper(null, this.vm.$.vnode.component?.proxy!)
}

return createWrapperError('VueWrapper')
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"outDir": "dist",
"module": "esnext",
"lib": ["DOM", "ES2015", "ES2017"],
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true,
"experimentalDecorators": true,
Expand Down

0 comments on commit b08cb49

Please sign in to comment.