Skip to content

Commit 0c98179

Browse files
committed
Complete unit tests
1 parent d679542 commit 0c98179

File tree

5 files changed

+103
-23
lines changed

5 files changed

+103
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"vue component decorator"
1717
],
1818
"scripts": {
19-
"test-build": "npm run test && npm run build",
19+
"test-build": "npm run build && npm run test",
2020
"test": "mocha -r ts-node/register test/test.ts",
2121
"build": "npm run build:cjs && npm run build:esm",
2222
"build:cjs": "./node_modules/.bin/tsc",

src/option/setup.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { proxyRefs } from 'vue';
21
import type { Ref, SetupContext, ShallowUnwrapRef } from 'vue';
32
import type { Cons } from '../component'
43
import type { OptionBuilder } from '../optionBuilder'
@@ -28,16 +27,14 @@ export function build(cons: Cons, optionBuilder: OptionBuilder): Record<string,
2827
}
2928
promise = promise.then(() => {
3029
return setupState.then((value: any) => {
31-
setupData[name] = proxyRefs(value)
32-
return {}
30+
setupData[name] = value
3331
})
3432
})
3533
} else {
36-
setupData[name] = setupState;
34+
setupData[name] = setupState
3735
}
3836
}
3937
}
40-
4138
return promise ?? {};
4239
}
4340
return setupData;

test/option/setup.ts

Lines changed: 77 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,103 @@
11

22
import { inject } from 'vue'
33
import { mount } from '@vue/test-utils'
4-
import { expect } from 'chai';
5-
import 'mocha';
6-
import { Component, Base, setup } from '../../dist'
4+
import { expect } from 'chai'
5+
import { mountSuspense } from '../utils'
6+
import 'mocha'
7+
import { Component, Base, Prop, setup } from '../../dist'
78

8-
const AXIOM = 'setup is working to allow composition API usage'
9+
const SETUP_AXIOM = 'setup is working to allow composition API usage'
10+
const DATA_AXIOM = 'data is injected into the template'
911
const injectionKey = Symbol('injection test key')
1012

1113
function useInjectedValue() {
12-
inject(injectionKey)
14+
return inject(injectionKey)
1315
}
1416

15-
@Component
16-
export class Comp extends Base {
17+
@Component({
18+
render() { return [] }
19+
})
20+
export class SyncComp extends Base {
1721

1822
injectedValue = setup(() => useInjectedValue())
1923

2024
}
2125

22-
const CompContext = Comp as any
26+
const SyncCompContext = SyncComp as any
2327

28+
@Component({
29+
render() { return [] }
30+
})
31+
export class AsyncComp extends Base {
2432

25-
describe('setup function',
26-
() => {
27-
const wrapper = mount(CompContext,{
33+
injectedValue = setup(() => {
34+
const value = useInjectedValue()
35+
return new Promise((resolve) => {
36+
setTimeout(() => {
37+
resolve(value)
38+
}, 1)
39+
})
40+
})
41+
42+
}
43+
44+
const AsyncCompContext = AsyncComp as any
45+
46+
@Component({
47+
setup() {
48+
const injectedValue = useInjectedValue()
49+
return { injectedValue }
50+
},
51+
template: '{{ injectedValue }} {{ dataValue }}'
52+
})
53+
export class SetupComp extends Base {
54+
dataValue = DATA_AXIOM
55+
}
56+
57+
const SetupCompContext = SetupComp as any
58+
59+
describe('setup function', () => {
60+
describe('synchronous setup', () => {
61+
const wrapper = mount(SyncCompContext, {
2862
global: {
2963
provide: {
30-
[injectionKey]: AXIOM
64+
[injectionKey]: SETUP_AXIOM
3165
}
3266
}
3367
})
3468
const vm = wrapper.vm
3569
it('injects the value provided to the component via composition API', () => {
36-
expect(vm.injectedValue).to.equal(AXIOM)
70+
expect(vm.injectedValue).to.equal(SETUP_AXIOM)
71+
})
72+
})
73+
74+
describe('asynchronous setup', () => {
75+
it('injects the value provided to the component via composition API', async () => {
76+
const wrapper = await mountSuspense(AsyncCompContext, {
77+
global: {
78+
provide: {
79+
[injectionKey]: SETUP_AXIOM
80+
}
81+
}
82+
})
83+
const vm = wrapper.findComponent(AsyncCompContext).vm
84+
expect(vm.injectedValue).to.equal(SETUP_AXIOM)
3785
})
38-
}
39-
)
86+
})
87+
})
88+
89+
describe('setup option', () => {
90+
const wrapper = mount(SetupCompContext, {
91+
global: {
92+
provide: {
93+
[injectionKey]: SETUP_AXIOM
94+
}
95+
}
96+
})
97+
it('can inject variables into the template', () => {
98+
expect(wrapper.text()).to.contain(SETUP_AXIOM)
99+
expect(wrapper.text()).to.contain(DATA_AXIOM)
100+
})
101+
})
40102

41103
export default {}

test/test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ require('jsdom-global/keys.js').push('SVGElement')
22
require('jsdom-global')()
33
import './internal/utils'
44
import './component'
5+
import './option/setup'
56
import './option/data'
67
import './option/methods'
78
import './option/computed'

test/utils.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import { defineComponent, h, Suspense } from 'vue'
2+
import { mount } from '@vue/test-utils'
3+
4+
import type { VueWrapper } from '@vue/test-utils'
5+
16
export function isEmptyObject(arg: any) {
2-
if (typeof arg === 'undefined') {
7+
if (['undefined', 'function'].includes(typeof arg)) {
38
return true
49
}
510
if (arg === null) {
@@ -12,4 +17,19 @@ export function isEmptyObject(arg: any) {
1217
return Object.keys(arg).length === 0
1318
}
1419
}
15-
}
20+
}
21+
22+
export function mountSuspense(component: any, options: any = {}): Promise<VueWrapper> {
23+
return new Promise<VueWrapper>((resolve, reject) => {
24+
const wrapper = mount(defineComponent({
25+
render() {
26+
return h(Suspense, {
27+
onResolve: () => resolve(wrapper)
28+
}, {
29+
default: h(component, options?.props),
30+
fallback: h('div', 'fallback')
31+
})
32+
}
33+
}), { global: options?.global })
34+
})
35+
}

0 commit comments

Comments
 (0)