Skip to content

Commit

Permalink
Merge pull request #6 from scorpion/develop
Browse files Browse the repository at this point in the history
v1.0.4
  • Loading branch information
brettwilcox authored Sep 30, 2020
2 parents bc5dc9d + 6c586f8 commit 8e9f037
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 59 deletions.
42 changes: 7 additions & 35 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,16 @@ var Scorpion = (function () {
}, {
key: 'once',
value: function once(factory) {
var _arguments = arguments;

var id = (0, _utils.uuid)();
return function () {
for (var _len4 = arguments.length, args = Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}

if (!_instanceCache[id]) {
var thisArg = {};
_instanceCache[id] = {
returnValue: factory.apply(thisArg, _arguments)
returnValue: factory.apply(thisArg, args)
};
}
return _instanceCache[id].returnValue;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@
"prepublish": "npm run bundle",
"test": "mocha --reporter dot --recursive -r setup-referee-sinon/globals test/unit --compilers js:babel/register"
},
"version": "1.0.3"
"version": "1.0.4"
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ export default class Scorpion {

static once(factory) {
const id = uuid();
return () => {
return (...args) => {
if (!_instanceCache[id]) {
const thisArg = {};
_instanceCache[id] = {
returnValue: factory.apply(thisArg, arguments)
returnValue: factory.apply(thisArg, args)
};
}
return _instanceCache[id].returnValue;
Expand Down
40 changes: 22 additions & 18 deletions test/unit/index-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,29 +76,27 @@ describe('Scorpion', function() {
});
});

it('retrieves a module', function(done) {
di.get('depA').then(function(retrievedObj) {
it('retrieves a module', function() {
return di.get('depA').then(function(retrievedObj) {
expect(retrievedObj).toBe(depA);
done();
});
});

it('retrieves an async module', function(done) {
di.get('depB').then(function(retrievedObj) {
it('retrieves an async module', function() {
return di.get('depB').then(function(retrievedObj) {
expect(retrievedObj).toBe(depB);
done();
});
});

it('retrieves an async module and resolves its dependencies', function(done) {
it('retrieves an async module and resolves its dependencies', function() {
di.register('depC', ['depB'], function(dependencyB) {
return new Promise(function(resolve) {
resolve([depC, dependencyB]);
});
});
di.get('depC').then(function(arr) {
return di.get('depC').then(function(arr) {
expect(arr).toEqual([depC, depB]);
}).then(done, done);
});
});

it('throws when trying to resolve a direct cicular dependency', function() {
Expand Down Expand Up @@ -160,32 +158,30 @@ describe('Scorpion', function() {
});

describe('Scorpion.withNew', function() {
it('initializes a Constructor with new', function(done) {
it('initializes a Constructor with new', function() {
class Foo {
bar() {}
}

di.register('Foo', Scorpion.withNew(Foo));

di.get('Foo').then(function(foo) {
return di.get('Foo').then(function(foo) {
expect(foo instanceof Foo);
expect(foo.bar).not.toBe(undefined);
done();
});
});
});

describe('Scorpion.withNewOnce', function() {
it('initializes a constructor with new once and then always returns the instance', function(done) {
it('initializes a constructor with new once and then always returns the instance', function() {
function Foo() {
this.foo = true;
}

di.register('Foo', Scorpion.withNewOnce(Foo));

Promise.all([di.get('Foo'), di.get('Foo')]).then((values) => {
return Promise.all([di.get('Foo'), di.get('Foo')]).then((values) => {
expect(values[0]).toBe(values[1]);
done();
});
});

Expand All @@ -209,13 +205,21 @@ describe('Scorpion', function() {
});

describe('Scorpion.once', function() {
it('calls the factory once even when get is called multiple times', function(done) {
it('calls the factory once even when get is called multiple times', function() {
const spy = sinon.spy();
di.register('foo', Scorpion.once(spy));

Promise.all([di.get('foo'), di.get('foo')]).then(function() {
return Promise.all([di.get('foo'), di.get('foo')]).then(function() {
expect(spy.calledOnce).toBe(true);
done();
});
});
it('forwards the defined dependencies to the passed factory', function() {
const spy = sinon.spy();
di.register('bar', Scorpion.always('bar'));
di.register('foo', ['bar'], Scorpion.once(spy));

return di.get('foo').then(function(bar) {
expect(spy).toHaveBeenCalledWith('bar');
});
});
});
Expand Down

0 comments on commit 8e9f037

Please sign in to comment.