diff --git a/scilog/src/app/app.component.spec.ts b/scilog/src/app/app.component.spec.ts index 10528238..7d448ff9 100644 --- a/scilog/src/app/app.component.spec.ts +++ b/scilog/src/app/app.component.spec.ts @@ -1,6 +1,7 @@ import { TestBed, waitForAsync } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { AppComponent } from './app.component'; +import { LogbookInfoService } from './core/logbook-info.service'; describe('AppComponent', () => { beforeEach(waitForAsync(() => { @@ -8,6 +9,12 @@ describe('AppComponent', () => { imports: [ RouterTestingModule ], + providers: [ + { + provide: LogbookInfoService, + useValue: { logbookInfo: {id: 'id'} } + } + ], declarations: [ AppComponent ], @@ -26,10 +33,16 @@ describe('AppComponent', () => { // expect(app.title).toEqual('SciLog'); // }); - // it('should render title', () => { - // const fixture = TestBed.createComponent(AppComponent); - // fixture.detectChanges(); - // const compiled = fixture.nativeElement; - // expect(compiled.querySelector('.content span').textContent).toContain('scilog app is running!'); - // }); + it('should unset the logbook', () => { + const fixture = TestBed.createComponent(AppComponent); + const app = fixture.componentInstance; + const popStateEvent = new PopStateEvent('popstate'); + Object.defineProperty( + popStateEvent, + 'target', + {writable: false, value: {location: {pathname: '/overview'}}} + ); + window.dispatchEvent(popStateEvent); + expect(app['logbookInfo'].logbookInfo).toBeNull(); + }); }); diff --git a/scilog/src/app/app.component.ts b/scilog/src/app/app.component.ts index 4c6bb443..47122e06 100644 --- a/scilog/src/app/app.component.ts +++ b/scilog/src/app/app.component.ts @@ -1,4 +1,5 @@ -import { Component, OnInit} from '@angular/core'; +import { Component, HostListener, OnInit } from '@angular/core'; +import { LogbookInfoService } from './core/logbook-info.service'; @Component({ @@ -11,7 +12,7 @@ export class AppComponent implements OnInit { light_mode = true; disable_log = false; - constructor() { + constructor(private logbookInfo: LogbookInfoService) { } ngOnInit(): void { @@ -30,6 +31,10 @@ export class AppComponent implements OnInit { } } -} - + @HostListener('window:popstate', ['$event']) + onPopState(event: Event & {target: Window}) { + if (event.target.location.pathname === '/overview') + this.logbookInfo.logbookInfo = null; + } +}