From b5b4fdf116a3e019445f324e5faae3ed38c6b9aa Mon Sep 17 00:00:00 2001 From: xavier-rdo Date: Thu, 30 Mar 2017 17:00:24 +0200 Subject: [PATCH] Feature/container fetch method renamed (#3) * Container.fetch method renamed to 'get' * Added tests with Jest --- .babelrc | 3 +++ .gitignore | 2 ++ README.md | 2 +- package.json | 9 +++++++-- src/Container.js | 8 ++++---- test/Container.spec.js | 19 +++++++++++++++++++ 6 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 .babelrc create mode 100644 .gitignore create mode 100644 test/Container.spec.js diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..c13c5f6 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "presets": ["es2015"] +} diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1e509d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/node_modules/ +yarn.lock diff --git a/README.md b/README.md index 4ae9305..900f5f4 100644 --- a/README.md +++ b/README.md @@ -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(); ``` diff --git a/package.json b/package.json index 5cc231a..c935340 100644 --- a/package.json +++ b/package.json @@ -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" + } } diff --git a/src/Container.js b/src/Container.js index 4726da3..a3efc02 100644 --- a/src/Container.js +++ b/src/Container.js @@ -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); } /** @@ -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); } @@ -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); diff --git a/test/Container.spec.js b/test/Container.spec.js new file mode 100644 index 0000000..763507b --- /dev/null +++ b/test/Container.spec.js @@ -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.'); + }); + }); +});