|
| 1 | +import { |
| 2 | + ComponentFixture, |
| 3 | + TestBed, |
| 4 | +} from '@angular/core/testing'; |
| 5 | +import { ActivatedRoute } from '@angular/router'; |
| 6 | +import { Store } from '@ngrx/store'; |
| 7 | +import { TranslateModule } from '@ngx-translate/core'; |
| 8 | +import { MockComponent } from 'ng-mocks'; |
| 9 | +import { |
| 10 | + Observable, |
| 11 | + of as observableOf, |
| 12 | + of, |
| 13 | +} from 'rxjs'; |
| 14 | +import { AuthService } from 'src/app/core/auth/auth.service'; |
| 15 | +import { AuthTokenInfo } from 'src/app/core/auth/models/auth-token-info.model'; |
| 16 | +import { ObjectCacheService } from 'src/app/core/cache/object-cache.service'; |
| 17 | +import { SiteDataService } from 'src/app/core/data/site-data.service'; |
| 18 | +import { LocaleService } from 'src/app/core/locale/locale.service'; |
| 19 | +import { CookieService } from 'src/app/core/services/cookie.service'; |
| 20 | +import { |
| 21 | + NativeWindowRef, |
| 22 | + NativeWindowService, |
| 23 | +} from 'src/app/core/services/window.service'; |
| 24 | +import { Site } from 'src/app/core/shared/site.model'; |
| 25 | +import { CookieServiceMock } from 'src/app/shared/mocks/cookie.service.mock'; |
| 26 | + |
| 27 | +import { MarkdownViewerComponent } from '../../shared/markdown-viewer/markdown-viewer.component'; |
| 28 | +import { CmsInfoComponent } from './cms-info.component'; |
| 29 | + |
| 30 | +describe('CmsInfoComponent', () => { |
| 31 | + let component: CmsInfoComponent; |
| 32 | + let fixture: ComponentFixture<CmsInfoComponent>; |
| 33 | + let authService: AuthService; |
| 34 | + let token: AuthTokenInfo; |
| 35 | + let store; |
| 36 | + let redirectUrl: string; |
| 37 | + let activatedRouteStub: any; |
| 38 | + let siteServiceStub: any; |
| 39 | + |
| 40 | + const site: Site = Object.assign(new Site(), { |
| 41 | + metadata: { |
| 42 | + 'dc.rights' : [{ |
| 43 | + value: 'English text', |
| 44 | + language: 'en', |
| 45 | + }, |
| 46 | + { |
| 47 | + value: 'German text', |
| 48 | + language: 'de', |
| 49 | + }], |
| 50 | + }, |
| 51 | + }); |
| 52 | + |
| 53 | + beforeEach(async () => { |
| 54 | + redirectUrl = 'redirect/url'; |
| 55 | + token = new AuthTokenInfo('eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9'); |
| 56 | + authService = jasmine.createSpyObj('authService', { |
| 57 | + isAuthenticated: observableOf(true), |
| 58 | + getToken: token, |
| 59 | + }); |
| 60 | + store = jasmine.createSpyObj('store', ['dispatch']); |
| 61 | + activatedRouteStub = { |
| 62 | + data: observableOf({ schema: 'cris', qualifier: 'testQualifier' }), |
| 63 | + queryParamMap: observableOf({}), |
| 64 | + }; |
| 65 | + siteServiceStub = { |
| 66 | + find(): Observable<Site> { |
| 67 | + return of(site); |
| 68 | + }, |
| 69 | + }; |
| 70 | + await TestBed.configureTestingModule({ |
| 71 | + imports: [ |
| 72 | + TranslateModule.forRoot(), |
| 73 | + CmsInfoComponent, |
| 74 | + MockComponent(MarkdownViewerComponent), |
| 75 | + ], |
| 76 | + providers: [ |
| 77 | + LocaleService, |
| 78 | + ObjectCacheService, |
| 79 | + { provide: SiteDataService, useValue: siteServiceStub }, |
| 80 | + { provide: Store, useValue: store }, |
| 81 | + { provide: AuthService, useValue: authService }, |
| 82 | + { provide: CookieService, useValue: new CookieServiceMock }, |
| 83 | + { provide: NativeWindowService, useValue: new NativeWindowRef() }, |
| 84 | + { provide: ActivatedRoute, useValue: activatedRouteStub }, |
| 85 | + ], |
| 86 | + }) |
| 87 | + .compileComponents(); |
| 88 | + |
| 89 | + fixture = TestBed.createComponent(CmsInfoComponent); |
| 90 | + component = fixture.componentInstance; |
| 91 | + fixture.detectChanges(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should create', () => { |
| 95 | + expect(component).toBeTruthy(); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should set headLabel$ when data is successfully retrieved', () => { |
| 99 | + const headLabelSpy = spyOn(component.headLabel$, 'next'); |
| 100 | + |
| 101 | + component.ngOnInit(); |
| 102 | + |
| 103 | + expect(headLabelSpy).toHaveBeenCalledOnceWith('info.testQualifier.head'); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should log a warning to console if metadata content is missing', () => { |
| 107 | + spyOn(console, 'warn'); |
| 108 | + site.metadata['cris.cms.testQualifier'] = undefined; |
| 109 | + |
| 110 | + component.ngOnInit(); |
| 111 | + |
| 112 | + expect(console.warn).toHaveBeenCalledWith('Metadata cris.cms.testQualifier has no content'); |
| 113 | + }); |
| 114 | +}); |
0 commit comments