Skip to content

Commit c07751f

Browse files
committed
refactor: adjust createApp related API signatures
BREAKING CHANGE: `createApp` API has been adjusted. - `createApp()` now accepts the root component, and optionally a props object to pass to the root component. - `app.mount()` now accepts a single argument (the root container) - `app.unmount()` no longer requires arguments. New behavior looks like the following: ``` js const app = createApp(RootComponent) app.mount('#app') app.unmount() ```
1 parent eacd390 commit c07751f

25 files changed

+276
-326
lines changed

packages/runtime-core/__tests__/apiApp.spec.ts renamed to packages/runtime-core/__tests__/apiCreateApp.spec.ts

+95-120
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ describe('api: createApp', () => {
3030
}
3131

3232
const root1 = nodeOps.createElement('div')
33-
createApp().mount(Comp, root1)
33+
createApp(Comp).mount(root1)
3434
expect(serializeInner(root1)).toBe(`0`)
3535

3636
// mount with props
3737
const root2 = nodeOps.createElement('div')
38-
const app2 = createApp()
39-
app2.mount(Comp, root2, { count: 1 })
38+
const app2 = createApp(Comp, { count: 1 })
39+
app2.mount(root2)
4040
expect(serializeInner(root2)).toBe(`1`)
4141

4242
// remount warning
4343
const root3 = nodeOps.createElement('div')
44-
app2.mount(Comp, root3)
44+
app2.mount(root3)
4545
expect(serializeInner(root3)).toBe(``)
4646
expect(`already been mounted`).toHaveBeenWarned()
4747
})
@@ -59,18 +59,14 @@ describe('api: createApp', () => {
5959
}
6060

6161
const root = nodeOps.createElement('div')
62-
const app = createApp()
63-
app.mount(Comp, root)
62+
const app = createApp(Comp)
63+
app.mount(root)
6464

6565
app.unmount(root)
6666
expect(serializeInner(root)).toBe(``)
6767
})
6868

