Skip to content

Commit

Permalink
ExecurionContext: focus/blur
Browse files Browse the repository at this point in the history
  • Loading branch information
ro0gr committed Oct 29, 2019
1 parent 3015c73 commit f7768e2
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 77 deletions.
26 changes: 8 additions & 18 deletions addon-test-support/-private/execution_context/acceptance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import run from '../run';
import $ from '-jquery';
import {
guardMultiple,
buildSelector,
Expand Down Expand Up @@ -37,6 +38,7 @@ AcceptanceExecutionContext.prototype = {
/* global click */
click(element);
},

fillIn(element, content) {
/* global focus */
focus(element);
Expand All @@ -53,28 +55,16 @@ AcceptanceExecutionContext.prototype = {
triggerEvent(element, eventName, eventOptions);
},

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

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

$selection.focus();
$(element).focus();
},

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

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

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

find(selector, options) {
Expand Down
11 changes: 2 additions & 9 deletions addon-test-support/-private/execution_context/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import $ from '-jquery';
import { throwBetterError } from '../better-errors';

/**
* @private
Expand Down Expand Up @@ -35,7 +34,7 @@ export function fillElement(selection, content) {
*
* @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 @@ -51,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}`);
}
}
24 changes: 6 additions & 18 deletions addon-test-support/-private/execution_context/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,28 +61,16 @@ IntegrationExecutionContext.prototype = {
$(element).trigger(event);
},

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

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

$selection.focus();
$(element).focus();
},

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

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

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

find(selector, options) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,26 +76,14 @@ ExecutionContext.prototype = {
}
},

focus(selector, options) {
const element = this.findWithAssert(selector, options)[0];

assertFocusable(element, {
selector,
pageObjectNode: this.pageObjectNode,
pageObjectKey: options.pageObjectKey
});
focus(element) {
assertFocusable(element);

focus(element);
},

blur(selector, options) {
const element = this.findWithAssert(selector, options)[0];

assertFocusable(element, {
selector,
pageObjectNode: this.pageObjectNode,
pageObjectKey: options.pageObjectKey
});
blur(element) {
assertFocusable(element);

blur(element);
},
Expand Down
10 changes: 4 additions & 6 deletions addon-test-support/-private/execution_context/rfc268.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ ExecutionContext.prototype = {
return triggerEvent(element, eventName, eventOptions);
},

focus(selector, options) {
selector = buildSelector(this.pageObjectNode, selector, options);
return this.invokeHelper(selector, options, focus);
focus(element) {
return focus(element);
},

blur(selector, options) {
selector = buildSelector(this.pageObjectNode, selector, options);
return this.invokeHelper(selector, options, blur);
blur(element) {
return blur(element);
},

find(selector, options) {
Expand Down
12 changes: 7 additions & 5 deletions addon-test-support/properties/blurrable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import run from '../-private/run';
import { assign } from '../-private/helpers';
import { getExecutionContext } from '../-private/execution_context';
import { action } from '../extend/index';

/**
*
Expand Down Expand Up @@ -68,11 +69,12 @@ export function blurrable(selector, userOptions = {}) {

get(key) {
return function() {
const executionContext = getExecutionContext(this);
const options = assign({ pageObjectKey: `${key}()` }, userOptions);
const query = assign({
pageObjectKey: `${key}()`,
}, userOptions);

return executionContext.runAsync((context) => {
return context.blur(selector, options);
return run(this, ({ blur }) => {
return action(this, selector, query, blur);
});
};
}
Expand Down
12 changes: 7 additions & 5 deletions addon-test-support/properties/focusable.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import run from '../-private/run';
import { assign } from '../-private/helpers';
import { getExecutionContext } from '../-private/execution_context';
import { action } from '../extend/index';

/**
*
Expand Down Expand Up @@ -68,11 +69,12 @@ export function focusable(selector, userOptions = {}) {

get(key) {
return function() {
const executionContext = getExecutionContext(this);
const options = assign({ pageObjectKey: `${key}()` }, userOptions);
const query = assign({
pageObjectKey: `${key}()`,
}, userOptions);

return executionContext.runAsync((context) => {
return context.focus(selector, options);
return run(this, ({ focus }) => {
return action(this, selector, query, focus);
});
};
}
Expand Down

0 comments on commit f7768e2

Please sign in to comment.