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

Prepare for adapters #493

Closed
wants to merge 8 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion addon-test-support/-private/action.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { resolve } from 'rsvp';
import Ceibo from 'ceibo';
import { getExecutionContext } from './execution_context';
import { getRoot } from './helpers';
import Ceibo from 'ceibo';
import { findOne } from '../extend';
import { throwBetterError } from './better-errors';

export function invokeHelper(node, selector, query, cb) {
const element = findOne(node, selector, query);

let res;
try {
res = cb(element);
} catch(e) {
throwBetterError(node, query.pageObjectKey, e, { selector })
}

return resolve(res).catch((e) => {
throwBetterError(node, query.pageObjectKey, e, { selector })
});
}

/**
* Run action
Expand Down
22 changes: 15 additions & 7 deletions addon-test-support/-private/better-errors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import EmberError from '@ember/error';
import Ceibo from 'ceibo';

export const ELEMENT_NOT_FOUND = 'Element not found.';
Expand All @@ -8,27 +7,36 @@ export const ELEMENT_NOT_FOUND = 'Element not found.';
*
* @param {Ceibo} node PageObject node containing the property that triggered the error
* @param {string} key Key of PageObject property tht triggered the error
* @param {string} msg Error message
* @param {string} error Error message or Error instance
* @param {Object} options
* @param {string} options.selector Selector of element targeted by PageObject property
* @return {Ember.Error}
*/
export function throwBetterError(node, key, msg, { selector } = {}) {
export function throwBetterError(node, key, error, { selector } = {}) {
let path = [key];
let current;

let fullErrorMessage = error instanceof Error ? error.message : error.toString();

for (current = node; current; current = Ceibo.parent(current)) {
path.unshift(Ceibo.meta(current).key);
}

path[0] = 'page';

let fullErrorMessage = `${msg}\n\nPageObject: '${path.join('.')}'`;
if (path.length > 0) {
fullErrorMessage += `\n\nPageObject: '${path.join('.')}'`;
}

if (selector) {
if (typeof selector === 'string' && selector.trim().length > 0) {
fullErrorMessage = `${fullErrorMessage}\n Selector: '${selector}'`;
}

console.error(fullErrorMessage);
throw new EmberError(fullErrorMessage);
const err = new Error(fullErrorMessage);
if (error instanceof Error && 'stack' in error) {
err.stack = error.stack;
}

console.error(err.message);
throw err;
}
94 changes: 16 additions & 78 deletions addon-test-support/-private/execution_context/acceptance.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
import $ from '-jquery';
import { run } from '../action';
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;
Expand Down Expand Up @@ -40,90 +32,36 @@ AcceptanceExecutionContext.prototype = {
visit(path);
},

click(selector, container) {
click(element) {
/* global click */
click(selector, container);
click(element);
},

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

fillIn(element, content) {
/* global focus */
focus($selection);
focus(element);

fillElement($selection, content, {
selector,
pageObjectNode: this.pageObjectNode,
pageObjectKey: options.pageObjectKey
});
fillElement(element, content);

/* global triggerEvent */
triggerEvent(selector, container, 'input');
triggerEvent(selector, container, 'change');
triggerEvent(element, 'input');
triggerEvent(element, 'change');
},

triggerEvent(selector, container, options, eventName, eventOptions) {
triggerEvent(element, eventName, eventOptions) {
/* global triggerEvent */
triggerEvent(selector, container, eventName, eventOptions);
},

focus(selector, options) {
let $selection = this.findWithAssert(selector, options);

assertFocusable($selection[0], {
selector,
pageObjectNode: this.pageObjectNode,
pageObjectKey: options.pageObjectKey
});

$selection.focus();
triggerEvent(element, eventName, eventOptions);
},

blur(selector, options) {
let $selection = this.findWithAssert(selector, options);
focus(element) {
assertFocusable(element);

assertFocusable($selection[0], {
selector,
pageObjectNode: this.pageObjectNode,
pageObjectKey: options.pageObjectKey
});

$selection.blur();
},

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

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

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);
blur(element) {
assertFocusable(element);

return result;
$(element).blur();
}
};
24 changes: 4 additions & 20 deletions addon-test-support/-private/execution_context/helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
import {
throwBetterError
} from '../better-errors';

import $ from '-jquery';

/**
Expand All @@ -19,19 +15,13 @@ import $ from '-jquery';
*
* @throws Will throw an error if called on a contenteditable element that has `contenteditable="false"`
*/
export function fillElement(selection, content, { selector, pageObjectNode, pageObjectKey }) {
export function fillElement(selection, content) {
const $selection = $(selection);

if ($selection.is('[contenteditable][contenteditable!="false"]')) {
$selection.html(content);
} else if ($selection.is('[contenteditable="false"]')) {
throwBetterError(
pageObjectNode,
pageObjectKey,
'Element cannot be filled because it has `contenteditable="false"`.', {
selector
}
);
throw new Error('Element cannot be filled because it has `contenteditable="false"`.');
} else {
$selection.val(content);
}
Expand All @@ -44,7 +34,7 @@ export function fillElement(selection, content, { selector, pageObjectNode, page
*
* @param {Element} element - the element to check
*/
export function assertFocusable(element, { selector, pageObjectNode, pageObjectKey }) {
export function assertFocusable(element) {
let $element = $(element);

let error;
Expand All @@ -60,12 +50,6 @@ export function assertFocusable(element, { selector, pageObjectNode, pageObjectK
}

if (error) {
throwBetterError(
pageObjectNode,
pageObjectKey,
`Element is not focusable because it is ${error}`, {
selector
}
);
throw new Error(`Element is not focusable because it is ${error}`);
}
}
105 changes: 14 additions & 91 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 } from '@ember/runloop';
import { run as runAction } from '../action';
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 Down Expand Up @@ -43,21 +34,15 @@ IntegrationExecutionContext.prototype = {

visit() {},

click(selector, container) {
this.$(selector, container).click();
click(element) {
$(element).click();
},

fillIn(selector, container, options, content) {
let $selection = this.$(selector, container);

fillElement($selection, content, {
selector,
pageObjectNode: this.pageObjectNode,
pageObjectKey: options.pageObjectKey
});
fillIn(element, content) {
fillElement(element, content);

$selection.trigger('input');
$selection.change();
$(element).trigger('input');
$(element).change();
},

$(selector, container) {
Expand All @@ -68,83 +53,21 @@ IntegrationExecutionContext.prototype = {
}
},

triggerEvent(selector, container, options, eventName, eventOptions) {
triggerEvent(element, eventName, eventOptions) {
let event = $.Event(eventName, eventOptions);

if (container) {
$(selector, container).trigger(event);
} else {
this.testContext.$(selector).trigger(event);
}
},

focus(selector, options) {
let $selection = this.findWithAssert(selector, options);

assertFocusable($selection[0], {
selector,
pageObjectNode: this.pageObjectNode,
pageObjectKey: options.pageObjectKey
});

$selection.focus();
},

blur(selector, options) {
let $selection = this.findWithAssert(selector, options);

assertFocusable($selection[0], {
selector,
pageObjectNode: this.pageObjectNode,
pageObjectKey: options.pageObjectKey
});

$selection.blur();
$(element).trigger(event);
},

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

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

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

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 }
);
}
blur(element) {
assertFocusable(element);

return result;
$(element).blur();
}
};
Loading