-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
236 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,21 @@ | ||
import { getCurrentInstance } from 'vue'; | ||
|
||
/** | ||
* 主要实现逻辑是粘贴复制与vue中 | ||
* 在原基础上增加了一个额外的参数selfInject=false | ||
* 所以默认行为和原vue的实现一致 | ||
* 但是当selfInject=true时则与原vue实现不一致 | ||
* 从当前组件开始查找provider | ||
* | ||
* @export | ||
* @param {*} key | ||
* @param {*} defaultValue | ||
* @param {boolean} [treatDefaultAsFactory=false] | ||
* @param {boolean} [selfInject=false] | ||
* @return {*} | ||
*/ | ||
export function inject( | ||
key: any, | ||
defaultValue: any, | ||
treatDefaultAsFactory = false, | ||
selfInject = false | ||
) { | ||
// fallback to `currentRenderingInstance` so that this can be called in | ||
// a functional component | ||
export function inject(key: any, defaultValue: any) { | ||
const instance: any = getCurrentInstance(); | ||
if (instance) { | ||
// #2400 | ||
// to support `app.use` plugins, | ||
// fallback to appContext's `provides` if the intance is at root | ||
// 注意这里的 instance.provides 的原型链的最末端就是 instance.vnode.appContext.provides | ||
const provides = selfInject | ||
? instance.provides | ||
: instance.parent == null | ||
? instance.vnode.appContext && instance.vnode.appContext.provides | ||
: instance.parent.provides; | ||
|
||
if (provides && key in provides) { | ||
// TS doesn't allow symbol as index type | ||
return provides[key]; | ||
} else if (arguments.length > 1) { | ||
return treatDefaultAsFactory && typeof defaultValue === 'function' | ||
? defaultValue() | ||
: defaultValue; | ||
} else { | ||
console.warn(`injection "${String(key)}" not found.`); | ||
} | ||
const provides = instance.provides; | ||
return provides[key] || defaultValue; | ||
} else { | ||
console.warn( | ||
`inject() can only be used inside setup() or functional components.` | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* 从当前组件开始查找provider | ||
* | ||
* @export | ||
* @param {*} key | ||
* @param {*} defaultValue | ||
* @return {*} | ||
*/ | ||
export function injectFromSelf(key: any, defaultValue: any) { | ||
return inject(key, defaultValue, false, true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import 'reflect-metadata'; | ||
|
||
import { mount } from '@vue/test-utils'; | ||
|
||
import Counter from '@components/Counter.vue'; | ||
|
||
import CounterService from '@services/counter.service'; | ||
import { COUNTER_THEME } from '@services/service.context'; | ||
|
||
import { declareProviders, useService } from '@src/index'; | ||
import { defineComponent, isReactive } from 'vue'; | ||
|
||
const TestApp = defineComponent({ | ||
template: '<Counter :name="name" :counter="counter"></Counter>', | ||
components: { Counter }, | ||
props: ['name'], | ||
setup() { | ||
declareProviders([ | ||
{ | ||
provide: COUNTER_THEME, | ||
useValue: '#69c0ff', | ||
}, | ||
]); | ||
// 调用2次declareProviders就会报错 | ||
declareProviders([ | ||
CounterService, // 这里设置CounterService决定了这个服务是被缓存在当前组件所对应的Injector中的,那么如果有多个组件实例意味着服务也是多例的。 | ||
]); | ||
const counterService = useService(CounterService); | ||
console.log( | ||
'isReactive(props.counterService) :>> ', | ||
isReactive(counterService) | ||
); | ||
return { | ||
counter: counterService, | ||
}; | ||
}, | ||
}); | ||
|
||
describe('Component 声明了服务CounterService', () => { | ||
test('渲染组件、获取服务数据', async () => { | ||
expect(() => { | ||
mount(TestApp, { | ||
props: { | ||
name: 'counter1', | ||
}, | ||
}); | ||
}).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import 'reflect-metadata'; | ||
|
||
import { mount } from '@vue/test-utils'; | ||
|
||
import Counter from '@components/Counter.vue'; | ||
|
||
import CounterService from '@services/counter.service'; | ||
import { COUNTER_THEME } from '@services/service.context'; | ||
|
||
import { declareProviders, useService } from '@src/index'; | ||
import { defineComponent, isReactive, ref } from 'vue'; | ||
|
||
const TestAComponent = defineComponent({ | ||
template: | ||
'<div id="a-component"><Counter :name="name" :counter="counter"></Counter></div>', | ||
components: { Counter }, | ||
setup() { | ||
console.log('setup 111111'); | ||
|
||
declareProviders([CounterService]); | ||
|
||
console.log('setup 2222222'); | ||
|
||
const counterService = useService(CounterService); | ||
|
||
console.log('setup 333333'); | ||
|
||
return { | ||
name: 'a-component', | ||
counter: counterService, | ||
}; | ||
}, | ||
}); | ||
|
||
const TestBComponent = defineComponent({ | ||
template: | ||
'<div id="b-component"><Counter :name="name" :counter="counter"></Counter></div>', | ||
components: { Counter }, | ||
setup() { | ||
console.log('setup 444444'); | ||
|
||
declareProviders([CounterService]); | ||
|
||
console.log('setup 5555555'); | ||
|
||
const counterService = useService(CounterService); | ||
|
||
console.log('setup 666666'); | ||
|
||
return { | ||
name: 'b-component', | ||
counter: counterService, | ||
}; | ||
}, | ||
}); | ||
|
||
const TestApp = defineComponent({ | ||
template: | ||
'<div><button id="btn-toggle" type="button" @click="toggleFlag">点我</button><div v-if="flag"><TestAComponent/></div><div v-else><TestBComponent/></div></div>', | ||
components: { TestAComponent, TestBComponent }, | ||
setup() { | ||
console.log('setup 0000001'); | ||
|
||
declareProviders([ | ||
{ | ||
provide: COUNTER_THEME, | ||
useValue: '#69c0ff', | ||
}, | ||
]); | ||
|
||
const flag = ref(true); | ||
|
||
const toggleFlag = () => { | ||
flag.value = !flag.value; | ||
}; | ||
|
||
console.log('setup 0000002'); | ||
|
||
return { | ||
flag, | ||
toggleFlag, | ||
}; | ||
}, | ||
}); | ||
|
||
describe('通过if-else来测试onUnmount事件', () => { | ||
test('渲染组件、获取服务数据', async () => { | ||
const wrapper = mount(TestApp); | ||
|
||
expect(wrapper.vm.flag).toBe(true); | ||
|
||
expect(wrapper.findComponent(TestAComponent).exists()).toBe(true); | ||
expect(wrapper.find('#a-component').exists()).toBe(true); | ||
|
||
expect(wrapper.findComponent(TestBComponent).exists()).toBe(false); | ||
expect(wrapper.find('#b-component').exists()).toBe(false); | ||
|
||
await wrapper.find('#btn-toggle').trigger('click'); | ||
|
||
expect(wrapper.vm.flag).toBe(false); | ||
|
||
expect(wrapper.findComponent(TestAComponent).exists()).toBe(false); | ||
expect(wrapper.find('#a-component').exists()).toBe(false); | ||
|
||
expect(wrapper.findComponent(TestBComponent).exists()).toBe(true); | ||
expect(wrapper.find('#b-component').exists()).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Component 组件快照、服务共享 1`] = `<div class="container" style="background: rgb(105, 192, 255);"><span class="title">counter2:</span><button class="decrementBtn" type="button"> - </button><span class="countNum">5</span><button class="incrementBtn" type="button"> + </button></div>`; | ||
exports[`Component 没有声明服务CounterService 组件快照、服务共享 1`] = `<div class="container" style="background: rgb(105, 192, 255);"><span class="title">counter2:</span><button class="decrementBtn" type="button"> - </button><span class="countNum">5</span><button class="incrementBtn" type="button"> + </button></div>`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Component 组件快照、服务共享 1`] = `<div class="container" style="background: rgb(105, 192, 255);"><span class="title">counter2:</span><button class="decrementBtn" type="button"> - </button><span class="countNum">0</span><button class="incrementBtn" type="button"> + </button></div>`; | ||
exports[`Component 声明了服务CounterService 组件快照、服务不共享 1`] = `<div class="container" style="background: rgb(105, 192, 255);"><span class="title">counter2:</span><button class="decrementBtn" type="button"> - </button><span class="countNum">0</span><button class="incrementBtn" type="button"> + </button></div>`; |
Oops, something went wrong.