Skip to content

Commit

Permalink
Merge pull request #505 from ro0gr/no-multiple
Browse files Browse the repository at this point in the history
v2: remove "multiple" option support
  • Loading branch information
ro0gr authored May 4, 2020
2 parents b9c2cd9 + 5cdc55e commit b6787fd
Show file tree
Hide file tree
Showing 14 changed files with 50 additions and 597 deletions.
20 changes: 0 additions & 20 deletions addon-test-support/-private/helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export { assign } from '@ember/polyfills';
import { A } from '@ember/array';
import { assert } from '@ember/debug';
import { get } from '@ember/object';
import { isPresent } from '@ember/utils';
Expand Down Expand Up @@ -146,25 +145,6 @@ export function buildSelector(node, targetSelector, options) {
return (new Selector(node, options.scope, targetSelector, options)).toString();
}

/**
* @private
*
* Trim whitespaces at both ends and normalize whitespaces inside `text`
*
* Due to variations in the HTML parsers in different browsers, the text
* returned may vary in newlines and other white space.
*
* @see http://api.jquery.com/text/
*/
export function normalizeText(text) {
return $.trim(text).replace(/\n/g, ' ').replace(/\s\s*/g, ' ');
}

export function every(jqArray, cb) {
let arr = jqArray.get();

return A(arr).every((element) => cb($(element)));
}

/**
* @private
Expand Down
27 changes: 4 additions & 23 deletions addon-test-support/properties/attribute.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import $ from '-jquery';
import { assign } from '../-private/helpers';
import { findMany, findOne } from '../extend';
import { findOne } from '../extend';

/**
* @public
*
* Returns the value of an attribute from the matched element, or an array of
* values from multiple matched elements.
* Returns the value of an attribute from the matched element
*
* @example
* // <input placeholder="a value">
Expand All @@ -21,19 +20,6 @@ import { findMany, findOne } from '../extend';
*
* @example
*
* // <input placeholder="a value">
* // <input placeholder="other value">
*
* import { create, attribute } from 'ember-cli-page-object';
*
* const page = create({
* inputPlaceholders: attribute('placeholder', ':input', { multiple: true })
* });
*
* assert.deepEqual(page.inputPlaceholders, ['a value', 'other value']);
*
* @example
*
* // <div><input></div>
* // <div class="scope"><input placeholder="a value"></div>
* // <div><input></div>
Expand Down Expand Up @@ -69,12 +55,11 @@ import { findMany, findOne } from '../extend';
* @param {string} options.scope - Nests provided scope within parent's scope
* @param {boolean} options.resetScope - Override parent's scope
* @param {number} options.at - Reduce the set of matched elements to the one at the specified index
* @param {boolean} options.multiple - If set, the function will return an array of values
* @param {string} options.testContainer - Context where to search elements in the DOM
* @return {Descriptor}
*
* @throws Will throw an error if no element matches selector
* @throws Will throw an error if multiple elements are matched by selector and multiple option is not set
* @throws Will throw an error if multiple elements are matched by selector
*/
export function attribute(attributeName, selector, userOptions = {}) {
return {
Expand All @@ -83,11 +68,7 @@ export function attribute(attributeName, selector, userOptions = {}) {
get(key) {
let options = assign({ pageObjectKey: key }, userOptions);

if (options.multiple) {
return findMany(this, selector, options).map(element => $(element).attr(attributeName));
} else {
return $(findOne(this, selector, options)).attr(attributeName);
}
return $(findOne(this, selector, options)).attr(attributeName);
}
};
}
37 changes: 3 additions & 34 deletions addon-test-support/properties/contains.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assign } from '../-private/helpers';
import { findMany, findOne } from '../extend';
import { A } from '@ember/array';
import { findOne } from '../extend';

