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

Possible fix for the error asserting issue #211

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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