6969
test('provide', () => {
70-
const app = createApp()
71-
app.provide('foo', 1)
72-
app.provide('bar', 2)
73-
7470
const Root = {
7571
setup() {
7672
// test override
@@ -87,25 +83,16 @@ describe('api: createApp', () => {
8783
}
8884
}
8985

86+
const app = createApp(Root)
87+
app.provide('foo', 1)
88+
app.provide('bar', 2)
89+
9090
const root = nodeOps.createElement('div')
91-
app.mount(Root, root)
91+
app.mount(root)
9292
expect(serializeInner(root)).toBe(`3,2`)
9393
})
9494

9595
test('component', () => {
96-
const app = createApp()
97-
98-
const FooBar = () => 'foobar!'
99-
app.component('FooBar', FooBar)
100-
expect(app.component('FooBar')).toBe(FooBar)
101-
102-
app.component('BarBaz', () => 'barbaz!')
103-
104-
app.component('BarBaz', () => 'barbaz!')
105-
expect(
106-
'Component "BarBaz" has already been registered in target app.'
107-
).toHaveBeenWarnedTimes(1)
108-
10996
const Root = {
11097
// local override
11198
components: {
@@ -122,33 +109,29 @@ describe('api: createApp', () => {
122109
}
123110
}
124111

112+
const app = createApp(Root)
113+
114+
const FooBar = () => 'foobar!'
115+
app.component('FooBar', FooBar)
116+
expect(app.component('FooBar')).toBe(FooBar)
117+
118+
app.component('BarBaz', () => 'barbaz!')
119+
120+
app.component('BarBaz', () => 'barbaz!')
121+
expect(
122+
'Component "BarBaz" has already been registered in target app.'
123+
).toHaveBeenWarnedTimes(1)
124+
125125
const root = nodeOps.createElement('div')
126-
app.mount(Root, root)
126+
app.mount(root)
127127
expect(serializeInner(root)).toBe(`<div>foobar!barbaz-local!</div>`)
128128
})
129129

130130
test('directive', () => {
131-
const app = createApp()
132-
133131
const spy1 = jest.fn()
134132
const spy2 = jest.fn()
135133
const spy3 = jest.fn()
136134

137-
const FooBar = { mounted: spy1 }
138-
app.directive('FooBar', FooBar)
139-
expect(app.directive('FooBar')).toBe(FooBar)
140-
141-
app.directive('BarBaz', {
142-
mounted: spy2
143-
})
144-
145-
app.directive('BarBaz', {
146-
mounted: spy2
147-
})
148-
expect(
149-
'Directive "BarBaz" has already been registered in target app.'
150-
).toHaveBeenWarnedTimes(1)
151-
152135
const Root = {
153136
// local override
154137
directives: {
@@ -165,8 +148,25 @@ describe('api: createApp', () => {
165148
}
166149
}
167150

151+
const app = createApp(Root)
152+
153+
const FooBar = { mounted: spy1 }
154+
app.directive('FooBar', FooBar)
155+
expect(app.directive('FooBar')).toBe(FooBar)
156+
157+
app.directive('BarBaz', {
158+
mounted: spy2
159+
})
160+
161+
app.directive('BarBaz', {
162+
mounted: spy2
163+
})
164+
expect(
165+
'Directive "BarBaz" has already been registered in target app.'
166+
).toHaveBeenWarnedTimes(1)
167+
168168
const root = nodeOps.createElement('div')
169-
app.mount(Root, root)
169+
app.mount(root)
170170
expect(spy1).toHaveBeenCalled()
171171
expect(spy2).not.toHaveBeenCalled()
172172
expect(spy3).toHaveBeenCalled()
@@ -232,7 +232,7 @@ describe('api: createApp', () => {
232232
}
233233
}
234234

235-
const app = createApp()
235+
const app = createApp(Comp)
236236
app.mixin(mixinA)
237237
app.mixin(mixinB)
238238

@@ -246,7 +246,7 @@ describe('api: createApp', () => {
246246
).toHaveBeenWarnedTimes(1)
247247

248248
const root = nodeOps.createElement('div')
249-
app.mount(Comp, root)
249+
app.mount(root)
250250

251251
expect(serializeInner(root)).toBe(`123`)
252252
expect(calls).toEqual([
@@ -272,20 +272,21 @@ describe('api: createApp', () => {
272272
}
273273
const PluginD: any = undefined
274274

275-
const app = createApp()
276-
app.use(PluginA)
277-
app.use(PluginB, 1, 1)
278-
app.use(PluginC)
279-
280275
const Root = {
281276
setup() {
282277
const foo = inject('foo')
283278
const bar = inject('bar')
284279
return () => `${foo},${bar}`
285280
}
286281
}
282+
283+
const app = createApp(Root)
284+
app.use(PluginA)
285+
app.use(PluginB, 1, 1)
286+
app.use(PluginC)
287+
287288
const root = nodeOps.createElement('div')
288-
app.mount(Root, root)
289+
app.mount(root)
289290
expect(serializeInner(root)).toBe(`1,2`)
290291

291292
app.use(PluginA)
@@ -301,18 +302,14 @@ describe('api: createApp', () => {
301302
})
302303

303304
test('config.errorHandler', () => {
304-
const app = createApp()
305-
306305
const error = new Error()
307306
const count = ref(0)
308307

309-
const handler = (app.config.errorHandler = jest.fn(
310-
(err, instance, info) => {
311-
expect(err).toBe(error)
312-
expect((instance as any).count).toBe(count.value)
313-
expect(info).toBe(`render function`)
314-
}
315-
))
308+
const handler = jest.fn((err, instance, info) => {
309+
expect(err).toBe(error)
310+
expect((instance as any).count).toBe(count.value)
311+
expect(info).toBe(`render function`)
312+
})
316313

317314
const Root = {
318315
setup() {
@@ -326,21 +323,19 @@ describe('api: createApp', () => {
326323
}
327324
}
328325

329-
app.mount(Root, nodeOps.createElement('div'))
326+
const app = createApp(Root)
327+
app.config.errorHandler = handler
328+
app.mount(nodeOps.createElement('div'))
330329
expect(handler).toHaveBeenCalled()
331330
})
332331

333332
test('config.warnHandler', () => {
334-
const app = createApp()
335333
let ctx: any
336-
337-
const handler = (app.config.warnHandler = jest.fn(
338-
(msg, instance, trace) => {
339-
expect(msg).toMatch(`Component is missing template or render function`)
340-
expect(instance).toBe(ctx.proxy)
341-
expect(trace).toMatch(`Hello`)
342-
}
343-
))
334+
const handler = jest.fn((msg, instance, trace) => {
335+
expect(msg).toMatch(`Component is missing template or render function`)
336+
expect(instance).toBe(ctx.proxy)
337+
expect(trace).toMatch(`Hello`)
338+
})
344339

345340
const Root = {
346341
name: 'Hello',
@@ -349,112 +344,92 @@ describe('api: createApp', () => {
349344
}
350345
}
351346

352-
app.mount(Root, nodeOps.createElement('div'))
347+
const app = createApp(Root)
348+
app.config.warnHandler = handler
349+
app.mount(nodeOps.createElement('div'))
353350
expect(handler).toHaveBeenCalledTimes(1)
354351
})
355352

356353
describe('config.isNativeTag', () => {
357354
const isNativeTag = jest.fn(tag => tag === 'div')
358355

359356
test('Component.name', () => {
360-
const app = createApp()
361-
Object.defineProperty(app.config, 'isNativeTag', {
362-
value: isNativeTag,
363-
writable: false
364-
})
365-
366357
const Root = {
367358
name: 'div',
368-
setup() {
369-
return {
370-
count: ref(0)
371-
}
372-
},
373359
render() {
374360
return null
375361
}
376362
}
377363

378-
app.mount(Root, nodeOps.createElement('div'))
379-
expect(
380-
`Do not use built-in or reserved HTML elements as component id: div`
381-
).toHaveBeenWarned()
382-
})
364+
const app = createApp(Root)
383365

384-
test('Component.components', () => {
385-
const app = createApp()
386366
Object.defineProperty(app.config, 'isNativeTag', {
387367
value: isNativeTag,
388368
writable: false
389369
})
390370

371+
app.mount(nodeOps.createElement('div'))
372+
expect(
373+
`Do not use built-in or reserved HTML elements as component id: div`
374+
).toHaveBeenWarned()
375+
})
376+
377+
test('Component.components', () => {
391378
const Root = {
392379
components: {
393380
div: () => 'div'
394381
},
395-
setup() {
396-
return {
397-
count: ref(0)
398-
}
399-
},
400382
render() {
401383
return null
402384
}
403385
}
404386

405-
app.mount(Root, nodeOps.createElement('div'))
387+
const app = createApp(Root)
388+
Object.defineProperty(app.config, 'isNativeTag', {
389+
value: isNativeTag,
390+
writable: false
391+
})
392+
393+
app.mount(nodeOps.createElement('div'))
406394
expect(
407395
`Do not use built-in or reserved HTML elements as component id: div`
408396
).toHaveBeenWarned()
409397
})
410398

411399
test('Component.directives', () => {
412-
const app = createApp()
413-
Object.defineProperty(app.config, 'isNativeTag', {
414-
value: isNativeTag,
415-
writable: false
416-
})
417-
418400
const Root = {
419401
directives: {
420402
bind: () => {}
421403
},
422-
setup() {
423-
return {
424-
count: ref(0)
425-
}
426-
},
427404
render() {
428405
return null
429406
}
430407
}
431408

432-
app.mount(Root, nodeOps.createElement('div'))
409+
const app = createApp(Root)
410+
Object.defineProperty(app.config, 'isNativeTag', {
411+
value: isNativeTag,
412+
writable: false
413+
})
414+
415+
app.mount(nodeOps.createElement('div'))
433416
expect(
434417
`Do not use built-in directive ids as custom directive id: bind`
435418
).toHaveBeenWarned()
436419
})
437420

438421
test('register using app.component', () => {
439-
const app = createApp()
422+
const app = createApp({
423+
render() {}
424+
})
425+
440426
Object.defineProperty(app.config, 'isNativeTag', {
441427
value: isNativeTag,
442428
writable: false
443429
})
444430

445-
const Root = {
446-
setup() {
447-
return {
448-
count: ref(0)
449-
}
450-
},
451-
render() {
452-
return null
453-
}
454-
}
455-
456431
app.component('div', () => 'div')
457-
app.mount(Root, nodeOps.createElement('div'))
432+
app.mount(nodeOps.createElement('div'))
458433
expect(
459434
`Do not use built-in or reserved HTML elements as component id: div`
460435
).toHaveBeenWarned()

0 commit comments

Comments
 (0)