Skip to content

Commit

Permalink
增加测试用例-找不到服务异常
Browse files Browse the repository at this point in the history
  • Loading branch information
kaokei committed Nov 22, 2024
1 parent ad6618f commit 5a4223e
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
1. bindContainer多次调用验证
3. 验证useRootService多次调用结果
4. 验证useRootService在组件内/外调用的结果
5. 全局provide路由变量-route和router // todo
5. 全局provide路由变量-route和router --> test11
9. ContainerOptions不同场景验证
1. 验证declareProviders不同参数的结果
2. 验证declareRootProviders不同参数的结果
Expand Down
26 changes: 26 additions & 0 deletions tests/test12/DemoComp.vue
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>
7 changes: 7 additions & 0 deletions tests/test12/DemoService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class DemoService {
public count = 1;

public increaseCount() {
this.count++;
}
}
3 changes: 3 additions & 0 deletions tests/test12/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 测试场景-当前组件访问当前组件的服务

主要测试了 useService在组件外调用的异常
22 changes: 22 additions & 0 deletions tests/test12/demo.test.ts
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')");
});
});
27 changes: 27 additions & 0 deletions tests/test13/DemoComp.vue
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>
7 changes: 7 additions & 0 deletions tests/test13/DemoService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class DemoService {
public count = 1;

public increaseCount() {
this.count++;
}
}
3 changes: 3 additions & 0 deletions tests/test13/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 测试场景-当前组件访问当前组件的服务

主要测试了 useService在组件内调用,但是没有declareProvides绑定服务,导致找不到服务异常
17 changes: 17 additions & 0 deletions tests/test13/demo.test.ts
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');
});
});
26 changes: 26 additions & 0 deletions tests/test14/DemoComp.vue
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>
7 changes: 7 additions & 0 deletions tests/test14/DemoService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export class DemoService {
public count = 1;

public increaseCount() {
this.count++;
}
}
3 changes: 3 additions & 0 deletions tests/test14/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 测试场景-当前组件访问当前组件的服务

主要测试了 useRootService在组件外调用,但是没有declareRootProvides绑定服务,导致找不到服务异常
22 changes: 22 additions & 0 deletions tests/test14/demo.test.ts
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');
});
});

0 comments on commit 5a4223e

Please sign in to comment.