From a3d7471c4a7366d9740d66727d01a979557cfce5 Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Mon, 22 Jan 2024 14:01:24 +0100 Subject: [PATCH] add tests --- .../core/services/pictograms/picto-types.ts | 2 +- .../pictograms/pictogram.service.spec.ts | 73 +++++++++++++++++++ .../dialog-pictograms.component.spec.ts | 48 ++++++++++++ .../toolbar/toolbar.component.spec.ts | 2 +- 4 files changed, 123 insertions(+), 2 deletions(-) create mode 100644 teammapper-frontend/src/app/core/services/pictograms/pictogram.service.spec.ts create mode 100644 teammapper-frontend/src/app/modules/application/components/dialog-pictograms/dialog-pictograms.component.spec.ts diff --git a/teammapper-frontend/src/app/core/services/pictograms/picto-types.ts b/teammapper-frontend/src/app/core/services/pictograms/picto-types.ts index b3abed3a..d100a46b 100644 --- a/teammapper-frontend/src/app/core/services/pictograms/picto-types.ts +++ b/teammapper-frontend/src/app/core/services/pictograms/picto-types.ts @@ -15,7 +15,7 @@ export type IPictogramResponse = { keyword: string; type: number; plural: string; - hasLocution: boolean; + hasLocation: boolean; }[]; created: Date; lastUpdated: Date; diff --git a/teammapper-frontend/src/app/core/services/pictograms/pictogram.service.spec.ts b/teammapper-frontend/src/app/core/services/pictograms/pictogram.service.spec.ts new file mode 100644 index 00000000..ee697e34 --- /dev/null +++ b/teammapper-frontend/src/app/core/services/pictograms/pictogram.service.spec.ts @@ -0,0 +1,73 @@ +import { PictogramService } from './pictogram.service'; +import { TestBed } from '@angular/core/testing'; +import { HttpClient } from '@angular/common/http'; +import { + HttpClientTestingModule, + HttpTestingController, +} from '@angular/common/http/testing'; +import { IPictogramResponse } from './picto-types'; + +const testData: IPictogramResponse = { + schematic: false, + sex: false, + violence: false, + aac: false, + aacColor: false, + skin: false, + hair: false, + downloads: 0, + categories: [], + synsets: [], + tags: [], + _id: 1, + created: new Date(), + lastUpdated: new Date(), + keywords: [], +}; + +describe('PictogramService', () => { + let httpClient: HttpClient; + let httpTestingController: HttpTestingController; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HttpClientTestingModule], + }); + + // Inject the http service and test controller for each test + httpClient = TestBed.inject(HttpClient); + httpTestingController = TestBed.inject(HttpTestingController); + }); + + it('fetches pictos', () => { + new PictogramService(httpClient).getPictos('House').subscribe(data => { + expect(data).toEqual([testData]); + }); + const httpRequest = httpTestingController.expectOne( + 'https://api.arasaac.org/v1/pictograms/de/bestsearch/House' + ); + expect(httpRequest.request.method).toBe('GET'); + httpRequest.flush([testData]); + httpTestingController.verify(); + }); + + it('constructs the asset url', () => { + const imageUrl = new PictogramService(httpClient).getPictoImageUrl(3); + expect(imageUrl).toEqual( + 'https://static.arasaac.org/pictograms/3/3_300.png' + ); + }); + + it('gets the image', () => { + const blob: Blob = new Blob(); + new PictogramService(httpClient).getPictoImage(3).subscribe(data => { + expect(data).toEqual(blob); + }); + const httpRequest = httpTestingController.expectOne( + 'https://static.arasaac.org/pictograms/3/3_300.png' + ); + expect(httpRequest.request.method).toBe('GET'); + httpRequest.flush(blob); + httpTestingController.verify(); + }); +}); diff --git a/teammapper-frontend/src/app/modules/application/components/dialog-pictograms/dialog-pictograms.component.spec.ts b/teammapper-frontend/src/app/modules/application/components/dialog-pictograms/dialog-pictograms.component.spec.ts new file mode 100644 index 00000000..2659e4ae --- /dev/null +++ b/teammapper-frontend/src/app/modules/application/components/dialog-pictograms/dialog-pictograms.component.spec.ts @@ -0,0 +1,48 @@ +import { NO_ERRORS_SCHEMA } from '@angular/core'; +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; +import { TranslateModule } from '@ngx-translate/core'; + +import { MatMenuModule } from '@angular/material/menu'; +import { DialogPictogramsComponent } from './dialog-pictograms.component'; +import { MmpService } from 'src/app/core/services/mmp/mmp.service'; +import { UtilsService } from 'src/app/core/services/utils/utils.service'; +import { PictogramService } from 'src/app/core/services/pictograms/pictogram.service'; + +describe('DialogPictogramsComponent', () => { + let component: DialogPictogramsComponent; + let fixture: ComponentFixture; + + beforeEach(waitForAsync(() => { + const mockMmpService: jasmine.SpyObj = jasmine.createSpyObj( + MmpService, + ['new'] + ); + const mockUtilsService: jasmine.SpyObj = jasmine.createSpyObj( + UtilsService, + ['new'] + ); + const mockPictoService: jasmine.SpyObj = + jasmine.createSpyObj(PictogramService, ['new']); + + TestBed.configureTestingModule({ + declarations: [DialogPictogramsComponent], + schemas: [NO_ERRORS_SCHEMA], + providers: [ + { provide: MmpService, useValue: mockMmpService }, + { provide: UtilsService, useValue: mockUtilsService }, + { provide: PictogramService, useValue: mockPictoService }, + ], + imports: [TranslateModule.forRoot(), MatMenuModule], + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(DialogPictogramsComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/teammapper-frontend/src/app/modules/application/components/toolbar/toolbar.component.spec.ts b/teammapper-frontend/src/app/modules/application/components/toolbar/toolbar.component.spec.ts index 9b874e35..b0a01ef6 100644 --- a/teammapper-frontend/src/app/modules/application/components/toolbar/toolbar.component.spec.ts +++ b/teammapper-frontend/src/app/modules/application/components/toolbar/toolbar.component.spec.ts @@ -1,11 +1,11 @@ import { NO_ERRORS_SCHEMA } from '@angular/core'; import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; -import { MatLegacyMenuModule as MatMenuModule } from '@angular/material/legacy-menu'; import { TranslateModule } from '@ngx-translate/core'; import { DialogService } from 'src/app/core/services/dialog/dialog.service'; import { MmpService } from 'src/app/core/services/mmp/mmp.service'; import { ToolbarComponent } from './toolbar.component'; +import { MatMenuModule } from '@angular/material/menu'; describe('ToolbarComponent', () => { let component: ToolbarComponent;