Skip to content

Commit

Permalink
- Fixed override function to ignore case-sensitiveness
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcwbr committed Mar 22, 2021
1 parent 7eec24d commit 933fc26
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.2.1] - 2020-03-22

### Fixed

* Fixed `ServiceContainer.override` function to ignore case-sensitiveness

## [1.2.0] - 2020-03-22

### Added
Expand Down
5 changes: 3 additions & 2 deletions src/ServiceContainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ export default {
},

override(id: string, factory: () => any) {
const index = services.findIndex(s => s.id === id.toLowerCase());
const lowerId = id.toLowerCase();
const index = services.findIndex(s => s.id === lowerId);
if (index === -1) {
throw new Error(`No service is registered for [${id}]`);
}

services[index] = {
id,
id: lowerId,
factory
};
},
Expand Down
10 changes: 5 additions & 5 deletions tests/ServiceContainer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,11 @@ describe('ServiceContainer', () => {
});

it('should override service if possible', () => {
expect(() => ServiceContainer.override('counter', () => 99)).toThrow('No service is registered for [counter]');
ServiceContainer.set('counter', () => 1);
expect(ServiceContainer.get('counter')).toBe(1);
ServiceContainer.override('counter', () => 22);
expect(ServiceContainer.get('counter')).toBe(22);
expect(() => ServiceContainer.override('TestCounter', () => 99)).toThrow('No service is registered for [TestCounter]');
ServiceContainer.set('TestCounter', () => 1);
expect(ServiceContainer.get('TestCounter')).toBe(1);
ServiceContainer.override('TestCounter', () => 22);
expect(ServiceContainer.get('TestCounter')).toBe(22);
});

afterEach(() => {
Expand Down

0 comments on commit 933fc26

Please sign in to comment.