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

Node test #538

Draft
wants to merge 6 commits into
base: v2-beta
Choose a base branch
from
Draft
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
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: node_js
node_js:
- 10
- 13

addons:
chrome: stable
Expand Down Expand Up @@ -34,6 +34,7 @@ jobs:
script:
- npm run lint:hbs
- npm run lint:js
- npm test:node
- npm test

# we recommend new addons test the current and previous LTS
Expand Down
23 changes: 12 additions & 11 deletions addon-test-support/-private/deprecate.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { deprecate } from '@ember/application/deprecations';
// import { deprecate } from '@ember/application/deprecations';

const NAMESPACE = 'ember-cli-page-object';

// @todo: remove `_` after getting rid of the ember deprecate
// for some reason renaming of the import leads to runrime issues
export default function _deprecate(name, message, since, until) {
const [major, minor] = since.split('.');
console.warn(NAMESPACE, name, message, since, until);
// const [major, minor] = since.split('.');

deprecate(message, false, {
id: `${NAMESPACE}.${name}`,
for: NAMESPACE,
since: {
enabled: since
},
until,
url: `https://ember-cli-page-object.js.org/docs/v${major}.${minor}.x/deprecations#${name}`,
});
// deprecate(message, false, {
// id: `${NAMESPACE}.${name}`,
// for: NAMESPACE,
// since: {
// enabled: since
// },
// until,
// url: `https://ember-cli-page-object.js.org/docs/v${major}.${minor}.x/deprecations#${name}`,
// });
}
22 changes: 21 additions & 1 deletion addon-test-support/-private/finders.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
import {
$,
buildSelector,
findClosestValue,
guardMultiple
} from './helpers';
import { getAdapter } from '../adapters';
import { throwBetterError, ELEMENT_NOT_FOUND } from './better-errors';
import jQuery from 'jquery';

function getContainer(pageObjectNode, options) {
return options.testContainer
|| findClosestValue(pageObjectNode, 'testContainer')
|| getAdapter().testContainer;
}

function isWindow(window) {
return window && window.document;
}

export function $(selector, container) {
const { window: adapterWindow } = getAdapter();

if (isWindow(adapterWindow)) {
if ((typeof window === 'undefined' || !isWindow(window))) {
// node.js environment
// see https://github.com/jquery/jquery/issues/3426#issuecomment-264649345
return jQuery(adapterWindow)(selector, container)
} else {
throw new Error('adapter `window` within browser environment is not supported');
}
} else {
return jQuery(selector, container);
}
}

/**
* Finds a single element, otherwise fails
*
Expand Down
59 changes: 1 addition & 58 deletions addon-test-support/-private/helpers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { get } from '@ember/object';
import Ceibo from 'ceibo';
import deprecate from './deprecate';
export { default as $ } from 'jquery';
export { $ } from './finders';

function isPresent(value) {
return typeof value !== 'undefined';
Expand Down Expand Up @@ -216,62 +215,6 @@ export function findClosestValue(node, property) {
}
}

/**
* @public
*
* Returns a boolean indicating whether an object contains a given property.
* The path to a nested property should be indicated by a dot-separated string.
*
* @param {Object} object - object to check for the target property
* @param {string} pathToProp - dot-separated path to property
* @return {Boolean}
*/
export function objectHasProperty(object, pathToProp) {
const pathSegments = pathToProp.split('.');

for (let i = 0; i < pathSegments.length; i++) {
const key = pathSegments[i];
if (object === null || object === undefined || !object.hasOwnProperty(key)) {
return false;
} else {
object = object[key];
}
}

return true;
}

/**
* @public
*
* Returns the value of an object property. If the property is a function,
* the return value is that function bound to its "owner."
*
* @param {Object} object - object on which to look up the target property
* @param {string} pathToProp - dot-separated path to property
* @return {Boolean|String|Number|Function|Null|Undefined} - value of property
*/
export function getProperty(object, pathToProp) {
const pathSegments = pathToProp.split('.');

if (pathSegments.length === 1) {
const value = get(object, pathToProp);
return typeof value === 'function' ? value.bind(object) : value;
}

const pathToPropOwner = pathSegments.slice(0, -1).join('.');
const propOwner = get(object, pathToPropOwner);

if (propOwner === null || propOwner === undefined) {
return undefined;
}

const propKey = pathSegments[pathSegments.length - 1];
const value = get(propOwner, propKey);

return typeof value === 'function' ? value.bind(propOwner) : value;
}

