Skip to content

Commit

Permalink
Merge pull request #21 from ef4/module-for-integration
Browse files Browse the repository at this point in the history
Adding standalone integration test support
  • Loading branch information
rwjblue committed Mar 9, 2015
2 parents 98076ed + 9675d6a commit 17bb72a
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lib/ember-test-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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 @@ -13,6 +14,7 @@ export {
TestModule,
TestModuleForComponent,
TestModuleForModel,
TestModuleForIntegration,
getContext,
setContext,
setResolver
Expand Down
104 changes: 104 additions & 0 deletions lib/ember-test-helpers/test-module-for-integration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import Ember from 'ember';
import TestModule from './test-module';
import { getResolver } from './test-resolver';
import { getContext, setContext } from './test-context';

export default TestModule.extend({
init: function(name, description, callbacks) {
this._super.call(this, name, description, callbacks);
this.setupSteps.push(this.setupIntegrationHelpers);
this.teardownSteps.push(this.teardownView);
},

setupIntegrationHelpers: 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 (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;
};

},

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({
container: this.container,
factory: function() {},
dispatcher: null
});

this.context = getContext();
},

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();
});
}
}

});
48 changes: 48 additions & 0 deletions tests/test-module-for-integration-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Ember from 'ember';
import { TestModuleForIntegration } 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);
qunitModuleFor(module);
}

moduleForIntegration('Better Integration Tests', {
beforeSetup: function() {
setResolverRegistry({
'template:components/my-component': Ember.Handlebars.compile(
'<span>{{name}}</span>'
)
});
}
});

test('it can render a template', function() {
this.render("<span>Hello</span>");
equal(this.$('span').text(), 'Hello');
});

test('it can access the full container', function() {
this.set('myColor', 'red');
this.render('{{my-component name=myColor}}');
equal(this.$('span').text(), 'red');
this.set('myColor', 'blue');
equal(this.$('span').text(), 'blue');
});

test('it can handle actions', function() {
var handlerArg;
this.render('<button {{action "didFoo" 42}} />');
this.on('didFoo', function(thing) {
handlerArg = thing;
});
this.$('button').click();
equal(handlerArg, 42);
});

test('it accepts precompiled templates', function() {
this.render(Ember.Handlebars.compile("<span>Hello</span>"));
equal(this.$('span').text(), 'Hello');
});

0 comments on commit 17bb72a

Please sign in to comment.