-
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
8 changed files
with
164 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<script setup lang="ts"> | ||
import { DemoService } from './DemoService'; | ||
import { OtherService } from './OtherService'; | ||
import { declareProviders, useService } from '../../src/index'; | ||
import { TYPES } from './token'; | ||
defineProps({ | ||
msg: String, | ||
}); | ||
declareProviders(con => { | ||
con.bind(TYPES.DemoService).to(DemoService); | ||
con.bind(TYPES.OtherService).to(OtherService); | ||
}); | ||
const service = useService(TYPES.DemoService); | ||
defineExpose({ | ||
service, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div class="msg">{{ msg }}</div> | ||
<div class="count">{{ service.count }}</div> | ||
<div class="other-count">{{ service.otherService.count }}</div> | ||
<div class="age">{{ service.age }}</div> | ||
<div class="name">{{ service.name }}</div> | ||
<div class="computedName">{{ service.computedName }}</div> | ||
|
||
<button type="button" class="btn-age" @click="service.increaseAge()"> | ||
Add age | ||
</button> | ||
<button type="button" class="btn-count" @click="service.increaseCount()"> | ||
Add count | ||
</button> | ||
<button type="button" class="btn-other-count" @click="service.increaseOtherCount()"> | ||
Add other count | ||
</button> | ||
</div> | ||
</template> |
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,37 @@ | ||
import { computed } from 'vue'; | ||
import { postReactive } from '../../src/index'; | ||
import { TYPES } from './token'; | ||
import { inject } from 'inversify'; | ||
|
||
export class DemoService { | ||
public count = 1; | ||
public age = 100; | ||
private _name = 'DemoService'; | ||
public computedName: any; | ||
|
||
@inject(TYPES.OtherService) | ||
public otherService: any; | ||
|
||
public increaseOtherCount() { | ||
this.otherService.increaseCount(); | ||
} | ||
|
||
public increaseCount() { | ||
this.count++; | ||
} | ||
|
||
public increaseAge() { | ||
this.age++; | ||
} | ||
|
||
public get name() { | ||
return `${this._name}-${this.age}`; | ||
} | ||
|
||
@postReactive() | ||
public init() { | ||
this.computedName = computed(() => { | ||
return `${this._name}-${this.age}`; | ||
}); | ||
} | ||
} |
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,7 @@ | ||
export class OtherService { | ||
public count = 100; | ||
|
||
public increaseCount() { | ||
this.count += 10; | ||
} | ||
} |
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,3 @@ | ||
## 测试场景-使用createToken生成token | ||
|
||
useService可以自动识别token对应的服务的类型 |
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,66 @@ | ||
import 'reflect-metadata'; | ||
import { mount } from '@vue/test-utils'; | ||
import DemoComp from './DemoComp.vue'; | ||
import { DemoService } from './DemoService'; | ||
import { OtherService } from './OtherService'; | ||
|
||
describe('test5', () => { | ||
it('get DemoService instance', async () => { | ||
const msg = 'Hello world'; | ||
const wrapper = mount(DemoComp, { | ||
props: { | ||
msg, | ||
}, | ||
}); | ||
|
||
expect(wrapper.vm.service).toBeInstanceOf(DemoService); | ||
expect(wrapper.vm.service.otherService).toBeInstanceOf(OtherService); | ||
|
||
expect(wrapper.get('.msg').text()).toBe(msg); | ||
expect(wrapper.get('.count').text()).toBe('1'); | ||
expect(wrapper.get('.other-count').text()).toBe('100'); | ||
expect(wrapper.get('.age').text()).toBe('100'); | ||
expect(wrapper.get('.name').text()).toBe('DemoService-100'); | ||
expect(wrapper.get('.computedName').text()).toBe('DemoService-100'); | ||
|
||
await wrapper.get('.btn-count').trigger('click'); | ||
expect(wrapper.get('.msg').text()).toBe(msg); | ||
expect(wrapper.get('.count').text()).toBe('2'); | ||
expect(wrapper.get('.other-count').text()).toBe('100'); | ||
expect(wrapper.get('.age').text()).toBe('100'); | ||
expect(wrapper.get('.name').text()).toBe('DemoService-100'); | ||
expect(wrapper.get('.computedName').text()).toBe('DemoService-100'); | ||
|
||
await wrapper.get('.btn-count').trigger('click'); | ||
expect(wrapper.get('.msg').text()).toBe(msg); | ||
expect(wrapper.get('.count').text()).toBe('3'); | ||
expect(wrapper.get('.other-count').text()).toBe('100'); | ||
expect(wrapper.get('.age').text()).toBe('100'); | ||
expect(wrapper.get('.name').text()).toBe('DemoService-100'); | ||
expect(wrapper.get('.computedName').text()).toBe('DemoService-100'); | ||
|
||
await wrapper.get('.btn-other-count').trigger('click'); | ||
expect(wrapper.get('.msg').text()).toBe(msg); | ||
expect(wrapper.get('.count').text()).toBe('3'); | ||
expect(wrapper.get('.other-count').text()).toBe('110'); | ||
expect(wrapper.get('.age').text()).toBe('100'); | ||
expect(wrapper.get('.name').text()).toBe('DemoService-100'); | ||
expect(wrapper.get('.computedName').text()).toBe('DemoService-100'); | ||
|
||
await wrapper.get('.btn-age').trigger('click'); | ||
expect(wrapper.get('.msg').text()).toBe(msg); | ||
expect(wrapper.get('.count').text()).toBe('3'); | ||
expect(wrapper.get('.other-count').text()).toBe('110'); | ||
expect(wrapper.get('.age').text()).toBe('101'); | ||
expect(wrapper.get('.name').text()).toBe('DemoService-101'); | ||
expect(wrapper.get('.computedName').text()).toBe('DemoService-101'); | ||
|
||
await wrapper.get('.btn-age').trigger('click'); | ||
expect(wrapper.get('.msg').text()).toBe(msg); | ||
expect(wrapper.get('.count').text()).toBe('3'); | ||
expect(wrapper.get('.other-count').text()).toBe('110'); | ||
expect(wrapper.get('.age').text()).toBe('102'); | ||
expect(wrapper.get('.name').text()).toBe('DemoService-102'); | ||
expect(wrapper.get('.computedName').text()).toBe('DemoService-102'); | ||
}); | ||
}); |
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,8 @@ | ||
import { DemoService } from './DemoService'; | ||
import { OtherService } from './OtherService'; | ||
import { createToken } from '../../src/index'; | ||
|
||
export const TYPES = { | ||
DemoService: createToken<DemoService>('DemoService'), | ||
OtherService: createToken<OtherService>('OtherService'), | ||
}; |