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, fake promises' , assert => {
52
+ const { all, resolve } = Promise ;
53
+ let FakePromise1 = function ( executor ) {
54
+ executor ( ( ) => { /* empty */ } , ( ) => { /* empty */ } ) ;
55
+ } ;
56
+ let FakePromise2 = FakePromise1 [ Symbol . species ] = function ( executor ) {
57
+ executor ( ( ) => { /* empty */ } , ( ) => { /* empty */ } ) ;
58
+ } ;
59
+ FakePromise1 . resolve = FakePromise2 . resolve = resolve . bind ( Promise ) ;
60
+ assert . true ( all . call ( FakePromise1 , [ 1 , 2 , 3 ] ) instanceof FakePromise1 , 'subclassing, `this` pattern' ) ;
61
+ FakePromise1 = function ( ) { /* empty */ } ;
62
+ FakePromise2 = function ( executor ) {
63
+ executor ( null , ( ) => { /* empty */ } ) ;
64
+ } ;
65
+ const FakePromise3 = function ( executor ) {
66
+ executor ( ( ) => { /* empty */ } , null ) ;
67
+ } ;
68
+ FakePromise1 . resolve = FakePromise2 . resolve = FakePromise3 . resolve = resolve . bind ( Promise ) ;
69
+ assert . throws ( ( ) => {
70
+ all . call ( FakePromise1 , [ 1 , 2 , 3 ] ) ;
71
+ } , 'NewPromiseCapability validations, #1' ) ;
72
+ assert . throws ( ( ) => {
73
+ all . call ( FakePromise2 , [ 1 , 2 , 3 ] ) ;
74
+ } , 'NewPromiseCapability validations, #2' ) ;
75
+ assert . throws ( ( ) => {
76
+ all . call ( FakePromise3 , [ 1 , 2 , 3 ] ) ;
77
+ } , 'NewPromiseCapability validations, #3' ) ;
78
+ } ) ;
79
+
80
+ QUnit . test ( 'Promise.all, iterables' , assert => {
11
81
const iterable = createIterable ( [ 1 , 2 , 3 ] ) ;
12
82
Promise . all ( iterable ) . catch ( ( ) => { /* empty */ } ) ;
13
83
assert . true ( iterable . received , 'works with iterables: iterator received' ) ;
14
84
assert . true ( iterable . called , 'works with iterables: next called' ) ;
85
+ } ) ;
86
+
87
+ QUnit . test ( 'Promise.all, iterables 2' , assert => {
15
88
const array = [ ] ;
16
89
let done = false ;
17
90
// eslint-disable-next-line es/no-nonstandard-array-prototype-properties -- legacy FF case
@@ -22,10 +95,11 @@ QUnit.test('Promise.all', assert => {
22
95
} ;
23
96
Promise . all ( array ) ;
24
97
assert . true ( done ) ;
25
- assert . throws ( ( ) => {
26
- all . call ( null , [ ] ) . catch ( ( ) => { /* empty */ } ) ;
27
- } , TypeError , 'throws without context' ) ;
28
- done = false ;
98
+ } ) ;
99
+
100
+ QUnit . test ( 'Promise.all, iterables closing' , assert => {
101
+ const { resolve } = Promise ;
102
+ let done = false ;
29
103
try {
30
104
Promise . resolve = function ( ) {
31
105
throw new Error ( ) ;
@@ -38,29 +112,10 @@ QUnit.test('Promise.all', assert => {
38
112
} catch { /* empty */ }
39
113
Promise . resolve = resolve ;
40
114
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' ) ;
115
+ } ) ;
116
+
117
+ QUnit . test ( 'Promise.all, without constructor context' , assert => {
118
+ const { all } = Promise ;
119
+ assert . throws ( ( ) => all ( [ ] ) , TypeError , 'Throws if called without a constructor context' ) ;
120
+ assert . throws ( ( ) => all . call ( null , [ ] ) , TypeError , 'Throws if called with null as this' ) ;
66
121
} ) ;
0 commit comments