From 20869f00485f2a7508630a003a5d9d701b15c381 Mon Sep 17 00:00:00 2001 From: Ruslan Hrabovyi Date: Sun, 10 May 2020 01:45:13 +0300 Subject: [PATCH] define shape of the `run(`'s `this` --- addon-test-support/-private/action.js | 15 +++++++++------ addon-test-support/properties/blurrable.js | 2 +- addon-test-support/properties/click-on-text.js | 2 +- addon-test-support/properties/clickable.js | 2 +- addon-test-support/properties/fillable.js | 2 +- addon-test-support/properties/focusable.js | 2 +- addon-test-support/properties/triggerable.js | 2 +- addon-test-support/properties/visitable.js | 2 +- tests/unit/-private/action-test.js | 16 ++++++++++++++++ 9 files changed, 32 insertions(+), 13 deletions(-) diff --git a/addon-test-support/-private/action.js b/addon-test-support/-private/action.js index 53e46ca5..b89acfea 100644 --- a/addon-test-support/-private/action.js +++ b/addon-test-support/-private/action.js @@ -54,10 +54,13 @@ export default function action(query, cb) { * @param {Function} cb Some async activity callback * @returns {Ceibo} */ -export function run(node, query, cb) { +function run(node, query, cb) { const adapter = getExecutionContext(node); - adapter.query = query; - adapter.node = adapter.pageObjectNode; + const executionContext = Object.freeze({ + query, + node, + adapter + }); const chainedRoot = getRoot(node)._chainedTree; @@ -66,7 +69,7 @@ export function run(node, query, cb) { // chanined VS independent action invocations. Awaiting for the previous // action settlement, before invoke a new action, is a part of // the legacy testing helpers adapters for backward compat reasons - chainedRoot._promise = adapter.andThen(cb); + chainedRoot._promise = adapter.andThen.bind(executionContext)(cb); return node; } else if (!chainedRoot) { @@ -74,13 +77,13 @@ export function run(node, query, cb) { // we need to wait on its promise if it has one so the // previous invocations can resolve before we run ours. let root = getRoot(node) - root._promise = resolve(root._promise).then(() => cb(adapter)); + root._promise = resolve(root._promise).then(() => cb(executionContext)); return node; } else { // Store our invocation result on the chained root // so that chained calls can find it to wait on it. - chainedRoot._promise = cb(adapter); + chainedRoot._promise = cb(executionContext); return chainable(node); } diff --git a/addon-test-support/properties/blurrable.js b/addon-test-support/properties/blurrable.js index d5327320..1d5455ba 100644 --- a/addon-test-support/properties/blurrable.js +++ b/addon-test-support/properties/blurrable.js @@ -67,7 +67,7 @@ export function blurrable(selector = '', userOptions = {}) { return action(assign({}, userOptions, { selector }), function() { const element = findOne(this.node, this.query.selector, this.query); - return this.blur(element); + return this.adapter.blur(element); }); } diff --git a/addon-test-support/properties/click-on-text.js b/addon-test-support/properties/click-on-text.js index 2703e3ea..4699b2f2 100644 --- a/addon-test-support/properties/click-on-text.js +++ b/addon-test-support/properties/click-on-text.js @@ -100,6 +100,6 @@ export function clickOnText(scope, userOptions = {}) { const element = findOne(this.node, selector, this.query); - return this.click(element); + return this.adapter.click(element); }); } diff --git a/addon-test-support/properties/clickable.js b/addon-test-support/properties/clickable.js index 7b237827..9879da4f 100644 --- a/addon-test-support/properties/clickable.js +++ b/addon-test-support/properties/clickable.js @@ -67,6 +67,6 @@ export function clickable(selector, userOptions = {}) { return action(assign({}, userOptions, { selector }), function() { const element = findOne(this.node, this.query.selector, this.query); - return this.click(element); + return this.adapter.click(element); }); } diff --git a/addon-test-support/properties/fillable.js b/addon-test-support/properties/fillable.js index acdcd37d..29a4b5f4 100644 --- a/addon-test-support/properties/fillable.js +++ b/addon-test-support/properties/fillable.js @@ -134,7 +134,7 @@ export function fillable(selector = '', userOptions = {}) { const element = findOne(this.node, scopeSelector, this.query); - return this.fillIn(element, content); + return this.adapter.fillIn(element, content); }); } diff --git a/addon-test-support/properties/focusable.js b/addon-test-support/properties/focusable.js index c0df9e46..4b77fac3 100644 --- a/addon-test-support/properties/focusable.js +++ b/addon-test-support/properties/focusable.js @@ -69,6 +69,6 @@ export function focusable(selector = '', userOptions = {}) { return action(query, function() { const element = findOne(this.node, this.query.selector, this.query); - return this.focus(element); + return this.adapter.focus(element); }); } diff --git a/addon-test-support/properties/triggerable.js b/addon-test-support/properties/triggerable.js index 8b7d166e..f8ab69b5 100644 --- a/addon-test-support/properties/triggerable.js +++ b/addon-test-support/properties/triggerable.js @@ -88,7 +88,7 @@ export function triggerable(event, selector, userOptions = {}) { const element = findOne(this.node, this.query.selector, this.query); - return this.triggerEvent(element, event, mergedEventProperties); + return this.adapter.triggerEvent(element, event, mergedEventProperties); }); } diff --git a/addon-test-support/properties/visitable.js b/addon-test-support/properties/visitable.js index 79c18a2c..c53aa5af 100644 --- a/addon-test-support/properties/visitable.js +++ b/addon-test-support/properties/visitable.js @@ -96,6 +96,6 @@ export function visitable(path) { fullPath = appendQueryParams(fullPath, params); - return this.visit(fullPath); + return this.adapter.visit(fullPath); }); } diff --git a/tests/unit/-private/action-test.js b/tests/unit/-private/action-test.js index 62fc17e1..189245ce 100644 --- a/tests/unit/-private/action-test.js +++ b/tests/unit/-private/action-test.js @@ -85,6 +85,22 @@ if (require.has('@ember/test-helpers')) { assert.deepEqual(finished, [1]); }); + test('this is frozen', async function(assert) { + const p = create({ + scope: 'it works', + + run: action(function() { + executionContext = this; + }) + }); + + await p.run(); + + assert.throws(() => { + executionContext.test = 1; + }) + }); + test('it handles sync errors', async function(assert) { const p = create({ scope: '.Scope',