@@ -518,4 +518,98 @@ describe(
518518 testPromiseSubClass ( done ) ;
519519 } : function ( ) { testPromiseSubClass ( ) ; } ) ;
520520 } ) ;
521+
522+ describe ( 'Promise.allSettled' , ( ) => {
523+ const yes = function makeFulfilledResult ( value : any ) {
524+ return { status : 'fulfilled' , value : value } ;
525+ } ;
526+ const no = function makeRejectedResult ( reason : any ) {
527+ return { status : 'rejected' , reason : reason } ;
528+ } ;
529+ const a = { } ;
530+ const b = { } ;
531+ const c = { } ;
532+ const allSettled = ( Promise as any ) . allSettled ;
533+ it ( 'no promise values' , ( done : DoneFn ) => {
534+ allSettled ( [ a , b , c ] ) . then ( ( results : any [ ] ) => {
535+ expect ( results ) . toEqual ( [ yes ( a ) , yes ( b ) , yes ( c ) ] ) ;
536+ done ( ) ;
537+ } ) ;
538+ } ) ;
539+ it ( 'all fulfilled' , ( done : DoneFn ) => {
540+ allSettled ( [
541+ Promise . resolve ( a ) , Promise . resolve ( b ) , Promise . resolve ( c )
542+ ] ) . then ( ( results : any [ ] ) => {
543+ expect ( results ) . toEqual ( [ yes ( a ) , yes ( b ) , yes ( c ) ] ) ;
544+ done ( ) ;
545+ } ) ;
546+ } ) ;
547+ it ( 'all rejected' , ( done : DoneFn ) => {
548+ allSettled ( [
549+ Promise . reject ( a ) , Promise . reject ( b ) , Promise . reject ( c )
550+ ] ) . then ( ( results : any [ ] ) => {
551+ expect ( results ) . toEqual ( [ no ( a ) , no ( b ) , no ( c ) ] ) ;
552+ done ( ) ;
553+ } ) ;
554+ } ) ;
555+ it ( 'mixed' , ( done : DoneFn ) => {
556+ allSettled ( [ a , Promise . resolve ( b ) , Promise . reject ( c ) ] ) . then ( ( results : any [ ] ) => {
557+ expect ( results ) . toEqual ( [ yes ( a ) , yes ( b ) , no ( c ) ] ) ;
558+ done ( ) ;
559+ } ) ;
560+ } ) ;
561+ it ( 'mixed should in zone' , ( done : DoneFn ) => {
562+ const zone = Zone . current . fork ( { name : 'settled' } ) ;
563+ const bPromise = Promise . resolve ( b ) ;
564+ const cPromise = Promise . reject ( c ) ;
565+ zone . run ( ( ) => {
566+ allSettled ( [ a , bPromise , cPromise ] ) . then ( ( results : any [ ] ) => {
567+ expect ( results ) . toEqual ( [ yes ( a ) , yes ( b ) , no ( c ) ] ) ;
568+ expect ( Zone . current . name ) . toEqual ( zone . name ) ;
569+ done ( ) ;
570+ } ) ;
571+ } ) ;
572+ } ) ;
573+ it ( 'poisoned .then' , ( done : DoneFn ) => {
574+ const promise = new Promise ( function ( ) { } ) ;
575+ promise . then = function ( ) { throw new EvalError ( ) ; } ;
576+ allSettled ( [ promise ] ) . then (
577+ ( ) => { fail ( 'should not reach here' ) ; } ,
578+ ( reason : any ) => {
579+ expect ( reason instanceof EvalError ) . toBe ( true ) ;
580+ done ( ) ;
581+ } ) ;
582+ } ) ;
583+ const Subclass = ( function ( ) {
584+ try {
585+ // eslint-disable-next-line no-new-func
586+ return Function (
587+ 'class Subclass extends Promise { constructor(...args) { super(...args); this.thenArgs = []; } then(...args) { Subclass.thenArgs.push(args); this.thenArgs.push(args); return super.then(...args); } } Subclass.thenArgs = []; return Subclass;' ) ( ) ;
588+ } catch ( e ) { /**/
589+ }
590+
591+ return false ;
592+ } ( ) ) ;
593+
594+ describe ( 'inheritance' , ( ) => {
595+ it ( 'preserves correct subclass' , ( ) => {
596+ const promise = allSettled . call ( Subclass , [ 1 ] ) ;
597+ expect ( promise instanceof Subclass ) . toBe ( true ) ;
598+ expect ( promise . constructor ) . toEqual ( Subclass ) ;
599+ } ) ;
600+
601+ it ( 'invoke the subclass' , ( ) => {
602+ Subclass . thenArgs . length = 0 ;
603+
604+ const original = Subclass . resolve ( ) ;
605+ expect ( Subclass . thenArgs . length ) . toBe ( 0 ) ;
606+ expect ( original . thenArgs . length ) . toBe ( 0 ) ;
607+
608+ allSettled . call ( Subclass , [ original ] ) ;
609+
610+ expect ( original . thenArgs . length ) . toBe ( 1 ) ;
611+ expect ( Subclass . thenArgs . length ) . toBe ( 1 ) ;
612+ } ) ;
613+ } ) ;
614+ } ) ;
521615 } ) ) ;
0 commit comments