Skip to content

Commit

Permalink
Fix Choice of Injection Strategy for Nested Files (#11)
Browse files Browse the repository at this point in the history
* adds package.json metadata

* bumps node dependency to 6.x

* fixes bug where given files in nested folders, it uses the file name and not folder name for determining injection strategy

* removes commented out filename
  • Loading branch information
ecowden committed Jun 14, 2017
1 parent bfb124c commit 7cf6368
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- "4"
- "6"
- "8"
after_success:
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
10 changes: 6 additions & 4 deletions lib/fixtures/dir2/MyConstructor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module.exports = function MyConstructor(myFactory, b) {
this.name = 'MyConstructor'
this.b = b
this.myFactory = myFactory
module.exports = class MyConstructor {
constructor(myFactory, b) {
this.name = 'MyConstructor'
this.b = b
this.myFactory = myFactory
}
}
7 changes: 7 additions & 0 deletions lib/fixtures/dir3/subdir/MyClass.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = class MyClass {
constructor(injected) {
this.injected = injected
}
}
1 change: 1 addition & 0 deletions lib/fixtures/dir3/subdir/injected.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'test message'
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = co.wrap(function* plutoPath(o) {
const component = components[filename]

const componentName = standardNamingStrategy(filename)
const bindingName = standardBindingStrategy(filename, component)
const bindingName = standardBindingStrategy(componentName, component)
bind(componentName)[bindingName](component)
}
if (options.extraBindings) {
Expand Down
26 changes: 17 additions & 9 deletions lib/indexSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ const path = require('path')
const plutoPath = require('./index')
const fixturesPath = path.join(__dirname, 'fixtures')
const simpleTestPath = path.join(fixturesPath, 'dir1')
const complexTextPath = path.join(fixturesPath, 'dir2')
const complexTestPath = path.join(fixturesPath, 'dir2')
const nestedTestPath = path.join(fixturesPath, 'dir3')

const simpleBindingMacro = co.wrap(function* (t, input, expected) {
const app = yield plutoPath(simpleTestPath)
Expand Down Expand Up @@ -39,31 +40,38 @@ test('The main function rejects with a meaningful error if given a bad configura
t.is(error.message, 'options must be a string, array or strings or an object')
})

test('given complex filex, it binds a non-function object using `toInstance`', function* (t) {
const app = yield plutoPath([complexTextPath, simpleTestPath])
test('given complex files, it binds a non-function object using `toInstance`', function* (t) {
const app = yield plutoPath([complexTestPath, simpleTestPath])
const actual = app.get('myData')
const expected = require('./fixtures/dir2/innerDir/myData.json')
t.is(actual, expected)
})

test('given complex filex, it binds a factory function using `toFactory`', function* (t) {
const app = yield plutoPath([complexTextPath, simpleTestPath])
test('given complex files, it binds a factory function using `toFactory`', function* (t) {
const app = yield plutoPath([complexTestPath, simpleTestPath])
const actual = app.get('myFactory')
const expected = require('./fixtures/dir2/myFactory')()
t.is(actual.name, expected.name)
})

test('given complex filex, it binds a constructor function using `toConstructor`', function* (t) {
test('given complex files, it binds a constructor function using `toConstructor`', function* (t) {
const MyConstructor = require('./fixtures/dir2/MyConstructor')
const app = yield plutoPath([complexTextPath, simpleTestPath])
const app = yield plutoPath([complexTestPath, simpleTestPath])
const actual = app.get('MyConstructor')
t.is(actual.name, MyConstructor.name)
t.truthy(actual instanceof MyConstructor)
})

test('given complex filex, it binds across modules', function* (t) {
const app = yield plutoPath([complexTextPath, simpleTestPath])
test('given complex files, it binds across modules', function* (t) {
const app = yield plutoPath([complexTestPath, simpleTestPath])
const actual = app.get('MyConstructor')
const expected = require('./fixtures/dir1/b')()
t.is(actual.b.name, expected.name)
})

test('given files in nested folders, it uses the file name and not folder name for determining injection strategy', function* (t) {
const app = yield plutoPath(nestedTestPath)
const actual = app.get('MyClass')
const expectedInjected = require('./fixtures/dir3/subdir/injected')
t.is(actual.injected, expectedInjected)
})
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pluto-path",
"version": "1.0.0",
"version": "1.1.0",
"description": "Create a Pluto dependency injection module from files in a path or paths",
"main": "lib/index.js",
"scripts": {
Expand All @@ -10,7 +10,12 @@
"lint-fix": "eslint . --fix"
},
"engines": {
"node": ">=4.0.0"
"node": ">=6.0.0"
},
"homepage": "https://github.com/ecowden/pluto-path",
"repository": {
"type": "git",
"url": "git://github.com/ecowden/pluto-path.git"
},
"keywords": [
"pluto",
Expand Down

0 comments on commit 7cf6368

Please sign in to comment.