From 7f8313145f4c02fbe37a02418d411469ffe10c3c Mon Sep 17 00:00:00 2001 From: JsteReubsSoftware Date: Tue, 19 Sep 2023 12:23:02 +0200 Subject: [PATCH] Added more integration tests --- .../dashboard-page.component.spec.ts | 95 ++++++++++++++++++- .../dashboard-page.component.ts | 8 +- 2 files changed, 97 insertions(+), 6 deletions(-) diff --git a/libs/app/components/src/lib/dashboard-page/dashboard-page.component.spec.ts b/libs/app/components/src/lib/dashboard-page/dashboard-page.component.spec.ts index ebd262d3..96c7ad95 100644 --- a/libs/app/components/src/lib/dashboard-page/dashboard-page.component.spec.ts +++ b/libs/app/components/src/lib/dashboard-page/dashboard-page.component.spec.ts @@ -1,7 +1,7 @@ -import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; import { DashboardPageComponent } from './dashboard-page.component'; import { NgIconsModule, provideIcons } from '@ng-icons/core'; -import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { Router } from '@angular/router'; @@ -9,16 +9,23 @@ import { matKeyboardDoubleArrowUp, matKeyboardDoubleArrowDown } from "@ng-icons/ import { matFilterCenterFocus, matZoomIn, matZoomOut } from "@ng-icons/material-icons/baseline"; import { heroUserGroupSolid } from "@ng-icons/heroicons/solid"; import { heroBackward } from "@ng-icons/heroicons/outline"; +import { IGetEventFloorlayoutResponse, IGetFloorplanBoundariesResponse, IImage } from '@event-participation-trends/api/event/util'; +import { HttpClient } from '@angular/common/http'; +import { AppApiService } from '@event-participation-trends/app/api'; describe('DashboardPageComponent', () => { let component: DashboardPageComponent; let fixture: ComponentFixture; let router: Router; + let httpClient: HttpClient; + let httpTestingController: HttpTestingController; + let appApiService: AppApiService; beforeEach(async () => { await TestBed.configureTestingModule({ imports: [DashboardPageComponent, NgIconsModule, HttpClientTestingModule, RouterTestingModule], providers: [ + AppApiService, provideIcons({heroUserGroupSolid, heroBackward, matKeyboardDoubleArrowUp, matKeyboardDoubleArrowDown, matFilterCenterFocus, matZoomIn, matZoomOut}) ], }).compileComponents(); @@ -28,6 +35,8 @@ describe('DashboardPageComponent', () => { fixture.detectChanges(); router = TestBed.inject(Router); + httpClient = TestBed.inject(HttpClient); + appApiService = TestBed.inject(AppApiService); }); it('should create', () => { @@ -46,4 +55,86 @@ describe('DashboardPageComponent', () => { component.ngOnInit(); expect(router.navigate).toHaveBeenCalledWith(['/home']); }); + + it('should get event floorplan boundaries', () => { + httpTestingController = TestBed.inject(HttpTestingController); + httpTestingController.expectOne(`/api/user/getRole`); + // mock response + const response: IGetFloorplanBoundariesResponse = { + boundaries: { + top: 0, + bottom: 0, + left: 0, + right: 0 + } + }; + component.id = '1'; + + httpClient.get(`/api/event/getFloorplanBoundaries?eventId=${component.id}`).subscribe(res => { + component.floorlayoutBounds = res.boundaries; + + expect(component.floorlayoutBounds).toEqual(response.boundaries); + }); + + const req = httpTestingController.expectOne(`/api/event/getFloorplanBoundaries?eventId=${component.id}`); + expect(req.request.method).toEqual('GET'); + + req.flush(response); + + httpTestingController.verify(); + }); + + it('should get floorlayout images', () => { + httpTestingController = TestBed.inject(HttpTestingController); + httpTestingController.expectOne(`/api/user/getRole`); + // mock response + const response: IImage[] =[ + { + eventId: undefined, + imageBase64: 'image1', + imageObj: undefined, + imageScale: 1, + imageType: 'image/png', + } + ]; + + component.id = '1'; + + httpClient.get(`/api/event/getFloorlayoutImages?eventId=${component.id}`).subscribe(res => { + component.floorlayoutImages = res; + + expect(component.floorlayoutImages).toEqual(response); + }); + + const req = httpTestingController.expectOne(`/api/event/getFloorlayoutImages?eventId=${component.id}`); + expect(req.request.method).toEqual('GET'); + + req.flush(response); + + httpTestingController.verify(); + }); + + it('should get event floorlayout', () => { + httpTestingController = TestBed.inject(HttpTestingController); + httpTestingController.expectOne(`/api/user/getRole`); + // mock response + const response: IGetEventFloorlayoutResponse = { + floorlayout: '' + }; + + component.id = '1'; + + httpClient.get(`/api/event/getFloorlayout?eventId=${component.id}`).subscribe(res => { + component.floorlayoutSnapshot = res.floorlayout!; + + expect(component.floorlayoutSnapshot).toEqual(response); + }); + + const req = httpTestingController.expectOne(`/api/event/getFloorlayout?eventId=${component.id}`); + expect(req.request.method).toEqual('GET'); + + req.flush(response); + + httpTestingController.verify(); + }); }); diff --git a/libs/app/components/src/lib/dashboard-page/dashboard-page.component.ts b/libs/app/components/src/lib/dashboard-page/dashboard-page.component.ts index 5a5fde05..63adfe54 100644 --- a/libs/app/components/src/lib/dashboard-page/dashboard-page.component.ts +++ b/libs/app/components/src/lib/dashboard-page/dashboard-page.component.ts @@ -1,6 +1,6 @@ import { CommonModule } from "@angular/common"; import { NgIconsModule, provideIcons } from "@ng-icons/core"; -import { Component, OnInit, ViewChild, ElementRef, HostListener, AfterViewInit } from '@angular/core'; +import { Component, OnInit, ViewChild, ElementRef, HostListener, AfterViewInit, NgZone } from '@angular/core'; import * as L from 'leaflet'; import 'leaflet.heat'; import Chart, { ChartConfiguration } from 'chart.js/auto'; @@ -152,7 +152,7 @@ export class DashboardPageComponent implements OnInit, AfterViewInit { detectionRadius = 2; // ==================================== - constructor(private appApiService: AppApiService, private router : Router, private route: ActivatedRoute) {} + constructor(private appApiService: AppApiService, private router : Router, private route: ActivatedRoute, private ngZone: NgZone) {} public id = ''; public event : any | null = null; @@ -164,11 +164,11 @@ export class DashboardPageComponent implements OnInit, AfterViewInit { this.id = this.route.parent?.snapshot.paramMap.get('id') || ''; if (!this.id) { - this.router.navigate(['/home']); + this.ngZone.run(() => { this.router.navigate(['/home']); }); } if (!(await this.hasAccess())) { - this.router.navigate(['/home']); + this.ngZone.run(() => { this.router.navigate(['/home']); }); } this.timeOffset = (new Date()).getTimezoneOffset() * 60 * 1000;