Skip to content

Commit

Permalink
Include Ember.inspect(query) in failed assertion for findOne (#26)
Browse files Browse the repository at this point in the history
Include `Ember.inspect(query)` in failed assertion for `findOne`
  • Loading branch information
twokul authored Oct 23, 2019
2 parents dca1085 + 0b147f4 commit b636e34
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
22 changes: 15 additions & 7 deletions addon-test-support/-private/properties/collection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import Ceibo from 'ceibo';

import { assert } from '@ember/debug';
import { create, collection as ecpoCollection } from 'ember-cli-page-object';
import { assert, inspect } from '@ember/debug';

class CollectionProxy {
constructor(scope, definition, key, parent) {
Expand Down Expand Up @@ -53,7 +51,12 @@ class CollectionProxy {
findOne(query) {
let result = this.findAll(query);

assert(`Expected at most one result from 'findOne' query in '${this._collection.key}' collection, but found ${result.length}`, result.length === 1);
assert(
`Expected at most one result from 'findOne' query in '${
this._collection.key
}' collection, but found ${result.length} using query ${inspect(query)}`,
result.length === 1
);

return result[0];
}
Expand All @@ -62,19 +65,24 @@ class CollectionProxy {
let predicate;

if (typeof query === 'object') {
predicate = (item) => {
predicate = item => {
let isMatch = true;

for (let key in query) {
isMatch = isMatch && item[key] === query[key];
}

return isMatch;
}
};
} else if (typeof query === 'function') {
predicate = query;
} else {
assert(`Expected query for findAll to be either an object or function, received: ${query}`, false);
assert(
`Expected query for findAll to be either an object or function, received: ${inspect(
query
)}`,
false
);
}

return this.filter(predicate);
Expand Down
8 changes: 3 additions & 5 deletions tests/acceptance/collection-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { test } from 'qunit';
import moduleForAcceptance from '../../tests/helpers/module-for-acceptance';

import PageObject, { collection, hasClass, text } from 'ember-classy-page-object';
// import { findElement } from 'ember-classy-page-object/extend';

const SimpleListPage = PageObject.extend({
scope: '[data-test-simple-list]',
Expand Down Expand Up @@ -37,9 +36,8 @@ test('collection works as expected', function(assert) {

assert.equal(list.items.findOne((i) => i.isActive).text, 'Bar');
assert.equal(list.items.findOne({ text: 'Bar', isActive: true }).text, 'Bar');
assert.throws(() => assert.equal(list.items.findOne({ text: 'Foo', isActive: true }), /Expected at most one result from 'findOne' query in 'foos' collection, but found 0/));

assert.throws(() => list.items.findOne((i) => i.isActive || i.text === 'Foo'), /Expected at most one result from 'findOne' query in 'items' collection, but found 2/);
assert.throws(() => list.items.findOne({ text: 'Foo', isActive: true }), /Expected at most one result.*'findOne' query in 'items' collection.*but found 0.*using query.*isActive: true/);
assert.throws(() => list.items.findOne((i) => i.isActive || i.text === 'Foo'), /Expected at most one result.*'findOne' query in 'items' collection.*but found 2/);
});
});

Expand All @@ -57,7 +55,7 @@ test('collections do not share instances of proxies', function(assert) {

andThen(() => {
assert.throws(() => {
assert.equal(page.simpleList.items.objectAt(0).text, 'Foo')
page.simpleList.items.objectAt(0).text
}, /foo-bar-baz \[data-test-simple-list\] \[data-test-simple-list-item\]:eq\(0\)/);
});
});
Expand Down

0 comments on commit b636e34

Please sign in to comment.