Skip to content

Commit

Permalink
Use TestAdapter to ensure render errors are thrown
Browse files Browse the repository at this point in the history
Uses a customized test adapter for some tests that will rethrow errors
that occur during the render cycle. This fixes a few tests that were
false-negativing. Notes on the change to Ember are in emberjs/ember.js#14898

Fixes #128
  • Loading branch information
bantic committed Mar 13, 2017
1 parent 0c7bb50 commit dd69098
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
38 changes: 38 additions & 0 deletions tests/helpers/create-throwing-adapter.js
@@ -0,0 +1,38 @@
import Ember from 'ember';
import { QUnitAdapter } from 'ember-qunit';

/**
* Creates a TestAdapter that will throw when an exception is encountered.
* Before Ember 2.11, errors that occur in tests during `this.render` were thrown
* and catchable (and therefore testable using `assert.throws`), but
* in https://github.com/emberjs/ember.js/pull/14898 this changed and we need
* to override the Test Adapter's exception handler to rethrow the error.
*
* This file and any references can be removed if the Ember Test Adapter's
* error-handling behavior changes in the future.
*/

const ThrowingAdapter = QUnitAdapter.extend({
exception(error) {
// rethrow error
throw error;
}
});

export function setup(context) {
let origTestAdapter = Ember.Test.adapter;
context.__originalTestAdapter = origTestAdapter;

Ember.run(() => { Ember.Test.adapter = ThrowingAdapter.create(); });
}

export function teardown(context) {
if (context.__originalTestAdapter) {
Ember.run(() => {
context.__originalTestAdapter.destroy();
delete context.__originalTestAdapter;

Ember.Test.adapter = QUnitAdapter.create();
});
}
}
14 changes: 13 additions & 1 deletion tests/integration/components/mobiledoc-editor/component-test.js
Expand Up @@ -14,6 +14,10 @@ import {
} from '../../../helpers/create-mobiledoc';
import wait from 'ember-test-helpers/wait';
import $ from 'jquery';
import {
setup as setupThrowingAdapter,
teardown as teardownThrowingAdapter
} from '../../../helpers/create-throwing-adapter';

let { Component } = Ember;

Expand All @@ -40,6 +44,10 @@ moduleForComponent('mobiledoc-editor', 'Integration | Component | mobiledoc edit
this.registry.register(`template:components/${cardName}-editor`, editorTemplate);
return card;
};
},

afterEach() {
teardownThrowingAdapter(this);
}
});

Expand Down Expand Up @@ -750,6 +758,9 @@ test('`addCard` passes `payload`, breaks reference to original payload', functio
});

test('throws on unknown card when `unknownCardHandler` is not passed', function(assert) {
setupThrowingAdapter(this);

assert.expect(1);
this.set('mobiledoc', {
version: MOBILEDOC_VERSION,
cards: [
Expand All @@ -769,7 +780,7 @@ test('throws on unknown card when `unknownCardHandler` is not passed', function(
options=(hash unknownCardHandler=unknownCardHandler) as |editor|}}
{{/mobiledoc-editor}}
`);
}, /Unknown card "missing-card" found.*no unknownCardHandler/);
}, /Unknown card "missing-card".*no unknownCardHandler/);
});

test('calls `unknownCardHandler` when it renders an unknown card', function(assert) {
Expand Down Expand Up @@ -965,6 +976,7 @@ test(`sets ${COMPONENT_ATOM_EXPECTED_PROPS.join(',')} properties on atom compone
});

test('throws on unknown atom when `unknownAtomHandler` is not passed', function(assert) {
setupThrowingAdapter(this);
this.set('mobiledoc', {
version: MOBILEDOC_VERSION,
atoms: [
Expand Down

0 comments on commit dd69098

Please sign in to comment.