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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(promise-helpers): assert that argument is an array #313

Merged
merged 1 commit into from
Sep 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions addon/-cancelable-promise-helpers.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { assert } from '@ember/debug';
import RSVP, { Promise } from 'rsvp';
import TaskInstance from './-task-instance';
import { yieldableSymbol } from './utils';
Expand All @@ -23,6 +24,9 @@ function * resolver(value) {
* [Check out the "Awaiting Multiple Child Tasks example"](/docs/examples/joining-tasks)
*/
export const all = (things) => {
// Extra assertion here to circumvent the `things.length` short circuit.
assert(`'all' expects an array.`, Array.isArray(things));

if (things.length === 0) {
return things;
}
Expand Down Expand Up @@ -105,6 +109,7 @@ function getValues(obj) {
function taskAwareVariantOf(obj, method, getItems) {
return function(thing) {
let items = getItems(thing);
assert(`'${method}' expects an array.`, Array.isArray(items));
let defer = RSVP.defer();

obj[method](thing).then(defer.resolve, defer.reject);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"ember-maybe-import-regenerator": "^0.1.6",
"ember-notify": "^5.3.0",
"ember-qunit": "4.1.2",
"ember-qunit-assert-helpers": "^0.2.2",
"ember-resolver": "^4.0.0",
"ember-sinon": "^2.1.0",
"ember-source": "~3.6.0",
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/cancelable-promise-helpers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ module('Unit: cancelable promises test helpers', function() {
assert.equal(childTask.get('concurrency'), 0);
});

test("all throws an assertion, if something other than an array is passed", function (assert) {
assert.expectAssertion(() => {
all();
}, /'all' expects an array/);
assert.expectAssertion(() => {
all(RSVP.Promise.resolve());
}, /'all' expects an array/);
});

test("allSettled behaves like Promise.allSettled", function(assert) {
assert.expect(6);

Expand Down Expand Up @@ -230,6 +239,15 @@ module('Unit: cancelable promises test helpers', function() {
assert.equal(childTask.get('concurrency'), 0);
});

test("allSettled throws an assertion, if something other than an array is passed", function (assert) {
assert.expectAssertion(() => {
allSettled();
}, /'allSettled' expects an array/);
assert.expectAssertion(() => {
allSettled(RSVP.Promise.resolve());
}, /'allSettled' expects an array/);
});

test("hash", function(assert) {
assert.expect(1);

Expand Down Expand Up @@ -308,6 +326,15 @@ module('Unit: cancelable promises test helpers', function() {
assert.equal(obj.get('child.concurrency'), 0);
});

test("race throws an assertion, if something other than an array is passed", function (assert) {
assert.expectAssertion(() => {
race();
}, /'race' expects an array/);
assert.expectAssertion(() => {
race(RSVP.Promise.resolve());
}, /'race' expects an array/);
});

test("yieldable helpers work with null/undefined values", function(assert) {
assert.expect(1);

Expand Down