Skip to content

Commit

Permalink
setup-rendering-context: Protect element and context methods from o…
Browse files Browse the repository at this point in the history
…verwriting
  • Loading branch information
Turbo87 committed Mar 26, 2018
1 parent 6aa7b9d commit c3a0bb1
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
32 changes: 17 additions & 15 deletions addon-test-support/@ember/test-helpers/setup-rendering-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,11 @@ export default function setupRenderingContext(context) {
// these methods being placed on the context itself will be deprecated in
// a future version (no giant rush) to remove some confusion about which
// is the "right" way to things...
context.render = render;
context.clearRender = clearRender;
Object.defineProperty(context, 'render', { value: render, writable: false });
Object.defineProperty(context, 'clearRender', { value:clearRender, writable: false });

if (global.jQuery) {
context.$ = jQuerySelector;
Object.defineProperty(context, '$', { value: jQuerySelector, writable: false });
}

// When the host app uses `setApplication` (instead of `setResolver`) the event dispatcher has
Expand Down Expand Up @@ -190,18 +190,20 @@ export default function setupRenderingContext(context) {
});
})
.then(() => {
// ensure the element is based on the wrapping toplevel view
// Ember still wraps the main application template with a
// normal tagged view
//
// In older Ember versions (2.4) the element itself is not stable,
// and therefore we cannot update the `this.element` until after the
// rendering is completed
if (EmberENV._APPLICATION_TEMPLATE_WRAPPER !== false) {
context.element = getRootElement().querySelector('.ember-view');
} else {
context.element = getRootElement();
}
Object.defineProperty(context, 'element', {
// ensure the element is based on the wrapping toplevel view
// Ember still wraps the main application template with a
// normal tagged view
//
// In older Ember versions (2.4) the element itself is not stable,
// and therefore we cannot update the `this.element` until after the
// rendering is completed
value: EmberENV._APPLICATION_TEMPLATE_WRAPPER !== false
? getRootElement().querySelector('.ember-view')
: getRootElement(),

writable: false,
});

return context;
});
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/setup-rendering-context-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ module('setupRenderingContext', function(hooks) {
setResolver(resolver);
});

function overwriteTest(key) {
test(`throws an error when trying to overwrite this.${key}`, function(assert) {
assert.throws(() => {
this[key] = null;
}, TypeError);
});
}

function setupRenderingContextTests(hooks) {
hooks.beforeEach(async function() {
setResolverRegistry({
Expand Down Expand Up @@ -72,6 +80,8 @@ module('setupRenderingContext', function(hooks) {
});
}

overwriteTest('element');

test('render can be used multiple times', async function(assert) {
await this.render(hbs`<p>Hello!</p>`);
assert.equal(this.element.textContent, 'Hello!');
Expand Down Expand Up @@ -108,6 +118,9 @@ module('setupRenderingContext', function(hooks) {
assert.strictEqual(this.element, originalElement, 'this.element is stable');
});

overwriteTest('render');
overwriteTest('clearRender');

(hasjQuery() ? test : skip)('this.$ is exposed when jQuery is present', async function(assert) {
await this.render(hbs`<p>Hello!</p>`);

Expand Down

0 comments on commit c3a0bb1

Please sign in to comment.