Skip to content

Commit 088cc68

Browse files
committed
test: unit test for optioanl inject
1 parent 45320f0 commit 088cc68

File tree

19 files changed

+261
-2
lines changed

19 files changed

+261
-2
lines changed

core/core-decorator/test/decorators.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ describe('test/decorator.test.ts', () => {
9090
assert.deepStrictEqual(injectConstructors, [
9191
{ refIndex: 0, refName: 'xCache', objName: 'fooCache' },
9292
{ refIndex: 1, refName: 'cache', objName: 'cache' },
93-
{ refIndex: 2, refName: 'optional1', objName: 'optional1' },
94-
{ refIndex: 3, refName: 'optional2', objName: 'optional2' },
93+
{ refIndex: 2, refName: 'optional1', objName: 'optional1', optional: true },
94+
{ refIndex: 3, refName: 'optional2', objName: 'optional2', optional: true },
9595
]);
9696
});
9797
});

plugin/tegg/test/Inject.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import mm from 'egg-mock';
2+
import path from 'path';
3+
import assert from 'assert';
4+
import { BarService } from './fixtures/apps/optional-inject/app/modules/module-a/BarService';
5+
import { FooService } from './fixtures/apps/optional-inject/app/modules/module-a/FooService';
6+
7+
describe('plugin/tegg/test/Inject.test.ts', () => {
8+
let app;
9+
10+
beforeEach(async () => {
11+
mm(process.env, 'EGG_TYPESCRIPT', true);
12+
mm(process, 'cwd', () => {
13+
return path.join(__dirname, '..');
14+
});
15+
});
16+
17+
afterEach(async () => {
18+
await app.close();
19+
mm.restore();
20+
});
21+
22+
describe('optional', () => {
23+
beforeEach(async () => {
24+
app = mm.app({
25+
baseDir: path.join(__dirname, 'fixtures/apps/optional-inject'),
26+
framework: require.resolve('egg'),
27+
});
28+
await app.ready();
29+
});
30+
31+
it('should work with property', async () => {
32+
const barService: BarService = await app.getEggObject(BarService);
33+
const res = barService.bar();
34+
assert.deepStrictEqual(res, {
35+
nil1: 'Y',
36+
nil2: 'Y',
37+
});
38+
});
39+
40+
it('should work with constructor', async () => {
41+
const fooService: FooService = await app.getEggObject(FooService);
42+
const res = fooService.foo();
43+
assert.deepStrictEqual(res, {
44+
nil1: 'Y',
45+
nil2: 'Y',
46+
});
47+
});
48+
});
49+
50+
it('should throw error if no proto found', async () => {
51+
app = mm.app({
52+
baseDir: path.join(__dirname, 'fixtures/apps/invalid-inject'),
53+
framework: require.resolve('egg'),
54+
});
55+
await assert.rejects(
56+
app.ready(),
57+
/EggPrototypeNotFound: Object doesNotExist not found in LOAD_UNIT:a/,
58+
);
59+
});
60+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { SingletonProto, Inject } from '@eggjs/tegg';
2+
3+
@SingletonProto()
4+
export class BarService {
5+
@Inject()
6+
doesNotExist: object;
7+
8+
bar() {
9+
console.log(this.doesNotExist);
10+
}
11+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "module-a",
3+
"eggModule": {
4+
"name": "a"
5+
}
6+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
5+
module.exports = function(appInfo) {
6+
const config = {
7+
keys: 'test key',
8+
customLogger: {
9+
xxLogger: {
10+
file: path.join(appInfo.root, 'logs/xx.log'),
11+
},
12+
},
13+
security: {
14+
csrf: {
15+
ignoreJSON: false,
16+
},
17+
},
18+
};
19+
return config;
20+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
exports.tracer = {
4+
package: 'egg-tracer',
5+
enable: true,
6+
};
7+
8+
exports.teggConfig = {
9+
package: '@eggjs/tegg-config',
10+
enable: true,
11+
};
12+
13+
exports.watcher = false;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "egg-app"
3+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SingletonProto, Inject, InjectOptional } from '@eggjs/tegg';
2+
3+
@SingletonProto()
4+
export class BarService {
5+
@Inject({ optional: true })
6+
doesNotExist1: object;
7+
8+
@InjectOptional()
9+
doesNotExist2: object;
10+
11+
bar() {
12+
return {
13+
nil1: this.doesNotExist1 ? 'N' : 'Y',
14+
nil2: this.doesNotExist2 ? 'N' : 'Y',
15+
};
16+
}
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { SingletonProto, Inject, InjectOptional } from '@eggjs/tegg';
2+
3+
@SingletonProto()
4+
export class FooService {
5+
constructor(
6+
@Inject({ optional: true }) readonly doesNotExist1: object,
7+
@InjectOptional() readonly doesNotExist2: object,
8+
) {}
9+
10+
foo() {
11+
return {
12+
nil1: this.doesNotExist1 ? 'N' : 'Y',
13+
nil2: this.doesNotExist2 ? 'N' : 'Y',
14+
};
15+
}
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"name": "module-a",
3+
"eggModule": {
4+
"name": "a"
5+
}
6+
}

0 commit comments

Comments
 (0)