From 3d6cd1ba5364273bb05d663c159b6837b3dc4441 Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Sun, 8 Mar 2015 23:02:01 -0400 Subject: [PATCH 1/4] Adding moduleForIntegration --- lib/ember-test-helpers.js | 2 + .../test-module-for-integration.js | 91 +++++++++++++++++++ tests/test-module-for-integration-test.js | 43 +++++++++ 3 files changed, 136 insertions(+) create mode 100644 lib/ember-test-helpers/test-module-for-integration.js create mode 100644 tests/test-module-for-integration-test.js diff --git a/lib/ember-test-helpers.js b/lib/ember-test-helpers.js index a3bd97390..8f8409f9d 100644 --- a/lib/ember-test-helpers.js +++ b/lib/ember-test-helpers.js @@ -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'; @@ -13,6 +14,7 @@ export { TestModule, TestModuleForComponent, TestModuleForModel, + TestModuleForIntegration, getContext, setContext, setResolver diff --git a/lib/ember-test-helpers/test-module-for-integration.js b/lib/ember-test-helpers/test-module-for-integration.js new file mode 100644 index 000000000..4da6181b0 --- /dev/null +++ b/lib/ember-test-helpers/test-module-for-integration.js @@ -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), + 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)); + } + +}); diff --git a/tests/test-module-for-integration-test.js b/tests/test-module-for-integration-test.js new file mode 100644 index 000000000..40ac59a1b --- /dev/null +++ b/tests/test-module-for-integration-test.js @@ -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( + '{{name}}' + ) + }); + } +}); + +test('it can render a template', function() { + this.render("Hello"); + 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('