-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
285 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { assign } from '@ember/polyfills'; | ||
|
||
import { | ||
create as upstreamCreate | ||
} from 'ember-cli-page-object'; | ||
|
||
function buildDescriptor(node, keyName, descriptor /*, descriptorBuilder*/) { | ||
descriptor = assign({ | ||
configurable: true, | ||
enumerable: true, | ||
}, descriptor); | ||
|
||
if (descriptor.value) { | ||
descriptor.writable = false; | ||
} else { | ||
descriptor.get = function() { | ||
return descriptor.get.call(this, keyName); | ||
}; | ||
} | ||
|
||
if (typeof node.__propertyNames__ === 'undefined') { | ||
node.__propertyNames__ = []; | ||
} | ||
|
||
node.__propertyNames__.push(keyName); | ||
|
||
Object.defineProperty(node, keyName, descriptor); | ||
} | ||
|
||
export function create(definition/* , options = {} */) { | ||
// options = assign({ | ||
// builder: { | ||
// descriptor: buildDescriptor | ||
// } | ||
// }, options); | ||
|
||
// return upstreamCreate(definition, options); | ||
return upstreamCreate(definition); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"compilerOptions":{"target":"es6","experimentalDecorators":true},"exclude":["node_modules","bower_components","tmp","vendor",".git","dist"]} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { module, test } from 'qunit'; | ||
import { setupRenderingTest } from 'ember-qunit'; | ||
import { render } from '@ember/test-helpers'; | ||
import { create } from 'ember-cli-page-object-qunit'; | ||
import hbs from 'htmlbars-inline-precompile'; | ||
|
||
const page = create({ | ||
screen: { | ||
scope: '.screen', | ||
result: { | ||
scope: 'result', | ||
} | ||
} | ||
}); | ||
|
||
module('basic test', function(hooks) { | ||
setupRenderingTest(hooks); | ||
|
||
test('1', async function(assert) { | ||
await render(hbs` | ||
<div class="screen"> | ||
</div>`); | ||
|
||
page.screen.as(s => { | ||
assert.po(s).isPresent(); | ||
assert.po(s).isVisible(); | ||
assert.po(s).isNotHidden(); | ||
|
||
s.result.as(r => { | ||
assert.po(r).hasText('91'); | ||
assert.po(r).contains('91'); | ||
assert.po(r).doesNotContain('99'); | ||
}) | ||
}); | ||
}); | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import Ceibo from 'ceibo'; | ||
|
||
import { | ||
camelize, | ||
decamelize | ||
} from '@ember/string'; | ||
|
||
export class Assertions { | ||
constructor(pageObject, testContext) { | ||
this.po = pageObject; | ||
this.testContext = testContext; | ||
|
||
for (let key in this.po.__propertNames__) { | ||
let inverseKey = buildInverseKey(key); | ||
|
||
this[key] = this.__wrap__(key, true, humanizeString(key)) | ||
this[inverseKey] = this.__wrap__(key, false, humanizeString(inverseKey)); | ||
} | ||
} | ||
|
||
__wrap__(k, isPositive = true, defaultMessage = undefined) { | ||
return (...assertionArgs) => { | ||
let message, | ||
result; | ||
|
||
if (typeof this.po[k] === 'function') { | ||
const poMethod = this.po[k]; | ||
const methodArgs = Array.prototype.slice.call(assertionArgs, 0, poMethod.length); | ||
|
||
message = assertionArgs[poMethod.length] || (defaultMessage + ' ' + argsToString(methodArgs)); | ||
result = poMethod.apply(this.po, methodArgs); | ||
} else { | ||
message = assertionArgs[0] || defaultMessage; | ||
result = this.po[k]; | ||
} | ||
|
||
this._pushResult(result === (isPositive === true), message) | ||
} | ||
} | ||
|
||
hasText(expected, message = `has text "${expected}"`) { | ||
const actual = trimWhitespace(this.po.text); | ||
const result = expected === actual; | ||
|
||
this._pushResult(result, message, { | ||
expected, | ||
actual | ||
}); | ||
} | ||
|
||
doesNotHaveText(expected, message = `has no text "${expected}"`) { | ||
const actual = trimWhitespace(this.po.text); | ||
const result = expected !== actual; | ||
|
||
this._pushResult(result, message || `does not have valid text "${expected}"`, { | ||
expected, | ||
actual | ||
}); | ||
} | ||
|
||
_pushResult(result, message, options = {}) { | ||
if (!message) { | ||
throw new Error('no message provided for the test result'); | ||
} | ||
|
||
if (typeof result !== 'boolean') { | ||
throw new Error('test result must be a boolean'); | ||
} | ||
|
||
let { | ||
actual = result, | ||
expected = true | ||
} = options; | ||
|
||
let prefix = `${buildFullPath(this.po)}`; | ||
|
||
message = `${prefix}: ${message}`; | ||
|
||
this.testContext.pushResult({ | ||
result, | ||
actual, | ||
expected, | ||
message | ||
}); | ||
} | ||
} | ||
|
||
function trimWhitespace(string) { | ||
return string | ||
.replace(/[\t\r\n]/g, ' ') | ||
.replace(/ +/g, ' ') | ||
.replace(/^ /, '') | ||
.replace(/ $/, ''); | ||
} | ||
|
||
export function buildFullPath(node) { | ||
let path = []; | ||
let current = node; | ||
|
||
do { | ||
path.unshift(Ceibo.meta(current).key); | ||
current = Ceibo.parent(current) | ||
} while (Ceibo.parent(current)); | ||
|
||
return path.join('/'); | ||
} | ||
|
||
export function buildSelector(node) { | ||
let path = []; | ||
let current = node; | ||
|
||
do { | ||
path.unshift(current.scope); | ||
current = Ceibo.parent(current) | ||
} while (Ceibo.parent(current)); | ||
|
||
return path.join(' '); | ||
} | ||
|
||
function humanizeString(input) { | ||
return decamelize(input).replace(/_/g, ' '); | ||
} | ||
|
||
function argsToString(args) { | ||
return args.map(a => typeof a === 'string' ? `"${a}"` : a).join(', ') | ||
} | ||
|
||
function buildInverseKey(key) { | ||
const [leader, ...restWords] = decamelize(key).split('_'); | ||
|
||
if (restWords.length === 0) { | ||
return camelize(`doesNot ${leader.replace(/s$/, '')}`); | ||
} | ||
|
||
let inverseLeader; | ||
switch (leader) { | ||
case 'is': inverseLeader = 'isNot'; break; | ||
case 'has': inverseLeader = 'doesNotHave'; break; | ||
case 'have': inverseLeader = 'doesNotHave'; break; | ||
} | ||
|
||
return camelize([inverseLeader, ...restWords].join(' ')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Assertions } from './assertions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters