Skip to content

Commit

Permalink
#38
Browse files Browse the repository at this point in the history
- дописаны тесты DI
  • Loading branch information
krutoo committed Mar 16, 2023
1 parent 374d272 commit 5bb3780
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/di/__test__/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,19 @@ describe('Application', () => {
child.get(TOKEN.client);
}).toThrow('Nothing bound to Token(config)');
});

it('should get component from cache', () => {
const app = createApplication();
const token = createToken<{ id: number }>('foo');

const value = { id: 123 };
app.bind(token).toValue(value);

const first = app.get(token);
const second = app.get(token);

expect(Object.is(value, first)).toBe(true);
expect(Object.is(value, second)).toBe(true);
expect(Object.is(first, second)).toBe(true);
});
});
11 changes: 10 additions & 1 deletion src/di/__test__/container.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createContainer } from '../container';
import { AlreadyBoundError, CircularDependencyError } from '../errors';
import { AlreadyBoundError, CircularDependencyError, NothingBoundError } from '../errors';
import { createToken } from '../token';

describe('Container', () => {
Expand Down Expand Up @@ -91,4 +91,13 @@ describe('Container', () => {
container.get(TOKEN.bar);
}).toThrow(new CircularDependencyError([TOKEN.bar, TOKEN.foo, TOKEN.baz, TOKEN.bar]));
});

it('should throw error when nothing boud to token', () => {
const container = createContainer();
const token = createToken('test');

expect(() => {
container.get(token);
}).toThrow(new NothingBoundError(token));
});
});
6 changes: 1 addition & 5 deletions src/di/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,7 @@ class ApplicationImplementation implements Application {
}

if (this.providers.has(token._key)) {
const provider = this.providers.get(token._key);

if (typeof provider !== 'function') {
throw new Error('Provider is not a function');
}
const provider = this.providers.get(token._key) as Provider<T>;

const component = provider(otherToken => this.resolve(otherToken, nextChain()));

Expand Down

0 comments on commit 5bb3780

Please sign in to comment.