Skip to content

Commit 9d3c2a6

Browse files
nandi95lmiller1990
authored andcommitted
chore: PR fixes
1 parent 9d5e3d2 commit 9d3c2a6

File tree

13 files changed

+93
-86
lines changed

13 files changed

+93
-86
lines changed

src/createDomEvent.ts

Lines changed: 45 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
1-
// @ts-ignore No DefinitelyTyped package exists
1+
// @ts-ignore todo - No DefinitelyTyped package exists for this
22
import eventTypes from 'dom-event-types'
3+
import { Key } from 'readline'
4+
5+
const keyCodesByKeyName = {
6+
backspace: 8,
7+
tab: 9,
8+
enter: 13,
9+
esc: 27,
10+
space: 32,
11+
pageup: 33,
12+
pagedown: 34,
13+
end: 35,
14+
home: 36,
15+
left: 37,
16+
up: 38,
17+
right: 39,
18+
down: 40,
19+
insert: 45,
20+
delete: 46
21+
} as const
22+
23+
// modifiers to keep an eye on
24+
const ignorableKeyModifiers = ['stop', 'prevent', 'self', 'exact']
25+
const systemKeyModifiers = ['ctrl', 'shift', 'alt', 'meta'] as const
26+
const mouseKeyModifiers = ['left', 'middle', 'right'] as const
27+
28+
type KeyName = keyof typeof keyCodesByKeyName
29+
type Modifier =
30+
| typeof systemKeyModifiers[number]
31+
| typeof mouseKeyModifiers[number]
332

