Skip to content

Commit

Permalink
Feature/container fetch method renamed (#3)
Browse files Browse the repository at this point in the history
* Container.fetch method renamed to 'get'
* Added tests with Jest
  • Loading branch information
xavier-rdo authored and Tom32i committed Mar 30, 2017
1 parent 0899c0d commit b5b4fdf
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015"]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules/
yarn.lock
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ Require the `api` service wherever you need it:
```js
import container from 'my-container.js';

container.fetch('api').login();
container.get('api').login();
```
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
},
"homepage": "https://github.com/Elao/container.js#readme",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "jest"
},
"keywords": [
"dependency",
"injection",
"container",
"service"
]
],
"devDependencies": {
"babel-jest": "^19.0.0",
"babel-preset-es2015": "^6.24.0",
"jest": "^19.0.2"
}
}
8 changes: 4 additions & 4 deletions src/Container.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Container {
this.parameters = new Map();

this.resolve = this.resolve.bind(this);
this.fetch = this.fetch.bind(this);
this.get = this.get.bind(this);
}

/**
Expand Down Expand Up @@ -75,13 +75,13 @@ class Container {
}

/**
* Fetch parameter or service
* Get parameter or service identified by its name
*
* @param {String} name
*
* @return {mixed}
*/
fetch(name) {
get(name) {
if (this.cache.has(name)) {
return this.cache.get(name);
}
Expand Down Expand Up @@ -119,7 +119,7 @@ class Container {
* @return {mixed}
*/
resolve(definition) {
const dependencies = definition.dependencies.map(this.fetch);
const dependencies = definition.dependencies.map(this.get);
const Constructor = definition.classname;
const service = new Constructor(...dependencies);

Expand Down
19 changes: 19 additions & 0 deletions test/Container.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Container from '../src/Container';

describe('Container', () => {
const container = new Container();
class MyClass { }
container.registerDefinition('my:class', MyClass);

describe('Container.get()', () => {
test('should return the expected service when asked for', () => {
expect(container.get('my:class')).toBeInstanceOf(MyClass);
});

test('should throw an error if an unexisting service or param is accessed', () => {
expect(() => {
container.get('unexisting:service');
}).toThrowError('Service or parameter "unexisting:service" not found.');
});
});
});

0 comments on commit b5b4fdf

Please sign in to comment.