-
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
13 changed files
with
171 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<script setup lang="ts"> | ||
import { DemoService } from './DemoService'; | ||
import { declareProviders, useService } from '../../src/index'; | ||
defineProps({ | ||
msg: String, | ||
}); | ||
declareProviders([DemoService]); | ||
const service = useService(DemoService); | ||
defineExpose({ | ||
service, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div class="msg">{{ msg }}</div> | ||
<div class="count">{{ service.count }}</div> | ||
|
||
<button type="button" class="btn-count" @click="service.increaseCount()"> | ||
Add 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,7 @@ | ||
export class DemoService { | ||
public count = 1; | ||
|
||
public increaseCount() { | ||
this.count++; | ||
} | ||
} |
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 @@ | ||
## 测试场景-当前组件访问当前组件的服务 | ||
|
||
主要测试了 useService在组件外调用的异常 |
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,22 @@ | ||
import 'reflect-metadata'; | ||
import { mount } from '@vue/test-utils'; | ||
import DemoComp from './DemoComp.vue'; | ||
import { DemoService } from './DemoService'; | ||
import { useService } from '../../src'; | ||
|
||
describe('test12', () => { | ||
it('get DemoService instance', async () => { | ||
const msg = 'Hello world'; | ||
const wrapper = mount(DemoComp, { | ||
props: { | ||
msg, | ||
}, | ||
}); | ||
|
||
expect(wrapper.vm.service).toBeInstanceOf(DemoService); | ||
|
||
expect(() => { | ||
useService(DemoService); | ||
}).toThrow("Cannot read properties of undefined (reading 'get')"); | ||
}); | ||
}); |
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,27 @@ | ||
<script setup lang="ts"> | ||
import { DemoService } from './DemoService'; | ||
import { useService } from '../../src/index'; | ||
defineProps({ | ||
msg: String, | ||
}); | ||
console.log('before useService'); | ||
const service = useService(DemoService); | ||
console.log('after useService', service); | ||
defineExpose({ | ||
service, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div class="msg">{{ msg }}</div> | ||
<div class="count">{{ service.count }}</div> | ||
|
||
<button type="button" class="btn-count" @click="service.increaseCount()"> | ||
Add 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,7 @@ | ||
export class DemoService { | ||
public count = 1; | ||
|
||
public increaseCount() { | ||
this.count++; | ||
} | ||
} |
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 @@ | ||
## 测试场景-当前组件访问当前组件的服务 | ||
|
||
主要测试了 useService在组件内调用,但是没有declareProvides绑定服务,导致找不到服务异常 |
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,17 @@ | ||
import 'reflect-metadata'; | ||
import { mount } from '@vue/test-utils'; | ||
import DemoComp from './DemoComp.vue'; | ||
|
||
describe('test13', () => { | ||
it('get DemoService instance', async () => { | ||
const msg = 'Hello world'; | ||
|
||
expect(() => { | ||
mount(DemoComp, { | ||
props: { | ||
msg, | ||
}, | ||
}); | ||
}).toThrow('No matching bindings found for serviceIdentifier: DemoService'); | ||
}); | ||
}); |
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,26 @@ | ||
<script setup lang="ts"> | ||
import { DemoService } from './DemoService'; | ||
import { declareProviders, useService } from '../../src/index'; | ||
defineProps({ | ||
msg: String, | ||
}); | ||
declareProviders([DemoService]); | ||
const service = useService(DemoService); | ||
defineExpose({ | ||
service, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div> | ||
<div class="msg">{{ msg }}</div> | ||
<div class="count">{{ service.count }}</div> | ||
|
||
<button type="button" class="btn-count" @click="service.increaseCount()"> | ||
Add 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,7 @@ | ||
export class DemoService { | ||
public count = 1; | ||
|
||
public increaseCount() { | ||
this.count++; | ||
} | ||
} |
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 @@ | ||
## 测试场景-当前组件访问当前组件的服务 | ||
|
||
主要测试了 useRootService在组件外调用,但是没有declareRootProvides绑定服务,导致找不到服务异常 |
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,22 @@ | ||
import 'reflect-metadata'; | ||
import { mount } from '@vue/test-utils'; | ||
import DemoComp from './DemoComp.vue'; | ||
import { DemoService } from './DemoService'; | ||
import { useRootService } from '../../src'; | ||
|
||
describe('test14', () => { | ||
it('get DemoService instance', async () => { | ||
const msg = 'Hello world'; | ||
const wrapper = mount(DemoComp, { | ||
props: { | ||
msg, | ||
}, | ||
}); | ||
|
||
expect(wrapper.vm.service).toBeInstanceOf(DemoService); | ||
|
||
expect(() => { | ||
useRootService(DemoService); | ||
}).toThrow('No matching bindings found for serviceIdentifier: DemoService'); | ||
}); | ||
}); |