-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from ef4/module-for-integration
Adding standalone integration test support
- Loading branch information
Showing
3 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} | ||
} | ||
|
||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |