Skip to content

Commit

Permalink
fix: New factory binding doesn't reset a cached value
Browse files Browse the repository at this point in the history
  • Loading branch information
mnasyrov committed Jun 2, 2024
1 parent 997ca44 commit 01b3dad
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
26 changes: 26 additions & 0 deletions packages/ditox/src/container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,32 @@ describe('Container', () => {
expect(container.get(CONTAINER)).toBe(container);
expect(container.get(PARENT_CONTAINER)).toBe(parent);
});

it('should rebind a value binding by the same token', () => {
const container = createContainer();

container.bindValue(NUMBER, 1);
container.bindFactory(NUMBER, () => 2);
expect(container.get(NUMBER)).toBe(2);
});

it('should rebind a factory binding by the same token', () => {
const container = createContainer();

container.bindFactory(NUMBER, () => 1);
container.bindFactory(NUMBER, () => 2);
expect(container.get(NUMBER)).toBe(2);
});

it('should rebind a factory binding with cached value by the same token', () => {
const container = createContainer();

container.bindFactory(NUMBER, () => 1);
expect(container.get(NUMBER)).toBe(1);

container.bindFactory(NUMBER, () => 2);
expect(container.get(NUMBER)).toBe(2);
});
});

describe('remove()', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/ditox/src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export function createContainer(parentContainer?: Container): Container {
}

factories.set(token.symbol, {factory, options});
values.delete(token.symbol);
},

remove<T>(token: Token<T>): void {
Expand Down

0 comments on commit 01b3dad

Please sign in to comment.