Run ngrx/effect outside of Angular's zone to prevent timeout in Protractor

后端 未结 2 460
盖世英雄少女心
盖世英雄少女心 2021-02-01 09:53

I just started to write e2e tests for my app and am running into timeout problems with Protractor and ngrx/effects.

I have the following effect dispatching an action ev

2条回答
  •  孤街浪徒
    2021-02-01 10:28

    For Angular 6 and RxJS 6 use the following code:

    import { SchedulerLike, Subscription } from 'rxjs'
    import { NgZone } from '@angular/core'
    
    class LeaveZoneScheduler implements SchedulerLike {
      constructor(private zone: NgZone, private scheduler: SchedulerLike) { }
    
      schedule(...args: any[]): Subscription {
        return this.zone.runOutsideAngular(() =>
          this.scheduler.schedule.apply(this.scheduler, args)
        )
      }
    
      now (): number {
        return this.scheduler.now()
      }
    }
    
    class EnterZoneScheduler implements SchedulerLike {
      constructor(private zone: NgZone, private scheduler: SchedulerLike) { }
    
      schedule(...args: any[]): Subscription {
        return this.zone.run(() =>
          this.scheduler.schedule.apply(this.scheduler, args)
        )
      }
    
      now (): number {
        return this.scheduler.now()
      }
    }
    
    export function leaveZone(zone: NgZone, scheduler: SchedulerLike): SchedulerLike {
      return new LeaveZoneScheduler(zone, scheduler)
    }
    
    export function enterZone(zone: NgZone, scheduler: SchedulerLike): SchedulerLike {
      return new EnterZoneScheduler(zone, scheduler)
    }
    

    The effect should look like:

    import { asyncScheduler } from 'rxjs'
    import { filter, observeOn, bufferTime } from 'rxjs/operators'
    import { enterZone, leaveZone } from './util';
    
    actions$.ofType('[Light] Turn On')
      .pipe(
        bufferTime(300, leaveZone(this.ngZone, asyncScheduler)),
        filter(messages => messages.length > 0),
        observeOn(enterZone(this.ngZone, asyncScheduler)),
      )
    

提交回复
热议问题