Skip to content

Commit

Permalink
support integration-mode component tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Apr 8, 2015
1 parent 532da8c commit 87b4972
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 101 deletions.
2 changes: 0 additions & 2 deletions lib/ember-test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import isolatedContainer from 'ember-test-helpers/isolated-container';
import TestModule from 'ember-test-helpers/test-module';
import TestModuleForComponent from 'ember-test-helpers/test-module-for-component';
import TestModuleForModel from 'ember-test-helpers/test-module-for-model';
import TestModuleForIntegration from 'ember-test-helpers/test-module-for-integration';
import { getContext, setContext } from 'ember-test-helpers/test-context';
import { setResolver } from 'ember-test-helpers/test-resolver';

Expand All @@ -14,7 +13,6 @@ export {
TestModule,
TestModuleForComponent,
TestModuleForModel,
TestModuleForIntegration,
getContext,
setContext,
setResolver
Expand Down
8 changes: 6 additions & 2 deletions lib/ember-test-helpers/isolated-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ function exposeRegistryMethodsWithoutDeprecations(container) {
export default function isolatedContainer(fullNames) {
var resolver = getResolver();
var container;
var registry;

var normalize = function(fullName) {
return resolver.normalize(fullName);
};

if (Ember.Registry) {
var registry = new Ember.Registry();
registry = new Ember.Registry();
registry.normalizeFullName = normalize;

container = registry.container();
Expand Down Expand Up @@ -83,5 +84,8 @@ export default function isolatedContainer(fullNames) {
var normalizedFullName = resolver.normalize(fullName);
container.register(fullName, resolver.resolve(normalizedFullName));
}
return container;
return {
container: container,
registry: registry
};
}
105 changes: 102 additions & 3 deletions lib/ember-test-helpers/test-module-for-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,38 @@ import { getResolver } from './test-resolver';

export default TestModule.extend({
init: function(componentName, description, callbacks) {
// Allow `description` to be omitted
if (!callbacks && typeof description === 'object') {
callbacks = description || {};
description = null;
}

this.componentName = componentName;
this.isUnitTest = !!(callbacks.needs || callbacks.unit) ||
callbacks.integration === false;

this._super.call(this, 'component:' + componentName, description, callbacks);
if (!this.isUnitTest) {
callbacks.integration = true;
}

if (description) {
this._super.call(this, 'component:' + componentName, description, callbacks);
} else {
this._super.call(this, 'component:' + componentName, callbacks);
}

this.setupSteps.push(this.setupComponent);
if (this.isUnitTest) {
this.setupSteps.push(this.setupComponentUnitTest);
} else {
this.callbacks.subject = function() {
throw new Error("component integration tests do not support `subject()`.");
};
this.setupSteps.push(this.setupComponentIntegrationTest);
this.teardownSteps.push(this.teardownView);
}
},

setupComponent: function() {
setupComponentUnitTest: function() {
var _this = this;
var resolver = getResolver();
var container = this.container;
Expand Down Expand Up @@ -55,5 +79,80 @@ export default TestModule.extend({

return subject.$.apply(subject, arguments);
};
},

setupComponentIntegrationTest: function() {
var self = this;
var context = this.context;
context.dispatcher = Ember.EventDispatcher.create();
context.dispatcher.setup({}, '#ember-testing');
this.actionHooks = {};

context.render = function(template) {
if (!template) {
throw new Error("in a component integration test you must pass a template to `render()`");
}
if (Ember.isArray(template)) {
template = template.join('');
}
if (typeof template === 'string') {
template = Ember.Handlebars.compile(template);
}
self.view = Ember.View.create({
context: context,
controller: self,
template: template,
container: self.container
});
Ember.run(function() {
self.view.appendTo('#ember-testing');
});
};

context.$ = function() {
return self.view.$.apply(self.view, arguments);
};

context.set = function(key, value) {
Ember.run(function() {
Ember.set(context, key, value);
});
};

context.get = function(key) {
return Ember.get(context, key);
};

context.on = function(actionName, handler) {
self.actionHooks[actionName] = handler;
};

},

setupContext: function() {
this._super.call(this);
if (!this.isUnitTest) {
this.context.factory = function() {};
}
},


send: function(actionName) {
var hook = this.actionHooks[actionName];
if (!hook) {
throw new Error("integration testing template received unexpected action " + actionName);
}
hook.apply(this, Array.prototype.slice.call(arguments, 1));
},

teardownView: function() {
var view = this.view;
if (view) {
Ember.run(function() {
view.destroy();
});
}
}


});
89 changes: 0 additions & 89 deletions lib/ember-test-helpers/test-module-for-integration.js

This file was deleted.

5 changes: 4 additions & 1 deletion lib/ember-test-helpers/test-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export default Klass.extend({

setContext({
container: this.container,
registry: this.registry,
factory: factory,
dispatcher: null
});
Expand Down Expand Up @@ -213,7 +214,9 @@ export default Klass.extend({


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

_setupIntegratedContainer: function() {
Expand Down
3 changes: 3 additions & 0 deletions tests/test-module-for-component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ test('clears out views from test to test', function() {
///////////////////////////////////////////////////////////////////////////////

moduleForComponent('pretty-color', {
unit: true,
beforeSetup: function() {
setupRegistry();
}
Expand Down Expand Up @@ -215,6 +216,7 @@ test("$", function(){
});

moduleForComponent('pretty-color', 'component:pretty-color -- this.render in setup', {
unit: true,
beforeSetup: function() {
setupRegistry();
},
Expand All @@ -236,6 +238,7 @@ test("className", function(){
});

moduleForComponent('boring-color', 'component:boring-color -- still in DOM in willDestroyElement', {
unit: true,
beforeSetup: function() {
setupRegistry();
},
Expand Down
24 changes: 20 additions & 4 deletions tests/test-module-for-integration-test.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import Ember from 'ember';
import { TestModuleForIntegration } from 'ember-test-helpers';
import { TestModuleForComponent } from 'ember-test-helpers';
import test from 'tests/test-support/qunit-test';
import qunitModuleFor from 'tests/test-support/qunit-module-for';
import { setResolverRegistry } from 'tests/test-support/resolver';

function moduleForIntegration(name, description, callbacks) {
var module = new TestModuleForIntegration(name, description, callbacks);
function moduleForComponent(name, description, callbacks) {
var module = new TestModuleForComponent(name, description, callbacks);
qunitModuleFor(module);
}

moduleForIntegration('Better Integration Tests', {

moduleForComponent('Component Integration Tests', {
beforeSetup: function() {
setResolverRegistry({
'template:components/my-component': Ember.Handlebars.compile(
Expand All @@ -24,6 +25,21 @@ test('it can render a template', function() {
equal(this.$('span').text(), 'Hello');
});

test('it complains if you try to use bare render', function() {
var self = this;
throws(function() {
self.render();
}, /in a component integration test you must pass a template to `render\(\)`/);
});

test('it complains if you try to use subject()', function() {
var self = this;
throws(function() {
self.subject();
}, /component integration tests do not support `subject\(\)`\./);
});


test('it can access the full container', function() {
this.set('myColor', 'red');
this.render('{{my-component name=myColor}}');
Expand Down

0 comments on commit 87b4972

Please sign in to comment.