Skip to content

Commit

Permalink
Merge pull request #24 from nikz/move-integration-logic-to-test-module
Browse files Browse the repository at this point in the history
Move integration container logic to TestModule
  • Loading branch information
dgeb committed Mar 12, 2015
2 parents 06ff474 + 312101d commit cdce5e8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 20 deletions.
21 changes: 3 additions & 18 deletions lib/ember-test-helpers/test-module-for-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import { getResolver } from './test-resolver';
import { getContext, setContext } from './test-context';

export default TestModule.extend({

isIntegration: true,

init: function(name, description, callbacks) {
this._super.call(this, name, description, callbacks);
this.setupSteps.push(this.setupIntegrationHelpers);
Expand Down Expand Up @@ -55,24 +58,6 @@ export default TestModule.extend({

},

setupContainer: function() {
var resolver = getResolver();
var namespace = Ember.Object.create({
Resolver: { create: function() { return resolver; } }
});

if (Ember.Application.buildRegistry) {
var registry;
registry = Ember.Application.buildRegistry(namespace);
registry.register('component-lookup:main', Ember.ComponentLookup);
this.registry = registry;
this.container = registry.container();
} else {
this.container = Ember.Application.buildContainer(namespace);
this.container.register('component-lookup:main', Ember.ComponentLookup);
}
},

setupContext: function() {

setContext({
Expand Down
37 changes: 36 additions & 1 deletion lib/ember-test-helpers/test-module.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Ember from 'ember';
import isolatedContainer from './isolated-container';
import { getContext, setContext } from './test-context';
import { Klass } from 'klassy';
import { getResolver } from './test-resolver';

export default Klass.extend({
init: function(subjectName, description, callbacks) {
Expand All @@ -16,6 +18,11 @@ export default Klass.extend({
this.name = description || subjectName;
this.callbacks = callbacks || {};

if (this.callbacks.integration) {
this.isIntegration = callbacks.integration;
delete callbacks.integration;
}

this.initSubject();
this.initNeeds();
this.initSetupSteps();
Expand Down Expand Up @@ -97,7 +104,11 @@ export default Klass.extend({
},

setupContainer: function() {
this.container = isolatedContainer(this.needs);
if (this.isIntegration) {
this._setupIntegratedContainer();
} else {
this._setupIsolatedContainer();
}
},

setupContext: function() {
Expand Down Expand Up @@ -184,6 +195,30 @@ export default Klass.extend({

})(keys[i]);
}
},


_setupIsolatedContainer: function() {
this.container = isolatedContainer(this.needs);
},

_setupIntegratedContainer: function() {
var resolver = getResolver();
var namespace = Ember.Object.create({
Resolver: { create: function() { return resolver; } }
});

if (Ember.Application.buildRegistry) {
var registry;
registry = Ember.Application.buildRegistry(namespace);
registry.register('component-lookup:main', Ember.ComponentLookup);
this.registry = registry;
this.container = registry.container();
} else {
this.container = Ember.Application.buildContainer(namespace);
this.container.register('component-lookup:main', Ember.ComponentLookup);
}
}

});

36 changes: 35 additions & 1 deletion tests/test-module-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ function moduleFor(fullName, description, callbacks) {

function setupRegistry() {
setResolverRegistry({
'component:x-foo': Ember.Component.extend()
'component:x-foo': Ember.Component.extend(),
'component:not-the-subject': Ember.Component.extend()
});
}

Expand Down Expand Up @@ -123,3 +124,36 @@ moduleFor('component:x-foo', 'component:x-foo -- uncreated subjects do not error
test("subject's created in a test are destroyed", function() {
expect(0);
});

moduleFor('component:x-foo', 'component:x-foo -- without needs or `integration: true`', {
beforeSetup: setupRegistry()
});

test("knows nothing about our non-subject component", function() {
var otherComponent = this.container.lookup('component:not-the-subject');
equal(null, otherComponent, "We shouldn't know about a non-subject component")
});

moduleFor('component:x-foo', 'component:x-foo -- when needing another component', {
beforeSetup: setupRegistry(),
needs: ['component:not-the-subject']
});

test("needs gets us the component we need", function() {
var otherComponent = this.container.lookup('component:not-the-subject');
ok(otherComponent, "another component can be resolved when it's in our needs array");
});

moduleFor('component:x-foo', 'component:x-foo -- `integration: true`', {
beforeSetup: function() {
setupRegistry()
ok(!this.callbacks.integration, "integration property should be removed from callbacks");
ok(this.isIntegration, "isIntegration should be set when we set `integration: true` in callbacks");
},
integration: true
});

test("needs is not needed (pun intended) when integration is true", function() {
var otherComponent = this.container.lookup('component:not-the-subject');
ok(otherComponent, 'another component can be resolved when integration is true');
});

0 comments on commit cdce5e8

Please sign in to comment.