oidc-client-rx/src/public-events/public-events.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

70 lines
2.2 KiB
TypeScript

import { TestBed, waitForAsync } from '@angular/core/testing';
import { filter } from 'rxjs/operators';
import { EventTypes } from './event-types';
import { PublicEventsService } from './public-events.service';
describe('Events Service', () => {
let eventsService: PublicEventsService;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [PublicEventsService],
});
});
beforeEach(() => {
eventsService = TestBed.inject(PublicEventsService);
});
it('should create', () => {
expect(eventsService).toBeTruthy();
});
it('registering to single event with one event emit works', waitForAsync(() => {
eventsService.registerForEvents().subscribe((firedEvent) => {
expect(firedEvent).toBeTruthy();
expect(firedEvent).toEqual({
type: EventTypes.ConfigLoaded,
value: { myKey: 'myValue' },
});
});
eventsService.fireEvent(EventTypes.ConfigLoaded, { myKey: 'myValue' });
}));
it('registering to single event with multiple same event emit works', waitForAsync(() => {
const spy = jasmine.createSpy('spy');
eventsService.registerForEvents().subscribe((firedEvent) => {
spy(firedEvent);
expect(firedEvent).toBeTruthy();
});
eventsService.fireEvent(EventTypes.ConfigLoaded, { myKey: 'myValue' });
eventsService.fireEvent(EventTypes.ConfigLoaded, { myKey: 'myValue2' });
expect(spy.calls.count()).toBe(2);
expect(spy.calls.first().args[0]).toEqual({
type: EventTypes.ConfigLoaded,
value: { myKey: 'myValue' },
});
expect(spy.calls.mostRecent().args[0]).toEqual({
type: EventTypes.ConfigLoaded,
value: { myKey: 'myValue2' },
});
}));
it('registering to single event with multiple emit works', waitForAsync(() => {
eventsService
.registerForEvents()
.pipe(filter((x) => x.type === EventTypes.ConfigLoaded))
.subscribe((firedEvent) => {
expect(firedEvent).toBeTruthy();
expect(firedEvent).toEqual({
type: EventTypes.ConfigLoaded,
value: { myKey: 'myValue' },
});
});
eventsService.fireEvent(EventTypes.ConfigLoaded, { myKey: 'myValue' });
eventsService.fireEvent(EventTypes.NewAuthenticationResult, true);
}));
});