From 23646a2c1219f12eca8d641bd6f68c4a84bbb3ec Mon Sep 17 00:00:00 2001 From: Matthijs Smets <93487259+MatthijsSmets@users.noreply.github.com> Date: Tue, 15 Oct 2024 17:56:16 +0200 Subject: [PATCH] Fix: show front and backend versions (#726) * feat: show frontend and backend versions, frontend version in browser tab is now correct * refactor: remove unused service * test: add unit tests for setting versions * refactor: remove comments --- src/app/app.component.html | 2 +- src/app/app.component.spec.ts | 43 +++++++++++++------ src/app/app.component.ts | 27 ++++-------- .../table-settings-modal.component.html | 14 ++++++ .../table-settings-modal.component.ts | 19 +++++++- src/app/shared/services/http.service.ts | 6 +++ .../node-link-strategy.service.spec.ts | 16 ------- .../services/node-link-strategy.service.ts | 10 ----- .../shared/services/version.service.spec.ts | 41 ++++++++++++++++++ src/app/shared/services/version.service.ts | 39 +++++++++++++++++ src/styles.css | 12 ++++++ 11 files changed, 169 insertions(+), 60 deletions(-) delete mode 100644 src/app/shared/services/node-link-strategy.service.spec.ts delete mode 100644 src/app/shared/services/node-link-strategy.service.ts create mode 100644 src/app/shared/services/version.service.spec.ts create mode 100644 src/app/shared/services/version.service.ts diff --git a/src/app/app.component.html b/src/app/app.component.html index e070e3af..f5bce3e4 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -16,7 +16,7 @@ }
  • - v{{ appVersion }} + v{{ frontendVersion }}
  • diff --git a/src/app/app.component.spec.ts b/src/app/app.component.spec.ts index b1912f99..2d2a7100 100644 --- a/src/app/app.component.spec.ts +++ b/src/app/app.component.spec.ts @@ -1,27 +1,46 @@ -import { TestBed } from '@angular/core/testing'; -import { RouterTestingModule } from '@angular/router/testing'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; import { AppComponent } from './app.component'; import { provideHttpClientTesting } from '@angular/common/http/testing'; -import { NgbNavModule } from '@ng-bootstrap/ng-bootstrap'; import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'; +import { Title } from '@angular/platform-browser'; +import { VersionService } from './shared/services/version.service'; +import { provideRouter } from '@angular/router'; + +class MockVersionService { + getFrontendVersion = jasmine.createSpy('getFrontendVersion').and.returnValue(Promise.resolve('1.0-TEST')); +} describe('AppComponent', () => { + let component: AppComponent; + let fixture: ComponentFixture; + let titleService: Title; + beforeEach(async () => { await TestBed.configureTestingModule({ - imports: [RouterTestingModule, NgbNavModule, AppComponent], - providers: [provideHttpClient(withInterceptorsFromDi()), provideHttpClientTesting()], + providers: [ + provideHttpClient(withInterceptorsFromDi()), + provideHttpClientTesting(), + provideRouter([]), + { provide: VersionService, useClass: MockVersionService }, + Title, + ], }).compileComponents(); + + fixture = TestBed.createComponent(AppComponent); + component = fixture.componentInstance; + titleService = TestBed.inject(Title); }); it('should create the app', () => { - const fixture = TestBed.createComponent(AppComponent); - const app = fixture.componentInstance; - expect(app).toBeTruthy(); + expect(component).toBeTruthy(); }); - it(`should have as title 'ladybug'`, () => { - const fixture = TestBed.createComponent(AppComponent); - const app = fixture.componentInstance; - expect(app.title).toEqual('ladybug'); + it('should set the title correctly based on frontend version from version service', async () => { + spyOn(titleService, 'setTitle'); + + await component.fetchAndSetFrontendVersion(); + + expect(titleService.setTitle).toHaveBeenCalledWith('Ladybug - v1.0-TEST'); + expect(component.frontendVersion).toEqual('1.0-TEST'); }); }); diff --git a/src/app/app.component.ts b/src/app/app.component.ts index b20f3e6a..87a990ad 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -18,6 +18,7 @@ import { CloseTab } from './shared/interfaces/close-tab'; import { HttpService } from './shared/services/http.service'; import { StubStrategy } from './shared/enums/stub-strategy'; import { ErrorHandling } from './shared/classes/error-handling.service'; +import { VersionService } from './shared/services/version.service'; @Component({ selector: 'app-root', @@ -27,7 +28,7 @@ import { ErrorHandling } from './shared/classes/error-handling.service'; imports: [RouterLinkActive, RouterLink, RouterOutlet, ToastComponent], }) export class AppComponent implements OnInit, OnDestroy { - appVersion: string = '0.0.0'; + frontendVersion?: string; @ViewChild(CompareComponent) compareComponent!: CompareComponent; @ViewChild(TestComponent) testComponent!: TestComponent; @@ -51,12 +52,11 @@ export class AppComponent implements OnInit, OnDestroy { private location: Location, private httpService: HttpService, private errorHandler: ErrorHandling, - ) { - this.titleService.setTitle(`Ladybug - v${this.appVersion}`); - } + private versionService: VersionService, + ) {} ngOnInit(): void { - this.fetchAndSetAppVersion(); + this.fetchAndSetFrontendVersion(); this.subscribeToServices(); this.getStubStrategies(); } @@ -65,20 +65,9 @@ export class AppComponent implements OnInit, OnDestroy { this.unsubscribeAll(); } - fetchAndSetAppVersion() { - fetch('assets/package.json') - .then((response: Response): void => { - if (response.ok) { - response.json().then((packageJson: { version: string }): void => { - this.appVersion = packageJson.version; - }); - } else { - console.error('package.json could not be found in assets', response); - } - }) - .catch((error): void => { - console.error('package.json could not be found in assets', error); - }); + async fetchAndSetFrontendVersion() { + this.frontendVersion = await this.versionService.getFrontendVersion(); + this.titleService.setTitle(`Ladybug - v${this.frontendVersion}`); } subscribeToServices(): void { diff --git a/src/app/debug/table/table-settings-modal/table-settings-modal.component.html b/src/app/debug/table/table-settings-modal/table-settings-modal.component.html index 3776e7c2..ca20fa7d 100644 --- a/src/app/debug/table/table-settings-modal/table-settings-modal.component.html +++ b/src/app/debug/table/table-settings-modal/table-settings-modal.component.html @@ -10,6 +10,20 @@