oidc-client-rx/src/callback/interval.service.spec.ts
lonelyhentxi 983254164b
Some checks are pending
Build, Lint & Test Lib / Built, Lint and Test Library (push) Waiting to run
Build, Lint & Test Lib / Angular latest (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular latest Standalone & Schematics Job (push) Blocked by required conditions
Build, Lint & Test Lib / Angular 16 & RxJs 6 (push) Blocked by required conditions
Build, Lint & Test Lib / Angular V16 (push) Blocked by required conditions
Docs / Build and Deploy Docs Job (push) Waiting to run
Docs / Close Pull Request Job (push) Waiting to run
feat: init and fork some code from angular-auth-oidc-client
2025-01-17 04:24:12 +08:00

77 lines
1.9 KiB
TypeScript

import { fakeAsync, TestBed, tick } from '@angular/core/testing';
import { Subscription } from 'rxjs';
import { IntervalService } from './interval.service';
describe('IntervalService', () => {
let intervalService: IntervalService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
{
provide: Document,
useValue: {
defaultView: {
setInterval: window.setInterval,
},
},
},
],
});
});
beforeEach(() => {
intervalService = TestBed.inject(IntervalService);
});
it('should create', () => {
expect(intervalService).toBeTruthy();
});
describe('stopPeriodicTokenCheck', () => {
it('calls unsubscribe and sets to null', () => {
intervalService.runTokenValidationRunning = new Subscription();
const spy = spyOn(
intervalService.runTokenValidationRunning,
'unsubscribe'
);
intervalService.stopPeriodicTokenCheck();
expect(spy).toHaveBeenCalled();
expect(intervalService.runTokenValidationRunning).toBeNull();
});
it('does nothing if `runTokenValidationRunning` is null', () => {
intervalService.runTokenValidationRunning = new Subscription();
const spy = spyOn(
intervalService.runTokenValidationRunning,
'unsubscribe'
);
intervalService.runTokenValidationRunning = null;
intervalService.stopPeriodicTokenCheck();
expect(spy).not.toHaveBeenCalled();
});
});
describe('startPeriodicTokenCheck', () => {
it('starts check after correct milliseconds', fakeAsync(() => {
const periodicCheck = intervalService.startPeriodicTokenCheck(0.5);
const spy = jasmine.createSpy();
const sub = periodicCheck.subscribe(() => {
spy();
});
tick(500);
expect(spy).toHaveBeenCalledTimes(1);
tick(500);
expect(spy).toHaveBeenCalledTimes(2);
sub.unsubscribe();
}));
});
});