/**
* Returns a boolean representing whether an element or a set of elements contains the specified text.
Expand All @@ -19,33 +18,6 @@ import { A } from '@ember/array';
*
* @example
*
* // <span>lorem</span>
* // <span>ipsum</span>
* // <span>dolor</span>
*
* const page = PageObject.create({
* spansContain: PageObject.contains('span', { multiple: true })
* });
*
* // not all spans contain 'lorem'
* assert.notOk(page.spansContain('lorem'));
*
* @example
*
* // <span>super text</span>
* // <span>regular text</span>
*
* import { create, contains } from 'ember-cli-page-object';
*
* const page = create({
* spansContain: contains('span', { multiple: true })
* });
*
* // all spans contain 'text'
* assert.ok(page.spansContain('text'));
*
* @example
*
* // <div><span>lorem</span></div>
* // <div class="scope"><span>ipsum</span></div>
* // <div><span>dolor</span></div>
Expand Down Expand Up @@ -83,12 +55,11 @@ import { A } from '@ember/array';
* @param {string} options.scope - Nests provided scope within parent's scope
* @param {number} options.at - Reduce the set of matched elements to the one at the specified index
* @param {boolean} options.resetScope - Override parent's scope
* @param {boolean} options.multiple - Check if all elements matched by selector contain the subtext
* @param {string} options.testContainer - Context where to search elements in the DOM
* @return {Descriptor}
*
* @throws Will throw an error if no element matches selector
* @throws Will throw an error if multiple elements are matched by selector and multiple option is not set
* @throws Will throw an error if multiple elements are matched by selector
*/
export function contains(selector, userOptions = {}) {
return {
Expand All @@ -100,9 +71,7 @@ export function contains(selector, userOptions = {}) {
pageObjectKey: `${key}("${textToSearch}")`
}, userOptions);

let elements = options.multiple ? findMany(this, selector, options) : [findOne(this, selector, options)];

return A(elements).every((element) => element.innerText.indexOf(textToSearch) >= 0);
return findOne(this, selector, options).innerText.indexOf(textToSearch) > -1;
};
}
};
Expand Down
36 changes: 4 additions & 32 deletions addon-test-support/properties/has-class.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assign } from '../-private/helpers';
import { findOne, findMany } from '../extend';
import { A } from '@ember/array';
import { findOne } from '../extend';

