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

Ensure view-registry is injected for unit tested components. #92

Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 27 additions & 7 deletions lib/ember-test-helpers/test-module-for-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,24 @@ export default TestModule.extend({
this.setupSteps.push(this.setupComponentIntegrationTest);
this.teardownSteps.push(this.teardownComponent);
}

if (Ember.View && Ember.View.views) {
this.setupSteps.push(this._aliasViewRegistry);
this.teardownSteps.push(this._resetViewRegistry);
}
},

_aliasViewRegistry: function() {
this._originalGlobalViewRegistry = Ember.View.views;
var viewRegistry = this.container.lookup('-view-registry:main');

if (viewRegistry) {
Ember.View.views = viewRegistry;
}
},

_resetViewRegistry: function() {
Ember.View.views = this._originalGlobalViewRegistry;
},

setupComponentUnitTest: function() {
Expand Down Expand Up @@ -100,12 +118,7 @@ export default TestModule.extend({
context.dispatcher.setup({}, '#ember-testing');
context.actions = module.actionHooks;

// This thing is registered as "view:" instead of "component:"
// because only views get the necessary _viewRegistry
// injection. Which is arguably an Ember bug, but doesn't impact
// normal app usage since apps always use a "view:" at the top
// level.
(this.registry || this.container).register('view:test-holder', Ember.Component.extend());
(this.registry || this.container).register('component:-test-holder', Ember.Component.extend());

context.render = function(template) {
if (!template) {
Expand All @@ -117,7 +130,7 @@ export default TestModule.extend({
if (typeof template === 'string') {
template = Ember.Handlebars.compile(template);
}
module.component = module.container.lookupFactory('view:test-holder').create({
module.component = module.container.lookupFactory('component:-test-holder').create({
layout: template
});

Expand Down Expand Up @@ -162,6 +175,13 @@ export default TestModule.extend({

setupContext: function() {
this._super.call(this);

// only setup the injection if we are running against a version
// of Ember that has `-view-registry:main` (Ember >= 1.12)
if (this.container.lookupFactory('-view-registry:main')) {
(this.registry || this.container).injection('component', '_viewRegistry', '-view-registry:main');
}

if (!this.isUnitTest) {
this.context.factory = function() {};
}
Expand Down
2 changes: 1 addition & 1 deletion lib/ember-test-helpers/test-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ export default Klass.extend({

// Ember 2.0.0 removed Ember.View as public API, so only do this when
// Ember.View is present
if (Ember.View) {
if (Ember.View && Ember.View.views) {
Ember.View.views = {};
}
},
Expand Down
25 changes: 23 additions & 2 deletions tests/test-module-for-component-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ var PrettyColor = Ember.Component.extend({
attributeBindings: ['style'],
style: function(){
return new Ember.Handlebars.SafeString('color: ' + this.get('name') + ';');
}.property('name')
});
}.property('name'),

click: function() {
ok(true, 'pretty-color was clicked');
}
});

var ColorController = Ember.Controller.extend({
hexa: function() {
Expand Down Expand Up @@ -211,6 +214,24 @@ test("className", function(){
// the assertion is in the willDestroyElement() hook of the component
});

moduleForComponent('pretty-color', 'component:pretty-color - event dispatching works in unit tests', {
unit: true,
beforeSetup: function() {
setupRegistry();
}
});

test('can handle click', function() {
expect(1); // assert in pretty-color `click` handler above

var component = this.subject();

this.render();

Ember.run(function() {
component.$().click();
});
});

moduleForComponent('changing-color', 'component:changing-color -- handles closure actions', {
integration: true
Expand Down