Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding standalone integration test support #21

Merged
merged 4 commits into from
Mar 9, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
91 changes: 91 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,91 @@
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);
},

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

context.render = function(templateString) {
if (Ember.isArray(templateString)) {
templateString = templateString.join('');
}
self.view = Ember.View.create({
context: self,
controller: self,
template: Ember.Handlebars.compile(templateString),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should allow for a precompiled template to be passed in. I can't recall if Ember.Handlebars.compile properly handles a precompiled 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(self, key, value);
});
};

context.get = function(key) {
return Ember.get(self, 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));
}

});
43 changes: 43 additions & 0 deletions tests/test-module-for-integration-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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);
});