|
1 | 1 |
|
2 | 2 | import { inject } from 'vue' |
3 | 3 | 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' |
7 | 8 |
|
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' |
9 | 11 | const injectionKey = Symbol('injection test key') |
10 | 12 |
|
11 | 13 | function useInjectedValue() { |
12 | | - inject(injectionKey) |
| 14 | + return inject(injectionKey) |
13 | 15 | } |
14 | 16 |
|
15 | | -@Component |
16 | | -export class Comp extends Base { |
| 17 | +@Component({ |
| 18 | + render() { return [] } |
| 19 | +}) |
| 20 | +export class SyncComp extends Base { |
17 | 21 |
|
18 | 22 | injectedValue = setup(() => useInjectedValue()) |
19 | 23 |
|
20 | 24 | } |
21 | 25 |
|
22 | | -const CompContext = Comp as any |
| 26 | +const SyncCompContext = SyncComp as any |
23 | 27 |
|
| 28 | +@Component({ |
| 29 | + render() { return [] } |
| 30 | +}) |
| 31 | +export class AsyncComp extends Base { |
24 | 32 |
|
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, { |
28 | 62 | global: { |
29 | 63 | provide: { |
30 | | - [injectionKey]: AXIOM |
| 64 | + [injectionKey]: SETUP_AXIOM |
31 | 65 | } |
32 | 66 | } |
33 | 67 | }) |
34 | 68 | const vm = wrapper.vm |
35 | 69 | 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) |
37 | 85 | }) |
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 | +}) |
40 | 102 |
|
41 | 103 | export default {} |
0 commit comments