Skip to content

Instantly share code, notes, and snippets.

@sylvain-hamel
Created January 27, 2016 04:35
Show Gist options
  • Save sylvain-hamel/332aeba3d9e65075c539 to your computer and use it in GitHub Desktop.
Save sylvain-hamel/332aeba3d9e65075c539 to your computer and use it in GitHub Desktop.
aurelia scoped instance using autoRegistrer
import {Container, singleton} from 'aurelia-dependency-injection';
describe('DI scoped singletong', () => {
@singleton(true)
class Foo{}
it('should work the way I think :)', () => {
let rootContainer = new Container();
let rootSingleton = rootContainer.get(Foo)
let rootSingletonB = rootContainer.get(Foo)
let childContainer1 = rootContainer.createChild();
childContainer1.autoRegister(Foo);
let childContainer1singleton = childContainer1.get(Foo)
let childContainer11 = childContainer1.createChild();
let childContainer11singleton = childContainer11.get(Foo)
let childContainer2 = rootContainer.createChild();
childContainer2.autoRegister(Foo);
let childContainer2singleton = childContainer2.get(Foo)
let childContainer22 = childContainer2.createChild();
let childContainer22singleton = childContainer22.get(Foo)
expect(rootSingletonB).toBe(rootSingleton, '1 - roots shoud be same object');
expect(childContainer1singleton).not.toBe(rootSingleton, '2 - scope singleton should not be same as root');
expect(childContainer2singleton).not.toBe(rootSingleton, '3 - scope singleton should not be same as root');
expect(childContainer11singleton).toBe(childContainer1singleton, '4 - inner scope singleton be same as outer');
expect(childContainer22singleton).toBe(childContainer2singleton, '5 - inner scope singleton be same as outer');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment