Skip to content

Commit

Permalink
ExecutionContext: Extract find logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ro0gr committed Nov 6, 2019
1 parent 74c9ac6 commit ebcb7f2
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 202 deletions.
51 changes: 7 additions & 44 deletions addon-test-support/-private/execution_context/acceptance.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import run from '../run';
import $ from '-jquery';
import {
guardMultiple,
buildSelector,
findClosestValue
} from '../helpers';
import {
fillElement,
assertFocusable
} from './helpers';
import {
ELEMENT_NOT_FOUND,
throwBetterError
} from '../better-errors';

export default function AcceptanceExecutionContext(pageObjectNode) {
this.pageObjectNode = pageObjectNode;
}

AcceptanceExecutionContext.prototype = {
get testContainer() {
// @todo: fix usage of private `_element`
return this.testContext ?
this.testContext._element :
'#ember-testing';
},

andThen(cb) {
return window.wait().then(() => {
cb(this);
Expand Down Expand Up @@ -66,39 +64,4 @@ AcceptanceExecutionContext.prototype = {

$(element).blur();
},

find(selector, options) {
let result;

selector = buildSelector(this.pageObjectNode, selector, options);

/* global find */
result = find(selector, options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer'));

guardMultiple(result, selector, options.multiple);

return result;
},

findWithAssert(selector, options) {
let result;

selector = buildSelector(this.pageObjectNode, selector, options);

/* global find */
result = find(selector, options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer'));

if (result.length === 0) {
throwBetterError(
this.pageObjectNode,
options.pageObjectKey,
ELEMENT_NOT_FOUND,
{ selector }
);
}

guardMultiple(result, selector, options.multiple);

return result;
}
};
59 changes: 7 additions & 52 deletions addon-test-support/-private/execution_context/integration.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import $ from '-jquery';
import { run as emberRunloopRun } from '@ember/runloop';
import run from '../run';
import {
guardMultiple,
buildSelector,
findClosestValue
} from '../helpers';
import {
fillElement,
assertFocusable
} from './helpers';
import {
ELEMENT_NOT_FOUND,
throwBetterError
} from '../better-errors';
import wait from 'ember-test-helpers/wait';

export default function IntegrationExecutionContext(pageObjectNode, testContext) {
Expand All @@ -22,6 +13,13 @@ export default function IntegrationExecutionContext(pageObjectNode, testContext)
}

IntegrationExecutionContext.prototype = {
get testContainer() {
// @todo: fix usage of private `_element`
return this.testContext ?
this.testContext._element :
'#ember-testing';
},

andThen(cb) {
emberRunloopRun(() => {
cb(this)
Expand Down Expand Up @@ -64,47 +62,4 @@ IntegrationExecutionContext.prototype = {

$(element).blur();
},

find(selector, options) {
let result;
let container = options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer');

selector = buildSelector(this.pageObjectNode, selector, options);

if (container) {
result = $(selector, container);
} else {
result = this.testContext.$(selector);
}

guardMultiple(result, selector, options.multiple);

return result;
},

findWithAssert(selector, options) {
let result;
let container = options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer');

selector = buildSelector(this.pageObjectNode, selector, options);

if (container) {
result = $(selector, container);
} else {
result = this.testContext.$(selector);
}

guardMultiple(result, selector, options.multiple);

if (result.length === 0) {
throwBetterError(
this.pageObjectNode,
options.pageObjectKey,
ELEMENT_NOT_FOUND,
{ selector }
);
}

return result;
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import $ from '-jquery';

import {
click,
triggerEvent,
Expand All @@ -9,19 +7,10 @@ import {
} from 'ember-native-dom-helpers';

import run from '../run';
import {
guardMultiple,
buildSelector,
findClosestValue
} from '../helpers';
import {
fillElement,
assertFocusable
} from './helpers';
import {
ELEMENT_NOT_FOUND,
throwBetterError
} from '../better-errors';

const KEYBOARD_EVENT_TYPES = ['keydown', 'keypress', 'keyup'];

Expand All @@ -31,6 +20,13 @@ export default function ExecutionContext(pageObjectNode, testContext) {
}

ExecutionContext.prototype = {
get testContainer() {
// @todo: fix usage of private `_element`
return this.testContext ?
this.testContext._element :
'#ember-testing';
},

runAsync(cb) {
return run(this.pageObjectNode, cb);
},
Expand All @@ -46,20 +42,6 @@ ExecutionContext.prototype = {
triggerEvent(element, 'change');
},

$(selector, container) {
if (container) {
return $(selector, container);
} else {
// @todo: we should fixed usage of private `_element`
// after https://github.com/emberjs/ember-test-helpers/issues/184 is resolved
let testsContainer = this.testContext ?
this.testContext._element :
'#ember-testing';

return $(selector, testsContainer);
}
},

triggerEvent(element, eventName, eventOptions) {
// `keyCode` is a deprecated property.
// @see: https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode
Expand Down Expand Up @@ -87,38 +69,5 @@ ExecutionContext.prototype = {

blur(element);
},

find(selector, options) {
let container = options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer');

selector = buildSelector(this.pageObjectNode, selector, options);

let result = this.$(selector, container);

guardMultiple(result, selector, options.multiple);

return result;
},

findWithAssert(selector, options) {
let container = options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer');

selector = buildSelector(this.pageObjectNode, selector, options);

let result = this.$(selector, container);

if (result.length === 0) {
throwBetterError(
this.pageObjectNode,
options.pageObjectKey,
ELEMENT_NOT_FOUND,
{ selector }
);
}

guardMultiple(result, selector, options.multiple);

return result;
}
};

50 changes: 4 additions & 46 deletions addon-test-support/-private/execution_context/rfc268.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import $ from '-jquery';
import run from '../run';
import {
guardMultiple,
buildSelector,
findClosestValue,
} from '../helpers';
import {
getRootElement,
visit,
Expand All @@ -15,16 +9,16 @@ import {
focus,
blur
} from '../compatibility';
import {
ELEMENT_NOT_FOUND,
throwBetterError
} from '../better-errors';

export default function ExecutionContext(pageObjectNode) {
this.pageObjectNode = pageObjectNode;
}

ExecutionContext.prototype = {
get testContainer() {
return getRootElement();
},

runAsync(cb) {
return run(this.pageObjectNode, cb);
},
Expand Down Expand Up @@ -58,40 +52,4 @@ ExecutionContext.prototype = {
blur(element) {
return blur(element);
},

find(selector, options) {
selector = buildSelector(this.pageObjectNode, selector, options);
let result = this.getElements(selector, options);

guardMultiple(result, selector, options.multiple);

return result;
},

findWithAssert(selector, options) {
selector = buildSelector(this.pageObjectNode, selector, options);
let result = this.getElements(selector, options);

guardMultiple(result, selector, options.multiple);

if (result.length === 0) {
throwBetterError(
this.pageObjectNode,
options.pageObjectKey,
ELEMENT_NOT_FOUND,
{ selector }
);
}

return result;
},

getElements(selector, options) {
let container = options.testContainer || findClosestValue(this.pageObjectNode, 'testContainer');
if (container) {
return $(selector, container);
} else {
return $(selector, getRootElement());
}
}
};
32 changes: 31 additions & 1 deletion addon-test-support/extend/find-element-with-assert.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import $ from '-jquery';
import {
guardMultiple,
buildSelector,
findClosestValue,
} from '../-private/helpers';
import {
ELEMENT_NOT_FOUND,
throwBetterError
} from '../-private/better-errors';

import { getExecutionContext } from '../-private/execution_context';

/**
Expand Down Expand Up @@ -36,5 +47,24 @@ import { getExecutionContext } from '../-private/execution_context';
* @throws Will throw an error if multiple elements are matched by selector and multiple option is not set
*/
export function findElementWithAssert(pageObjectNode, targetSelector, options = {}) {
return getExecutionContext(pageObjectNode).findWithAssert(targetSelector, options);
const selector = buildSelector(pageObjectNode, targetSelector, options);

const container = options.testContainer
|| findClosestValue(pageObjectNode, 'testContainer')
|| getExecutionContext(pageObjectNode).testContainer;

const result = $(selector, container);

guardMultiple(result, selector, options.multiple);

if (result.length === 0) {
throwBetterError(
pageObjectNode,
options.pageObjectKey,
ELEMENT_NOT_FOUND,
{ selector }
);
}

return result;
}
18 changes: 17 additions & 1 deletion addon-test-support/extend/find-element.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import $ from '-jquery';
import {
guardMultiple,
buildSelector,
findClosestValue
} from '../-private/helpers';

import { getExecutionContext } from '../-private/execution_context';

/**
Expand Down Expand Up @@ -34,5 +41,14 @@ import { getExecutionContext } from '../-private/execution_context';
* @throws Will throw an error if multiple elements are matched by selector and multiple option is not set
*/
export function findElement(pageObjectNode, targetSelector, options = {}) {
return getExecutionContext(pageObjectNode).find(targetSelector, options);
const selector = buildSelector(pageObjectNode, targetSelector, options);
const container = options.testContainer
|| findClosestValue(pageObjectNode, 'testContainer')
|| getExecutionContext(pageObjectNode).testContainer;

const result = $(selector, container);

guardMultiple(result, selector, options.multiple);

return result;
}

0 comments on commit ebcb7f2

Please sign in to comment.