2017-01-12 1 views
3

일정 기간 후에 자동 로그 아웃 사용자에게 ng2-idle을 사용하고 있습니다. 내 appComponent의 생성자를 초기화 : 나는 this.idle.watch() 사용하여 내 LoginComponent에서 그것을 시작 성공적으로 로그인하면견인기 테스트를 위해 ng2-idle을 중지하십시오.

import {Idle, DEFAULT_INTERRUPTSOURCES} from '@ng-idle/core'; 

export class AppComponent { 

    constructor(
     private idle:Idle) { 

     idle.setIdle(21600); 
     idle.setTimeout(1); 
     idle.setInterrupts(DEFAULT_INTERRUPTSOURCES); 

     idle.onTimeout.subscribe(() => { this.logout(); }); 
    }; 

(유휴 생성자에 주입).

이 모든 것이 잘 작동하지만 각도기 테스트를 실행하면 제한 시간이 초과되어 길잡이가 시간 초과를 기다리고 있기 때문에 시간이 얼마 남지 않았으며 ng2-idle을 6 시간으로 설정했습니다.

Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL. 

Idle.watch()을 사용하여 초기화하지 않으면 테스트가 실행됩니다.

는 내가하고 싶은 내 각도기 설정 파일의 onPrepare 블록에 Idle.stop();을 설정하고 테스트를 내 onComplete 블록 완료 후 Idle.watch();으로 재설정하는 것입니다.

나는 각도기의 conf 파일에 var idle = require('@ng-idle/core');을 시도했지만 그것은 다음 날 다시 던졌습니다 :

ReferenceError: document is not defined

그래서 어떻게 각도기 설정 파일의 NG2 - 유휴 모듈을 필요로 할 수 있습니까?

+0

를위한 오픈 결함이 발견했다. idle + timeout 값이 DEFAULT_TIMEOUT_INTERVAL보다 작 으면 테스트가 실행중인 것처럼 보입니다. – santon

답변

2

질문하신 내용에 대한 해결책을 제공해 드릴 수 없습니다. 하지만이 문제를 해결하기위한 다른 접근 방식을 제공 할 것입니다. 팀을위한 각도기를 설치하는 동안 ng-idle을 사용하여 세션 시간 제한 전략을 구현 한 후에도 동일한 문제가 발생했습니다. 이 문제를 해결하는 방법에 대한 가슴 아픈 생각을 한 후에 Angular zone API를 사용하여이 문제를 해결할 수 있음을 깨달았습니다. 여기에 솔루션과 함께 내 appcomponent입니다. runOutsideAngular 메소드를 영역에서 참조하십시오.

모든 상태 변경 내용은 run 메서드 내에 래핑됩니다. 그렇지 않으면 변경 감지가 작동하지 않으며 카운트 다운 타이머를 포함하여 상태 필드가 UI를 업데이트하지 않습니다.

이 해결책은 ng-idle을 사용하는 응용 프로그램을 수정하여 현재 이해중인 각도기 내에서 수행 할 수 없도록합니다.

this.ngZone.runOutsideAngular

@Component({ 
selector : 'app-root', 
templateUrl: './app.component.html', 
styleUrls : [ './app.component.scss' ] 
}) 

export class AppComponent implements OnInit, OnDestroy{ 

idleState = 'Not started.'; 
idleStateTitle = ''; 
idleStateBtnText = ''; 
timedOut = false; 

@ViewChild(DialogComponent) dialog: DialogComponent; 

constructor(private idle: Idle, 
      private locationStrategy: LocationStrategy, 
      private authUtils: AuthUtils, 
      private ngZone: NgZone){ 

    this.ngZone.runOutsideAngular(() => { 
     //Idle timer is set to 28 minutes and timeout count down timer will run for 2 minutes. 
     idle.setIdle(28 * 60); 
     idle.setTimeout(120); 
    }); 


    //When idling starts display the dialog with count down timer 
    idle.onIdleStart.subscribe(() =>{ 
     this.idleState = ''; 
     this.idleStateTitle = 'Your session is about to expire.'; 
     this.idleStateBtnText = 'Stay Logged In'; 
     this.ngZone.run(() => this.dialog.show()); 
    }); 

    //User stopped idling 
    idle.onIdleEnd.subscribe(() =>{ 
     this.idleState = 'No longer idle.'; 
    }); 

    //Show timeout warning two minutes before expiry 
    idle.onTimeoutWarning.subscribe((countdown) =>{ 
     this.ngZone.run(() => { this.idleState = 'Your session will time out in ' + countdown + ' seconds! '}); 
    }); 

    //Session Timed out 
    idle.onTimeout.subscribe(() =>{ 
     this.ngZone.run(() =>{ 
      this.idleStateTitle = "Your session has expired."; 
      this.idleState = 'To Continue, log back in'; 
      this.timedOut = true; 
      this.idleStateBtnText = 'Log in Again'; 
     }); 
    }); 
} 

reset(){ 
    this.ngZone.runOutsideAngular(() => { 
     this.idle.watch(); 
    }); 
    this.ngZone.run(() =>{ 
     this.idleState = 'Started.'; 
     this.idleStateTitle = ""; 
     this.idleStateBtnText = ''; 
     this.timedOut = false; 
    }); 
} 

title = 'Projects'; 

/** 
* <p>Refresh Token by getting user token from the user service. Also reset the idle state timer here.</p> 
*/ 
refreshToken(){ 
    this.authUtils.refreshToken(); 
    this.reset(); 
} 

/** 
* Handle timeout 
*/ 
processTimeout(){ 
    this.dialog.hide(null); 
    if(this.timedOut){ 
     AuthUtils.invalidateSession(); 
     let origin = window.location.origin; 
     window.location.href = origin + this.locationStrategy.getBaseHref() + "?_="+ (Math.floor(Math.random() * 10000)); 
    } 
    else{ 
     this.refreshToken(); 
    } 
} 

ngOnInit(): void{ 
    this.reset(); 
} 

ngOnDestroy(): void{ 
    this.ngZone.runOutsideAngular(() =>{ 
     this.idle.stop(); 
    }); 
} 
} 

나는 어떤 부분 성공 browser.waitForAngularEnabled(false)를 사용하지 않도록 설정하고 beforeEachafterEach 자스민 루틴 내에서 가능했다. 그러나 그들은 벗겨져서 모든 테스트에서 작동하지 않았습니다.

편집 : 나는 NG2 유휴 이슈 트래커를 확인하고 내가 알아 내려고 같은 문제를 타격하고이 here

관련 문제