Skip to content

Commit

Permalink
Fix interval service injection and use of setInterval
Browse files Browse the repository at this point in the history
  • Loading branch information
FabianGosebrink committed Mar 21, 2024
1 parent f2ceaef commit d2da01e
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 18 deletions.
Expand Up @@ -23,7 +23,7 @@ describe('CodeFlowCallbackService ', () => {
CodeFlowCallbackService,
mockProvider(FlowsService),
mockProvider(FlowsDataService),
IntervalService,
mockProvider(IntervalService),
],
});
});
Expand Down
Expand Up @@ -20,10 +20,9 @@ describe('ImplicitFlowCallbackService ', () => {
TestBed.configureTestingModule({
imports: [RouterTestingModule],
providers: [
ImplicitFlowCallbackService,
mockProvider(FlowsService),
mockProvider(FlowsDataService),
IntervalService,
mockProvider(IntervalService),
],
});
});
Expand Down
Expand Up @@ -7,7 +7,16 @@ describe('IntervalService', () => {

beforeEach(() => {
TestBed.configureTestingModule({
providers: [IntervalService],
providers: [
{
provide: Document,
useValue: {
defaultView: {
setInterval: window.setInterval,
},
},
},
],
});
});

Expand Down
@@ -1,11 +1,13 @@
import { Injectable, NgZone } from '@angular/core';
import { Injectable, NgZone, inject } from '@angular/core';
import { Observable, Subscription } from 'rxjs';

@Injectable({ providedIn: 'root' })
export class IntervalService {
runTokenValidationRunning: Subscription | null = null;
private readonly zone = inject(NgZone);

private readonly document = inject<Document>(Document);

constructor(private readonly zone: NgZone) {}
runTokenValidationRunning: Subscription | null = null;

isTokenValidationRunning(): boolean {
return Boolean(this.runTokenValidationRunning);
Expand All @@ -22,10 +24,10 @@ export class IntervalService {
const millisecondsDelayBetweenTokenCheck = repeatAfterSeconds * 1000;

return new Observable((subscriber) => {
let intervalId?: number;
let intervalId: number | undefined;

this.zone.runOutsideAngular(() => {
intervalId = setInterval(
intervalId = this.document?.defaultView?.setInterval(
() => this.zone.run(() => subscriber.next()),
millisecondsDelayBetweenTokenCheck
);
Expand Down
Expand Up @@ -43,7 +43,7 @@ describe('PeriodicallyTokenCheckService', () => {
mockProvider(AuthStateService),
mockProvider(RefreshSessionIframeService),
mockProvider(RefreshSessionRefreshTokenService),
IntervalService,
mockProvider(IntervalService),
mockProvider(StoragePersistenceService),
mockProvider(PublicEventsService),
mockProvider(ConfigurationService),
Expand All @@ -67,6 +67,8 @@ describe('PeriodicallyTokenCheckService', () => {
resetAuthDataService = TestBed.inject(ResetAuthDataService);
publicEventsService = TestBed.inject(PublicEventsService);
configurationService = TestBed.inject(ConfigurationService);

spyOn(intervalService, 'startPeriodicTokenCheck').and.returnValue(of(null));
});

afterEach(() => {
Expand Down Expand Up @@ -154,9 +156,6 @@ describe('PeriodicallyTokenCheckService', () => {
{ silentRenew: true, configId: 'configId1', tokenRefreshInSeconds: 1 },
];

spyOn(intervalService, 'startPeriodicTokenCheck').and.returnValue(
of(null)
);
spyOn(
periodicallyTokenCheckService as any,
'shouldStartPeriodicallyCheckForConfig'
Expand Down Expand Up @@ -196,9 +195,6 @@ describe('PeriodicallyTokenCheckService', () => {
{ silentRenew: true, configId: 'configId1', tokenRefreshInSeconds: 1 },
];

spyOn(intervalService, 'startPeriodicTokenCheck').and.returnValue(
of(null)
);
spyOn(
periodicallyTokenCheckService as any,
'shouldStartPeriodicallyCheckForConfig'
Expand Down
Expand Up @@ -22,7 +22,7 @@ describe('RefreshSessionRefreshTokenService', () => {
mockProvider(LoggerService),
mockProvider(FlowsService),
mockProvider(ResetAuthDataService),
IntervalService,
mockProvider(IntervalService),
],
});
});
Expand Down
Expand Up @@ -37,8 +37,8 @@ describe('SilentRenewService ', () => {
mockProvider(AuthStateService),
mockProvider(LoggerService),
mockProvider(ImplicitFlowCallbackService),
mockProvider(IntervalService),
FlowHelper,
IntervalService,
],
});
});
Expand Down

0 comments on commit d2da01e

Please sign in to comment.