Skip to content

Commit

Permalink
fix: check md.value before container.set (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
hyj1991 authored Jun 30, 2022
1 parent 5aac90d commit 0177440
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 4 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@
"typescript": "^4.7.2"
},
"dependencies": {
"@artus/injection": "^0.2.0",
"@artus/pipeline": "^0.2.1",
"@artus/injection": "^0.2.5",
"@artus/pipeline": "^0.2.2",
"deepmerge": "^4.2.2",
"js-yaml": "^4.1.0",
"minimatch": "^5.0.1"
Expand Down
5 changes: 4 additions & 1 deletion src/loader/impl/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class ModuleLoader implements Loader {
if (item.id) {
opts.id = item.id;
}
this.container.set(opts);

if(!this.container.hasValue(opts)) {
this.container.set(opts);
}
}
}

Expand Down
29 changes: 29 additions & 0 deletions test/fixtures/custom_instance/bootstrap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {
ArtusApplication,
ApplicationLifecycle, LifecycleHookUnit, LifecycleHook,
WithApplication, WithContainer
} from '../../../src/index';

import { Container } from '@artus/injection';
import Custom from './custom';

@LifecycleHookUnit()
export default class MyLifecycle implements ApplicationLifecycle {
app: ArtusApplication;
container: Container;

constructor(@WithApplication() app: ArtusApplication, @WithContainer() container) {
this.app = app;
this.container = container;
}

@LifecycleHook()
configDidLoad() {
this.container.set({ id: Custom, value: new Custom('foo') });
}

@LifecycleHook()
async beforeClose() {
console.log(this.container.get(Custom))
}
}
1 change: 1 addition & 0 deletions test/fixtures/custom_instance/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default {};
14 changes: 14 additions & 0 deletions test/fixtures/custom_instance/custom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Injectable } from "../../../src/index";

@Injectable()
export default class Custom {
private name: string;

constructor(name: string) {
this.name = name;
}

getName(): string {
return this.name;
}
}
17 changes: 17 additions & 0 deletions test/fixtures/custom_instance/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Scanner, ArtusApplication } from '../../../src/index';

export async function createApp() {
const baseDir = __dirname;
const scanner = new Scanner({
needWriteFile: false,
configDir: 'config',
extensions: ['.ts'],
});
const manifest = await scanner.scan(baseDir);

const app = new ArtusApplication();
await app.load(manifest.default, baseDir);
await app.run();

return app;
}
13 changes: 12 additions & 1 deletion test/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import assert from 'assert';
import path from 'path';
import { Container } from '@artus/injection';
import { LoaderFactory } from '../src';
import { createApp } from './fixtures/custom_instance/index';
import Custom from './fixtures/custom_instance/custom';

describe('test/loader.test.ts', () => {
describe('module with ts', () => {
Expand All @@ -16,6 +18,7 @@ describe('test/loader.test.ts', () => {
assert((container.get('testServiceA') as any).testMethod() === 'Hello Artus');
});
});

describe('module with js', () => {
it('should load module testServiceA.js and testServiceB.js', async () => {
const container = new Container('testDefault');
Expand All @@ -24,13 +27,14 @@ describe('test/loader.test.ts', () => {
const manifest = require('./fixtures/module_with_js/src/index');
await loaderFactory.loadManifest(manifest);
const appProxy = new Proxy({}, {
get (_target, properName: string) {
get(_target, properName: string) {
return container.get(properName);
}
});
assert((container.get('testServiceA') as any).testMethod(appProxy) === 'Hello Artus');
});
});

describe('module with custom loader', () => {
it('should load module test.ts with custom loader', async () => {
// SEEME: the import&register code need be replaced by scanner at production.
Expand All @@ -55,4 +59,11 @@ describe('test/loader.test.ts', () => {
expect(console.log).toBeCalledWith('TestCustomLoader.state loaderState');
});
});

describe('custom instance', () => {
it('should not overide custom instance', async () => {
const app = await createApp();
expect(app.getContainer().get(Custom).getName()).toBe('foo');
})
});
});

0 comments on commit 0177440

Please sign in to comment.