433
interface TriggerOptions {
534
code?: String
@@ -10,24 +39,19 @@ interface TriggerOptions {
1039

1140
interface EventParams {
1241
eventType: string
13-
modifiers: KeyNameArray
42+
modifiers: KeyName[]
1443
options?: TriggerOptions
1544
}
1645

17-
// modifiers to keep an eye on
18-
const ignorableKeyModifiers = ['stop', 'prevent', 'self', 'exact']
19-
const systemKeyModifiers = ['ctrl', 'shift', 'alt', 'meta']
20-
const mouseKeyModifiers = ['left', 'middle', 'right']
21-
2246
/**
2347
* Groups modifiers into lists
2448
*/
25-
function generateModifiers(modifiers: KeyNameArray, isOnClick: boolean) {
26-
const keyModifiers: KeyNameArray = []
27-
const systemModifiers: string[] = []
49+
function generateModifiers(modifiers: KeyName[], isOnClick: boolean) {
50+
const keyModifiers: KeyName[] = []
51+
const systemModifiers: Modifier[] = []
2852

2953
for (let i = 0; i < modifiers.length; i++) {
30-
const modifier = modifiers[i]
54+
const modifier: KeyName | Modifier = modifiers[i]
3155

3256
// addEventListener() options, e.g. .passive & .capture, that we dont need to handle
3357
if (ignorableKeyModifiers.includes(modifier)) {
@@ -36,10 +60,15 @@ function generateModifiers(modifiers: KeyNameArray, isOnClick: boolean) {
3660
// modifiers that require special conversion
3761
// if passed a left/right key modifier with onClick, add it here as well.
3862
if (
39-
systemKeyModifiers.includes(modifier) ||
40-
(mouseKeyModifiers.includes(modifier) && isOnClick)
63+
systemKeyModifiers.includes(
64+
modifier as Exclude<typeof modifier, KeyName>
65+
) ||
66+
(mouseKeyModifiers.includes(
67+
modifier as Exclude<typeof modifier, KeyName>
68+
) &&
69+
isOnClick)
4170
) {
42-
systemModifiers.push(modifier)
71+
systemModifiers.push(modifier as Modifier)
4372
} else {
4473
keyModifiers.push(modifier)
4574
}
@@ -51,25 +80,6 @@ function generateModifiers(modifiers: KeyNameArray, isOnClick: boolean) {
5180
}
5281
}
5382

54-
export type KeyNameArray = Array<keyof typeof keyCodesByKeyName>
55-
export const keyCodesByKeyName = {
56-
backspace: 8,
57-
tab: 9,
58-
enter: 13,
59-
esc: 27,
60-
space: 32,
61-
pageup: 33,
62-
pagedown: 34,
63-
end: 35,
64-
home: 36,
65-
left: 37,
66-
up: 38,
67-
right: 39,
68-
down: 40,
69-
insert: 45,
70-
delete: 46
71-
}
72-
7383
function getEventProperties(eventParams: EventParams) {
7484
let { modifiers, options = {}, eventType } = eventParams
7585

@@ -155,7 +165,7 @@ function createDOMEvent(eventString: String, options?: TriggerOptions) {
155165

156166
const eventParams: EventParams = {
157167
eventType,
158-
modifiers: modifiers as KeyNameArray,
168+
modifiers: modifiers as KeyName[],
159169
options
160170
}
161171
const event: Event & TriggerOptions = createEvent(eventParams)
@@ -178,4 +188,4 @@ function createDOMEvent(eventString: String, options?: TriggerOptions) {
178188
return event
179189
}
180190

181-
export { TriggerOptions, createDOMEvent }
191+
export { TriggerOptions, createDOMEvent, keyCodesByKeyName, KeyName }

src/domWrapper.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ export class DOMWrapper<ElementType extends Element> {
157157
return
158158
}
159159

160+
// todo - review all non-null assertion operators in project
161+
// search globally for `!.` and with regex `!$`
160162
element.selected = true
161163
let parentElement = element.parentElement!
162164

src/emit.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
ComponentInternalInstance
66
} from 'vue'
77

8-
export type Events<T = unknown> = Record<number, Record<string, T[]>>
8+
type Events<T = unknown> = Record<number, Record<string, T[]>>
99

1010
const enum DevtoolsHooks {
1111
COMPONENT_EMIT = 'component:emit'
@@ -16,10 +16,10 @@ let events: Events
1616
export function emitted<T = unknown>(
1717
vm: ComponentPublicInstance,
1818
eventName?: string
19-
): undefined | Events<T>[number][string] | Events<T>[number] {
19+
): undefined | T[] | Record<string, T[]> {
2020
const cid = vm.$.uid
2121

22-
const vmEvents: Events<T>[number] = (events as Events<T>)[cid] || {}
22+
const vmEvents: Record<string, T[]> = (events as Events<T>)[cid] || {}
2323
if (eventName) {
2424
return vmEvents ? vmEvents[eventName] : undefined
2525
}

src/mount.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ export function mount(
268268
[name, slot]: [string, Slot]
269269
): { [key: string]: Function } => {
270270
// case of an SFC getting passed
271-
if (typeof slot === 'object' && 'render' in slot) {
272-
acc[name] = slot.render!
271+
if (typeof slot === 'object' && 'render' in slot && slot.render) {
272+
acc[name] = slot.render
273273
return acc
274274
}
275275

src/vueWrapper.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import { TriggerOptions } from './createDomEvent'
1313
import { find, matches } from './utils/find'
1414
import { mergeDeep, textContent } from './utils'
1515
import { emitted } from './emit'
16-
import type { Events } from './emit'
1716

1817
export class VueWrapper<T extends ComponentPublicInstance> {
1918
private componentVM: T
@@ -62,26 +61,28 @@ export class VueWrapper<T extends ComponentPublicInstance> {
6261
classes(): string[]
6362
classes(className: string): boolean
6463
classes(className?: string): string[] | boolean {
65-
return new DOMWrapper(this.element).classes(className!)
64+
return className
65+
? new DOMWrapper(this.element).classes(className)
66+
: new DOMWrapper(this.element).classes()
6667
}
6768

6869
attributes(): { [key: string]: string }
6970
attributes(key: string): string
7071
attributes(key?: string): { [key: string]: string } | string {
71-
return new DOMWrapper(this.element).attributes(key!)
72+
return key
73+
? new DOMWrapper(this.element).attributes(key)
74+
: new DOMWrapper(this.element).attributes()
7275
}
7376

7477
exists() {
7578
return true
7679
}
7780

78-
emitted<T = unknown>(): Events<T>[number]
81+
emitted<T = unknown>(): Record<string, T[]>
82+
emitted<T = unknown>(eventName?: string): undefined | T[]
7983
emitted<T = unknown>(
8084
eventName?: string
81-
): undefined | Events<T>[number][string]
82-
emitted<T = unknown>(
83-
eventName?: string
84-
): undefined | Events<T>[number][string] | Events<T>[number] {
85+
): undefined | T[] | Record<string, T[]> {
8586
return emitted(this.vm, eventName)
8687
}
8788

tests/isVisible.spec.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { mount } from '../src'
2+
import { defineComponent, ref } from 'vue'
23

34
describe('isVisible', () => {
45
const Comp = {
@@ -79,7 +80,7 @@ describe('isVisible', () => {
7980
})
8081

8182
it('handles transition-group', async () => {
82-
const Comp = {
83+
const Comp = defineComponent({
8384
template: `
8485
<div id="list-demo">
8586
<button @click="add" id="add">Add</button>
@@ -91,22 +92,13 @@ describe('isVisible', () => {
9192
</transition-group>
9293
</div>
9394
`,
94-
methods: {
95-
add() {
96-
// @ts-expect-error
97-
this.items.push(2)
98-
},
99-
remove() {
100-
// @ts-expect-error
101-
this.items.splice(1) // back to [1]
102-
}
103-
},
104-
data() {
105-
return {
106-
items: [1]
107-
}
95+
setup() {
96+
const items = ref([1])
97+
const add = () => items.value.push(2)
98+
const remove = () => items.value.splice(1)
99+
return { add, remove, items }
108100
}
109-
}
101+
})
110102
const wrapper = mount(Comp)
111103

112104
expect(wrapper.html()).toContain('Item: 1')

tests/mountingOptions/data.spec.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import { h } from 'vue'
1+
import { defineComponent, h } from 'vue'
22

33
import { mount } from '../../src'
44

55
describe('mounting options: data', () => {
66
it('merges data from mounting options with component', () => {
7-
const Comp = {
7+
const Comp = defineComponent({
88
data() {
99
return {
1010
foo: 'foo',
1111
bar: 'bar'
1212
}
1313
},
14-
render(): Function {
15-
// @ts-expect-error
14+
render() {
1615
return h('div', `Foo is ${this.foo} bar is ${this.bar}`)
1716
}
18-
}
17+
})
1918

2019
const wrapper = mount(Comp, {
2120
data() {

tests/mountingOptions/mocks.spec.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { mount, RouterLinkStub } from '../../src'
2+
import { defineComponent } from 'vue'
23

34
describe('mocks', () => {
45
it('mocks a vuex store', async () => {
@@ -29,30 +30,27 @@ describe('mocks', () => {
2930
})
3031

3132
it('mocks vue-router', async () => {
32-
const Foo = {
33+
const Foo = defineComponent({
3334
template: `
3435
<div>
3536
<RouterLink :to="url">Go to post: {{ id }}</RouterLink>
3637
<button @click="submit">Go</button>
3738
</div>
3839
`,
3940
computed: {
40-
url(): string {
41-
// @ts-expect-error
41+
url() {
4242
return `/posts/${this.$route.params.id}`
4343
},
44-
id(): number {
45-
// @ts-expect-error
44+
id() {
4645
return this.$route.params.id
4746
}
4847
},
4948
methods: {
5049
submit() {
51-
// @ts-expect-error
5250
this.$router.push(`/posts/${this.id}`)
5351
}
5452
}
55-
}
53+
})
5654

5755
const $router = {
5856
push: jest.fn()

tests/setData.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ describe('setData', () => {
2020
// See: https://github.com/vuejs/vue-test-utils/issues/1756
2121
// Making sure it does not regress here.
2222
it('triggers a watcher', async () => {
23-
const Comp = {
23+
const Comp = defineComponent({
2424
template: `<div />`,
2525
data() {
2626
return {
@@ -34,12 +34,11 @@ describe('setData', () => {
3434
myObject: {
3535
immediate: true,
3636
handler() {
37-
// @ts-expect-error
3837
this.watchCounter += 1
3938
}
4039
}
4140
}
42-
}
41+
})
4342

4443
const initial = 'value'
4544
const expected = 'something else'

tests/setProps.spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ describe('setProps', () => {
5656
})
5757

5858
it('triggers a watcher', async () => {
59-
const Foo = {
59+
const Foo = defineComponent({
6060
props: ['foo'],
6161
data() {
6262
return {
@@ -65,13 +65,12 @@ describe('setProps', () => {
6565
},
6666
watch: {
6767
foo(val: string) {
68-
// @ts-expect-error
6968
this.bar = val
7069
}
7170
},
7271
template: `
7372
<div>{{ bar }}</div>`
74-
}
73+
})
7574
const wrapper = mount(Foo)
7675
expect(wrapper.html()).toContain('original-bar')
7776

0 commit comments

Comments
 (0)