export function isPageObject(property) {
if (property && typeof property === 'object') {
let meta = Ceibo.meta(property);
Expand Down
63 changes: 58 additions & 5 deletions addon-test-support/macros/alias.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { get } from '@ember/object';
import { throwBetterError } from '../-private/better-errors';
import {
getProperty,
objectHasProperty
} from '../-private/helpers';
import { chainable } from "../-private/chainable";

const ALIASED_PROP_NOT_FOUND = 'PageObject does not contain aliased property';
Expand Down Expand Up @@ -79,7 +76,7 @@ const ALIASED_PROP_NOT_FOUND = 'PageObject does not contain aliased property';
*/
export function alias(pathToProp, options = {}) {
return {
isDescriptor: true,
isDescriptor: true,

get(key) {
if (!objectHasProperty(this, pathToProp)) {
Expand All @@ -103,3 +100,59 @@ export function alias(pathToProp, options = {}) {
}
};
}

/**
* @private
*
* Returns a boolean indicating whether an object contains a given property.
* The path to a nested property should be indicated by a dot-separated string.
*
* @param {Object} object - object to check for the target property
* @param {string} pathToProp - dot-separated path to property
* @return {Boolean}
*/
function objectHasProperty(object, pathToProp) {
const pathSegments = pathToProp.split('.');

for (let i = 0; i < pathSegments.length; i++) {
const key = pathSegments[i];
if (object === null || object === undefined || !object.hasOwnProperty(key)) {
return false;
} else {
object = object[key];
}
}

return true;
}

/**
* @private
*
* Returns the value of an object property. If the property is a function,
* the return value is that function bound to its "owner."
*
* @param {Object} object - object on which to look up the target property
* @param {string} pathToProp - dot-separated path to property
* @return {Boolean|String|Number|Function|Null|Undefined} - value of property
*/
function getProperty(object, pathToProp) {
const pathSegments = pathToProp.split('.');

if (pathSegments.length === 1) {
const value = get(object, pathToProp);
return typeof value === 'function' ? value.bind(object) : value;
}

const pathToPropOwner = pathSegments.slice(0, -1).join('.');
const propOwner = get(object, pathToPropOwner);

if (propOwner === null || propOwner === undefined) {
return undefined;
}

const propKey = pathSegments[pathSegments.length - 1];
const value = get(propOwner, propKey);

return typeof value === 'function' ? value.bind(propOwner) : value;
}
2 changes: 1 addition & 1 deletion config/ember-try.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ module.exports = function() {
},
{
name: 'node-tests',
command: 'npm run nodetest',
command: 'npm run test:ember-node',
bower: {
dependencies: {}
}
Expand Down
53 changes: 53 additions & 0 deletions node-tests/basic-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// requires >= node@13
// should be invoked like `node --es-module-specifier-resolution=node ./node-tests/run-test.js`

import { create, visitable } from 'ember-cli-page-object';
import Adapter from 'ember-cli-page-object/adapter';
import { setAdapter } from 'ember-cli-page-object/adapters';
import jsdom from 'jsdom';
import QUnit from 'qunit';
const { module, test } = QUnit;

const RESPONSE_FIXTURE = `<html>
<body>
<div class="test"></div>
</body>
</html>`;

class NodeJSAdapter extends Adapter {
visit() {
return new Promise((resolve) => {
setTimeout(() => {
const { JSDOM } = jsdom;
const { window } = (new JSDOM(RESPONSE_FIXTURE));

this.window = window;

resolve();
}, 1000)
})
}

get testContainer() {
return this.window.document;
}
}

module('node.js', function(hooks) {
hooks.beforeEach(function() {
setAdapter(new NodeJSAdapter());
});

test('it works!', async function(assert) {
const a = create({
scope: '.test',

visit: visitable(''),
});

await a.visit();

assert.strictEqual(a.isVisible, false);
assert.strictEqual(a.isPresent, true);
});
});
15 changes: 13 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"pageobject",
"testing"
],
"type": "module",
"repository": {
"type": "git",
"url": "https://github.com/san650/ember-cli-page-object.git"
Expand All @@ -32,8 +33,9 @@
"start": "ember serve",
"test": "ember test",
"test:all": "ember try:each",
"test:ember-node": "mocha node-tests --recursive",
"test:node": "node --es-module-specifier-resolution=node ./node_modules/.bin/qunit node-tests/basic-test.js",
"docs": "node ./docs",
"nodetest": "mocha node-tests --recursive",
"prepublishOnly": "ember ts:precompile",
"postpublish": "ember ts:clean"
},
Expand All @@ -42,7 +44,7 @@
"@babel/preset-env": "^7.12.11",
"broccoli-file-creator": "^2.1.1",
"broccoli-merge-trees": "^2.0.0",
"ceibo": "~2.0.0",
"ceibo": "~2.1.0",
"ember-cli-babel": "^7.24.0-beta.2",
"ember-cli-node-assets": "^0.2.2",
"ember-native-dom-helpers": "^0.6.3",
Expand Down Expand Up @@ -95,6 +97,7 @@
"mkdirp": "^0.5.1",
"mocha": "^4.0.1",
"ncp": "^2.0.0",
"qunit": "^2.14.0",
"qunit-dom": "^0.8.0",
"rimraf": "^2.5.2",
"typescript": "^3.7.5",
Expand All @@ -103,6 +106,14 @@
"engines": {
"node": "6.* || >= 7.*"
},
"exports": {
".": "./addon-test-support",
"./adapter": "./addon-test-support/adapter",
"./adapters": "./addon-test-support/adapters",
"./adapters/*": "./addon-test-support/adapters/*.js",
"./extend": "./addon-test-support/extend",
"./macros": "./addon-test-support/macros"
},
"homepage": "http://ember-cli-page-object.js.org",
"bugs": "https://github.com/san650/ember-cli-page-object/issues",
"ember-addon": {
Expand Down
Loading