Skip to content

Commit

Permalink
(f) A possible fix for the issue emberjs/ember.js#15013
Browse files Browse the repository at this point in the history
  • Loading branch information
ocrest committed Aug 10, 2017
1 parent 253d2f5 commit 264ef87
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 18 additions & 0 deletions lib/ember-test-helpers/test-module-for-component.js
Expand Up @@ -238,6 +238,14 @@ export function setupComponentIntegrationTest() {
outletState = { render: stateToRender, outlets: {} };
}

// A hacky fix for the issue https://github.com/emberjs/ember.js/issues/15013 — listen
// for errors on the global window object and save them in order to rethrow later.
var uncaughtErrors = [];
var originalOnErrorHandler = window.onerror;
window.onerror = function windowErrorsHandler(error) {
uncaughtErrors.push(error);
};

Ember.run(() => {
toplevelView.setOutletState(outletState);
});
Expand All @@ -247,6 +255,16 @@ export function setupComponentIntegrationTest() {
hasRendered = true;
}

if (uncaughtErrors.length > 0) {
// Rethrow any errors caught on the window to a test framework after the component
// was rendered:
uncaughtErrors.forEach(function throwWindowError(error) {
throw error;
});
}
// Restore global onerror handler:
window.onerror = originalOnErrorHandler;

// ensure the element is based on the wrapping toplevel view
// Ember still wraps the main application template with a
// normal tagged view
Expand Down
26 changes: 25 additions & 1 deletion tests/test-module-for-component-test.js
Expand Up @@ -62,6 +62,22 @@ var ChangingColor = Ember.Component.extend({
}
});

// A component which throws an error during render:
var BadComponent = Ember.Component.extend({
layout: Ember.Handlebars.compile('<input type="text" onfocus=(action "focus")>'),
didInsertElement() {
this.set('input', this.element.querySelector('input'));
},
didRender() {
this.get('input').focus();
},
actions: {
focus() {
this.get('unexistedMethod')();
}
}
});

function setupRegistry() {
setResolverRegistry({
'component:x-foo': Ember.Component.extend(),
Expand Down Expand Up @@ -373,7 +389,8 @@ moduleForComponent('Component Integration Tests', {
setResolverRegistry({
'template:components/my-component': Ember.Handlebars.compile(
'<span>{{name}}</span>'
)
),
'component:bad-component': BadComponent
});
}
});
Expand All @@ -395,6 +412,13 @@ if (hasEmberVersion(1,11)) {
});
}

test('it correctly handles uncaught errors when rendering a component', function() {
var self = this;
throws(function() {
self.render('{{bad-component}}');
});
});

test('it complains if you try to use bare render', function() {
var self = this;
throws(function() {
Expand Down

0 comments on commit 264ef87

Please sign in to comment.