/**
* Validates if an element or a set of elements have a given CSS class.
Expand All @@ -19,32 +18,6 @@ import { A } from '@ember/array';
*
* @example
*
* // <span class="success"></span>
* // <span class="error"></span>
*
* import { create, hasClass } from 'ember-cli-page-object';
*
* const page = create({
* messagesAreSuccessful: hasClass('success', 'span', { multiple: true })
* });
*
* assert.notOk(page.messagesAreSuccessful);
*
* @example
*
* // <span class="success"></span>
* // <span class="success"></span>
*
* import { create, hasClass } from 'ember-cli-page-object';
*
* const page = create({
* messagesAreSuccessful: hasClass('success', 'span', { multiple: true })
* });
*
* assert.ok(page.messagesAreSuccessful);
*
* @example
*
* // <div>
* // <span class="lorem"></span>
* // </div>
Expand Down Expand Up @@ -86,12 +59,11 @@ import { A } from '@ember/array';
* @param {string} options.scope - Nests provided scope within parent's scope
* @param {number} options.at - Reduce the set of matched elements to the one at the specified index
* @param {boolean} options.resetScope - Override parent's scope
* @param {boolean} options.multiple - Check if all elements matched by selector have the CSS class
* @param {string} options.testContainer - Context where to search elements in the DOM
* @return {Descriptor}
*
* @throws Will throw an error if no element matches selector
* @throws Will throw an error if multiple elements are matched by selector and multiple option is not set
* @throws Will throw an error if multiple elements are matched by selector
*/
export function hasClass(cssClass, selector, userOptions = {}) {
return {
Expand All @@ -100,9 +72,9 @@ export function hasClass(cssClass, selector, userOptions = {}) {
get(key) {
let options = assign({ pageObjectKey: key }, userOptions);

let elements = options.multiple ? findMany(this, selector, options) : [findOne(this, selector, options)];
let element = findOne(this, selector, options);

return A(elements).every((element) => element.classList.contains(cssClass));
return element.classList.contains(cssClass);
}
};
}
37 changes: 3 additions & 34 deletions addon-test-support/properties/is-hidden.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { assign, guardMultiple } from '../-private/helpers';
import { findMany } from '../extend';
import { A } from '@ember/array';
import $ from '-jquery';

/**
Expand All @@ -20,34 +19,6 @@ import $ from '-jquery';
*
* @example
*
* // <span>ipsum</span>
* // <span style="display:none">dolor</span>
*
* import { create, isHidden } from 'ember-cli-page-object';
*
* const page = create({
* spansAreHidden: isHidden('span', { multiple: true })
* });
*
* // not all spans are hidden
* assert.notOk(page.spansAreHidden);
*
* @example
*
* // <span style="display:none">dolor</span>
* // <span style="display:none">dolor</span>
*
* import { create, isHidden } from 'ember-cli-page-object';
*
* const page = create({
* spansAreHidden: isHidden('span', { multiple: true })
* });
*
* // all spans are hidden
* assert.ok(page.spansAreHidden);
*
* @example
*
* // Lorem <strong>ipsum</strong>
*
* import { create, isHidden } from 'ember-cli-page-object';
Expand Down Expand Up @@ -95,11 +66,10 @@ import $ from '-jquery';
* @param {string} options.scope - Nests provided scope within parent's scope
* @param {number} options.at - Reduce the set of matched elements to the one at the specified index
* @param {boolean} options.resetScope - Override parent's scope
* @param {boolean} options.multiple - Check if all elements matched by selector are hidden
* @param {string} options.testContainer - Context where to search elements in the DOM
* @return {Descriptor}
*
* @throws Will throw an error if multiple elements are matched by selector and multiple option is not set
* @throws Will throw an error if multiple elements are matched by selector
*/
export function isHidden(selector, userOptions = {}) {
return {
Expand All @@ -110,10 +80,9 @@ export function isHidden(selector, userOptions = {}) {

let elements = findMany(this, selector, options);

guardMultiple(elements, selector, options.multiple);
guardMultiple(elements, selector);

return elements.length === 0 ||
A(elements).every(element => $(element).is(':hidden'));
return elements.length === 0 || $(elements[0]).is(':hidden');
}
};
}
21 changes: 4 additions & 17 deletions addon-test-support/properties/is-present.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,6 @@ import { assign, guardMultiple } from '../-private/helpers';
*
* @example
*
* // <span>ipsum</span>
* // <span style="display:none">dolor</span>
*
* import { create, isPresent } from 'ember-cli-page-object';
*
* const page = create({
* spanIsPresent: isPresent('span', { multiple: true })
* });
*
* assert.ok(page.spanIsPresent);
*
* @example
*
* // <head>
* // <meta name='robots' content='noindex'>
* // </head>
Expand Down Expand Up @@ -74,11 +61,10 @@ import { assign, guardMultiple } from '../-private/helpers';
* @param {string} options.scope - Nests provided scope within parent's scope
* @param {number} options.at - Reduce the set of matched elements to the one at the specified index
* @param {boolean} options.resetScope - Override parent's scope
* @param {boolean} options.multiple - Check if all elements matched by selector are visible
* @param {string} options.testContainer - Context where to search elements in the DOM
* @return {Descriptor}
*
* @throws Will throw an error if multiple elements are matched by selector and multiple option is not set
* @throws Will throw an error if multiple elements are matched by selector
*/
export function isPresent(selector, userOptions = {}) {
return {
Expand All @@ -87,8 +73,9 @@ export function isPresent(selector, userOptions = {}) {
let options = assign({ pageObjectKey: key }, userOptions);

let elements = findMany(this, selector, options);
guardMultiple(elements, selector, options.multiple);
return elements.length > 0;
guardMultiple(elements, selector);

return elements.length === 1;
}
};
}
Loading

0 comments on commit b6787fd

Please sign in to comment.