1
1
import { createIterable } from '../helpers/helpers.js' ;
2
2
3
3
QUnit . test ( 'Promise.all' , assert => {
4
- let FakePromise1 , FakePromise2 ;
5
- const { all, resolve } = Promise ;
4
+ const { all } = Promise ;
6
5
assert . isFunction ( all ) ;
7
6
assert . arity ( all , 1 ) ;
8
7
assert . name ( all , 'all' ) ;
9
8
assert . looksNative ( all ) ;
10
9
assert . nonEnumerable ( Promise , 'all' ) ;
10
+ assert . true ( Promise . all ( [ ] ) instanceof Promise , 'returns a promise' ) ;
11
+ } ) ;
12
+
13
+ QUnit . test ( 'Promise.all, resolved' , assert => {
14
+ return Promise . all ( [
15
+ Promise . resolve ( 1 ) ,
16
+ Promise . resolve ( 2 ) ,
17
+ Promise . resolve ( 3 ) ,
18
+ ] ) . then ( it => {
19
+ assert . deepEqual ( it , [ 1 , 2 , 3 ] , 'resolved with a correct value' ) ;
20
+ } ) ;
21
+ } ) ;
22
+
23
+ QUnit . test ( 'Promise.all, resolved with rejection' , assert => {
24
+ return Promise . all ( [
25
+ Promise . resolve ( 1 ) ,
26
+ Promise . reject ( 2 ) ,
27
+ Promise . resolve ( 3 ) ,
28
+ ] ) . then ( ( ) => {
29
+ assert . avoid ( ) ;
30
+ } , error => {
31
+ assert . same ( error , 2 , 'rejected with a correct value' ) ;
32
+ } ) ;
33
+ } ) ;
34
+
35
+ QUnit . test ( 'Promise.all, resolved with empty array' , assert => {
36
+ return Promise . all ( [ ] ) . then ( it => {
37
+ assert . deepEqual ( it , [ ] , 'resolved with a correct value' ) ;
38
+ } ) ;
39
+ } ) ;
40
+
41
+ QUnit . test ( 'Promise.all, resolved with timeouts' , assert => {
42
+ return Promise . all ( [
43
+ Promise . resolve ( 1 ) ,
44
+ new Promise ( resolve => setTimeout ( ( ) => resolve ( 2 ) , 10 ) ) ,
45
+ Promise . resolve ( 3 ) ,
46
+ ] ) . then ( it => {
47
+ assert . deepEqual ( it , [ 1 , 2 , 3 ] , 'keeps correct mapping, even with delays' ) ;
48
+ } ) ;
49
+ } ) ;
50
+
51
+ QUnit . test ( 'Promise.all, subclassing' , assert => {
52
+ const { all, resolve } = Promise ;
53
+ function SubPromise ( executor ) {
54
+ executor ( ( ) => { /* empty */ } , ( ) => { /* empty */ } ) ;
55
+ }
56
+ SubPromise . resolve = resolve . bind ( Promise ) ;
57
+ assert . true ( all . call ( SubPromise , [ 1 , 2 , 3 ] ) instanceof SubPromise , 'subclassing, `this` pattern' ) ;
58
+
59
+ function FakePromise1 ( ) { /* empty */ }
60
+ function FakePromise2 ( executor ) {
61
+ executor ( null , ( ) => { /* empty */ } ) ;
62
+ }
63
+ function FakePromise3 ( executor ) {
64
+ executor ( ( ) => { /* empty */ } , null ) ;
65
+ }
66
+ FakePromise1 . resolve = FakePromise2 . resolve = FakePromise3 . resolve = resolve . bind ( Promise ) ;
67
+ assert . throws ( ( ) => {
68
+ all . call ( FakePromise1 , [ 1 , 2 , 3 ] ) ;
69
+ } , 'NewPromiseCapability validations, #1' ) ;
70
+ assert . throws ( ( ) => {
71
+ all . call ( FakePromise2 , [ 1 , 2 , 3 ] ) ;
72
+ } , 'NewPromiseCapability validations, #2' ) ;
73
+ assert . throws ( ( ) => {
74
+ all . call ( FakePromise3 , [ 1 , 2 , 3 ] ) ;
75
+ } , 'NewPromiseCapability validations, #3' ) ;
76
+ } ) ;
77
+
78
+ QUnit . test ( 'Promise.all, iterables' , assert => {
11
79
const iterable = createIterable ( [ 1 , 2 , 3 ] ) ;
12
80
Promise . all ( iterable ) . catch ( ( ) => { /* empty */ } ) ;
13
81
assert . true ( iterable . received , 'works with iterables: iterator received' ) ;
14
82
assert . true ( iterable . called , 'works with iterables: next called' ) ;
83
+ } ) ;
84
+
85
+ QUnit . test ( 'Promise.all, iterables 2' , assert => {
15
86
const array = [ ] ;
16
87
let done = false ;
17
88
// eslint-disable-next-line es/no-nonstandard-array-prototype-properties -- legacy FF case
@@ -22,10 +93,11 @@ QUnit.test('Promise.all', assert => {
22
93
} ;
23
94
Promise . all ( array ) ;
24
95
assert . true ( done ) ;
25
- assert . throws ( ( ) => {
26
- all . call ( null , [ ] ) . catch ( ( ) => { /* empty */ } ) ;
27
- } , TypeError , 'throws without context' ) ;
28
- done = false ;
96
+ } ) ;
97
+
98
+ QUnit . test ( 'Promise.all, iterator closing' , assert => {
99
+ const { resolve } = Promise ;
100
+ let done = false ;
29
101
try {
30
102
Promise . resolve = function ( ) {
31
103
throw new Error ( ) ;
@@ -38,29 +110,10 @@ QUnit.test('Promise.all', assert => {
38
110
} catch { /* empty */ }
39
111
Promise . resolve = resolve ;
40
112
assert . true ( done , 'iteration closing' ) ;
41
- FakePromise1 = function ( executor ) {
42
- executor ( ( ) => { /* empty */ } , ( ) => { /* empty */ } ) ;
43
- } ;
44
- FakePromise2 = FakePromise1 [ Symbol . species ] = function ( executor ) {
45
- executor ( ( ) => { /* empty */ } , ( ) => { /* empty */ } ) ;
46
- } ;
47
- FakePromise1 . resolve = FakePromise2 . resolve = resolve . bind ( Promise ) ;
48
- assert . true ( all . call ( FakePromise1 , [ 1 , 2 , 3 ] ) instanceof FakePromise1 , 'subclassing, `this` pattern' ) ;
49
- FakePromise1 = function ( ) { /* empty */ } ;
50
- FakePromise2 = function ( executor ) {
51
- executor ( null , ( ) => { /* empty */ } ) ;
52
- } ;
53
- const FakePromise3 = function ( executor ) {
54
- executor ( ( ) => { /* empty */ } , null ) ;
55
- } ;
56
- FakePromise1 . resolve = FakePromise2 . resolve = FakePromise3 . resolve = resolve . bind ( Promise ) ;
57
- assert . throws ( ( ) => {
58
- all . call ( FakePromise1 , [ 1 , 2 , 3 ] ) ;
59
- } , 'NewPromiseCapability validations, #1' ) ;
60
- assert . throws ( ( ) => {
61
- all . call ( FakePromise2 , [ 1 , 2 , 3 ] ) ;
62
- } , 'NewPromiseCapability validations, #2' ) ;
63
- assert . throws ( ( ) => {
64
- all . call ( FakePromise3 , [ 1 , 2 , 3 ] ) ;
65
- } , 'NewPromiseCapability validations, #3' ) ;
113
+ } ) ;
114
+
115
+ QUnit . test ( 'Promise.all, without constructor context' , assert => {
116
+ const { all } = Promise ;
117
+ assert . throws ( ( ) => all ( [ ] ) , TypeError , 'Throws if called without a constructor context' ) ;
118
+ assert . throws ( ( ) => all . call ( null , [ ] ) , TypeError , 'Throws if called with null as this' ) ;
66
119
} ) ;
0 commit comments