Skip to content

Commit 07a203b

Browse files
chore: code review and type improvements
1 parent bf9cde8 commit 07a203b

File tree

6 files changed

+35
-30
lines changed

6 files changed

+35
-30
lines changed

src/constants.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export const MOUNT_ELEMENT_ID = 'app'
2+
export const MOUNT_COMPONENT_REF = 'VTU_COMPONENT'
3+
export const MOUNT_PARENT_NAME = 'VTU_ROOT'

src/emitMixin.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { getCurrentInstance, App } from 'vue'
1+
import { getCurrentInstance } from 'vue'
22

3-
export const attachEventListener = (vm: App) => {
4-
const emitMixin = {
3+
export const attachEmitListener = () => {
4+
return {
55
beforeCreate() {
66
let events: Record<string, unknown[]> = {}
77
this.__emitted = events
@@ -15,6 +15,4 @@ export const attachEventListener = (vm: App) => {
1515
}
1616
}
1717
}
18-
19-
vm.mixin(emitMixin)
2018
}

src/mount.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,13 @@ import {
1414
} from 'vue'
1515

1616
import { createWrapper, VueWrapper } from './vue-wrapper'
17-
import { attachEventListener } from './emitMixin'
17+
import { attachEmitListener } from './emitMixin'
1818
import { createDataMixin } from './dataMixin'
19-
import { MOUNT_ELEMENT_ID } from './constants'
19+
import {
20+
MOUNT_COMPONENT_REF,
21+
MOUNT_ELEMENT_ID,
22+
MOUNT_PARENT_NAME
23+
} from './constants'
2024
import { stubComponents } from './stubs'
2125

2226
type Slot = VNode | string | { render: Function }
@@ -82,11 +86,11 @@ export function mount<T extends ComponentPublicInstance>(
8286

8387
// we define props as reactive so that way when we update them with `setProps`
8488
// Vue's reactivity system will cause a rerender.
85-
const props = reactive({ ...options?.props, ref: 'VTU_COMPONENT' })
89+
const props = reactive({ ...options?.props, ref: MOUNT_COMPONENT_REF })
8690

8791
// create the wrapper component
8892
const Parent = defineComponent({
89-
name: 'VTU_COMPONENT',
93+
name: MOUNT_PARENT_NAME,
9094
render() {
9195
return h(component, props, slots)
9296
}
@@ -145,7 +149,7 @@ export function mount<T extends ComponentPublicInstance>(
145149
}
146150

147151
// add tracking for emitted events
148-
attachEventListener(vm)
152+
vm.mixin(attachEmitListener())
149153

150154
// stubs
151155
if (options?.global?.stubs) {
@@ -156,6 +160,6 @@ export function mount<T extends ComponentPublicInstance>(
156160

157161
// mount the app!
158162
const app = vm.mount(el)
159-
const App = app.$refs['VTU_COMPONENT'] as ComponentPublicInstance
163+
const App = app.$refs[MOUNT_COMPONENT_REF] as T
160164
return createWrapper<T>(App, setProps)
161165
}

src/vue-wrapper.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,18 +88,16 @@ export class VueWrapper<T extends ComponentPublicInstance>
8888
return result
8989
}
9090

91-
findComponent(selector: FindComponentSelector): VueWrapper | ErrorWrapper {
91+
findComponent(selector: FindComponentSelector): VueWrapper<T> | ErrorWrapper {
9292
if (typeof selector === 'object' && 'ref' in selector) {
93-
return createWrapper(
94-
this.vm.$refs[selector.ref] as ComponentPublicInstance
95-
)
93+
return createWrapper(this.vm.$refs[selector.ref] as T)
9694
}
9795
const result = find(this.vm.$.subTree, selector)
9896
if (!result.length) return new ErrorWrapper({ selector })
9997
return createWrapper(result[0])
10098
}
10199

102-
findAllComponents(selector: FindAllComponentsSelector): VueWrapper[] {
100+
findAllComponents(selector: FindAllComponentsSelector): VueWrapper<T>[] {
103101
return find(this.vm.$.subTree, selector).map((c) => createWrapper(c))
104102
}
105103

tests/findAllComponents.spec.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { mount } from '../src'
22
import Hello from './components/Hello.vue'
3+
import { defineComponent } from 'vue'
34

4-
const compC = {
5+
const compC = defineComponent({
56
name: 'ComponentC',
67
template: '<div class="C">C</div>'
7-
}
8-
const compB = {
8+
})
9+
const compB = defineComponent({
910
template: '<div class="B">TextBefore<comp-c/>TextAfter<comp-c/></div>',
1011
components: { compC }
11-
}
12-
const compA = {
12+
})
13+
const compA = defineComponent({
1314
template: '<div class="A"><comp-b ref="b"/><hello ref="b"/></div>',
1415
components: { compB, Hello }
15-
}
16+
})
1617

1718
describe('findAllComponents', () => {
1819
it('finds all deeply nested vue components', () => {

tests/findComponent.spec.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
import { defineComponent } from 'vue'
12
import { mount } from '../src'
23
import Hello from './components/Hello.vue'
34

4-
const compC = {
5+
const compC = defineComponent({
56
name: 'ComponentC',
67
template: '<div class="C">C</div>'
7-
}
8+
})
89

9-
const compD = {
10+
const compD = defineComponent({
1011
name: 'ComponentD',
1112
template: '<comp-c class="c-as-root-on-d" />',
1213
components: { compC }
13-
}
14+
})
1415

15-
const compB = {
16+
const compB = defineComponent({
1617
name: 'component-b',
1718
template: `
1819
<div class="B">
@@ -23,16 +24,17 @@ const compB = {
2324
<comp-d id="compD" />
2425
</div>`,
2526
components: { compC, compD }
26-
}
27-
const compA = {
27+
})
28+
29+
const compA = defineComponent({
2830
template: `
2931
<div class="A">
3032
<comp-b />
3133
<hello ref="b" />
3234
<div class="domElement" />
3335
</div>`,
3436
components: { compB, Hello }
35-
}
37+
})
3638

3739
describe('findComponent', () => {
3840
it('does not find plain dom elements', () => {

0 commit comments

Comments
 (0)