import {ComponentFixture, fakeAsync, TestBed, tick} from '@angular/core/testing'; import {RoleSelectorComponent} from './role-selector.component'; import {AngularMaterialModule} from '../modules/angular-material.module'; import {UserAuthorizationType, UserContextService} from '../services/user-context.service'; import {HttpClientTestingModule} from '@angular/common/http/testing'; import {NoopAnimationsModule} from '@angular/platform-browser/animations'; import {of} from 'rxjs'; import {SorItem, SorService} from '../services/sor.service'; import {Injectable} from '@angular/core'; import {DialogService} from '../modals/dialog/dialog.service'; @Injectable({ providedIn: 'root' }) export class UserContextServiceMock { public selectedRole$ = of({authorizationCode: 'test', educationCode: 'test', educationType: 'test'}); public userContext$ = of({ commonName: 'Lars Larsen', organizationName: 'Company A/S', eligibleRoles: [ { authorizationCode: 'J0184', educationCode: '7170', educationType: 'Læge' }, { authorizationCode: 'LK34Q', educationCode: '9495', educationType: 'Bandagist' }] }); selectedOrganizationSor$ = of({institutionOwnerSorId: 'test', name: 'test', sorId: 'test'}); selectOrganizationSor(): void { } selectRole(): void { } } describe('RoleSelectorComponents\'s', () => { describe('lookup functionality', () => { let component: RoleSelectorComponent; let fixture: ComponentFixture; let sorService: SorService; let userContextService: UserContextService; beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [RoleSelectorComponent], imports: [AngularMaterialModule, NoopAnimationsModule, HttpClientTestingModule], providers: [ {provide: UserContextService, useClass: UserContextServiceMock}, {provide: DialogService, useValue: {close: () => undefined}}, ] }) .compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(RoleSelectorComponent); component = fixture.componentInstance; sorService = TestBed.inject(SorService); userContextService = TestBed.inject(UserContextService); fixture.detectChanges(); }); it('should create', () => { expect(component).toBeTruthy(); }); it('should lookup', fakeAsync(() => { const spy = spyOn(sorService, 'lookUp').and.returnValue(of([{name: 'Trifork', sorId: '293591000016003'}])); component.organizationFormControl.setValue('trifork'); tick(600); component.organizationFormControl.setValue('trifork1234'); tick(600); expect(spy).toHaveBeenCalled(); })); it('should select sor', fakeAsync(() => { const sor: SorItem = {institutionOwnerSorId: 'test', name: 'test', sorId: 'test'}; const spy = spyOn(userContextService, 'selectOrganizationSor'); component.selectSor(sor); expect(spy).toHaveBeenCalled(); })); it('should select role', fakeAsync(() => { const role: UserAuthorizationType = {authorizationCode: 'test', educationCode: 'test', educationType: 'test'}; const spy = spyOn(userContextService, 'selectRole'); component.selectRole(role); expect(spy).toHaveBeenCalled(); })); }); describe('way to handle being both dialog and standalone component', () => { let component: RoleSelectorComponent; let fixture: ComponentFixture; let sorService: SorService; let userContextService: UserContextService; beforeEach(async () => { await TestBed.configureTestingModule({ declarations: [RoleSelectorComponent], imports: [AngularMaterialModule, NoopAnimationsModule, HttpClientTestingModule], providers: [ {provide: UserContextService, useClass: UserContextServiceMock}, ] }) .compileComponents(); }); beforeEach(() => { fixture = TestBed.createComponent(RoleSelectorComponent); component = fixture.componentInstance; sorService = TestBed.inject(SorService); userContextService = TestBed.inject(UserContextService); fixture.detectChanges(); }); it('does not error when trying to close dialog when its not in dialog-mode', () => { const role: UserAuthorizationType = {authorizationCode: 'test', educationCode: 'test', educationType: 'test'}; expect(() => component.selectRole(role)).not.toThrow( new Error('Could not close dialog - did you try to close it without opening it?') ); }); it('does not eat other errors', () => { (component as any).dialogService = {close: () => {throw new Error('Some other error'); }}; const role: UserAuthorizationType = {authorizationCode: 'test', educationCode: 'test', educationType: 'test'}; expect(() => component.selectRole(role)).toThrow(new Error('Some other error')); }); }); });