|
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | +import { UrlTree, Router } from '@angular/router'; |
| 3 | +import { Observable, of as observableOf } from 'rxjs'; |
| 4 | +import { AuthService } from 'src/app/core/auth/auth.service'; |
| 5 | +import { AuthorizationDataService } from 'src/app/core/data/feature-authorization/authorization-data.service'; |
| 6 | +import { FeatureID } from 'src/app/core/data/feature-authorization/feature-id'; |
| 7 | +import { |
| 8 | + Store, |
| 9 | +} from '@ngrx/store'; |
| 10 | + |
| 11 | +import { itemPageEditAuthorizationsGuard } from './item-page-edit-authorizations.guard'; |
| 12 | +import { APP_DATA_SERVICES_MAP } from '../../../config/app-config.interface'; |
| 13 | +import { TranslateService } from '@ngx-translate/core'; |
| 14 | +import { getMockTranslateService } from '../../shared/mocks/translate.service.mock'; |
| 15 | +import { |
| 16 | + dsoPageSingleFeatureGuard |
| 17 | +} from '../../core/data/feature-authorization/feature-authorization-guard/dso-page-single-feature.guard'; |
| 18 | + |
| 19 | +describe('itemPageEditAuthorizationsGuard', () => { |
| 20 | + let authorizationService: AuthorizationDataService; |
| 21 | + let authService: AuthService; |
| 22 | + let router: Router; |
| 23 | + let route; |
| 24 | + let parentRoute; |
| 25 | + let store: Store; |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + |
| 29 | + store = jasmine.createSpyObj('store', { |
| 30 | + dispatch: {}, |
| 31 | + pipe: observableOf(true), |
| 32 | + }); |
| 33 | + authorizationService = jasmine.createSpyObj('authorizationService', { |
| 34 | + isAuthorized: observableOf(true), |
| 35 | + }); |
| 36 | + router = jasmine.createSpyObj('router', { |
| 37 | + parseUrl: {}, |
| 38 | + }); |
| 39 | + authService = jasmine.createSpyObj('authService', { |
| 40 | + isAuthenticated: observableOf(true), |
| 41 | + }); |
| 42 | + |
| 43 | + parentRoute = { |
| 44 | + params: { |
| 45 | + id: '3e1a5327-dabb-41ff-af93-e6cab9d032f0', |
| 46 | + }, |
| 47 | + }; |
| 48 | + route = { |
| 49 | + params: { |
| 50 | + }, |
| 51 | + parent: parentRoute, |
| 52 | + }; |
| 53 | + |
| 54 | + TestBed.configureTestingModule({ |
| 55 | + providers: [ |
| 56 | + { provide: AuthorizationDataService, useValue: authorizationService }, |
| 57 | + { provide: Router, useValue: router }, |
| 58 | + { provide: AuthService, useValue: authService }, |
| 59 | + { provide: Store, useValue: store }, |
| 60 | + { provide: APP_DATA_SERVICES_MAP, useValue: {} }, |
| 61 | + { provide: TranslateService, useValue: getMockTranslateService() }, |
| 62 | + ], |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should call authorizationService.isAuthorized with the appropriate arguments', (done) => { |
| 67 | + const result$ = TestBed.runInInjectionContext(() => { |
| 68 | + return itemPageEditAuthorizationsGuard(route, { url: 'current-url' } as any); |
| 69 | + }) as Observable<boolean | UrlTree>; |
| 70 | + |
| 71 | + console.log('will subscribe'); |
| 72 | + result$.subscribe((result) => { |
| 73 | + console.log('result inside subscribe:', result); |
| 74 | + expect(authorizationService.isAuthorized).toHaveBeenCalledWith( |
| 75 | + FeatureID.CanManagePolicies, |
| 76 | + 'fake-object-url', |
| 77 | + 'fake-eperson-uuid', |
| 78 | + ); |
| 79 | + done(); |
| 80 | + }); |
| 81 | + |
| 82 | + }); |
| 83 | +}); |
0 commit comments