From 819af7455bb1dfea788ab0bbdade81003d6f6e58 Mon Sep 17 00:00:00 2001 From: Bastian Wagner Date: Thu, 28 Nov 2024 16:59:23 +0100 Subject: [PATCH] Testing --- client/jest.config.ts | 3 + client/mocks/modules/hot-toast.ts | 5 ++ .../mocks/services/mock.api.service.ts | 0 .../mocks/services/mock.hottoast.service.ts | 11 ++++ .../archive/archive.component.spec.ts | 2 +- .../app/modules/keys/keys.component.spec.ts | 59 ++++++++++--------- client/src/app/modules/keys/keys.component.ts | 1 + 7 files changed, 53 insertions(+), 28 deletions(-) create mode 100644 client/mocks/modules/hot-toast.ts rename client/{src => }/mocks/services/mock.api.service.ts (100%) create mode 100644 client/mocks/services/mock.hottoast.service.ts diff --git a/client/jest.config.ts b/client/jest.config.ts index 3473c78..19c5f3b 100644 --- a/client/jest.config.ts +++ b/client/jest.config.ts @@ -3,6 +3,9 @@ import type { Config } from 'jest'; const jestConfig: Config = { preset: 'jest-preset-angular', setupFilesAfterEnv: ['/setup-jest.ts'], + moduleNameMapper: { + '@ngxpert/hot-toast': '/mocks/modules/hot-toast', + }, }; export default jestConfig; \ No newline at end of file diff --git a/client/mocks/modules/hot-toast.ts b/client/mocks/modules/hot-toast.ts new file mode 100644 index 0000000..0ea13a4 --- /dev/null +++ b/client/mocks/modules/hot-toast.ts @@ -0,0 +1,5 @@ +import { map } from "rxjs"; + +export class HotToastService { + observe = jest.fn().mockImplementation(() => map(x => x)) +}; diff --git a/client/src/mocks/services/mock.api.service.ts b/client/mocks/services/mock.api.service.ts similarity index 100% rename from client/src/mocks/services/mock.api.service.ts rename to client/mocks/services/mock.api.service.ts diff --git a/client/mocks/services/mock.hottoast.service.ts b/client/mocks/services/mock.hottoast.service.ts new file mode 100644 index 0000000..c3601fa --- /dev/null +++ b/client/mocks/services/mock.hottoast.service.ts @@ -0,0 +1,11 @@ +import { map } from "rxjs"; + +jest.mock('@ngxpert/hot-toast', () => ({ + HotToastService: { + observe: jest.fn().mockImplementation(() => map(x => x)) + }, +})); + +export class MockHotToastService { + observe = jest.fn().mockImplementation(() => map(x => x)) +}; \ No newline at end of file diff --git a/client/src/app/modules/keys/components/archive/archive.component.spec.ts b/client/src/app/modules/keys/components/archive/archive.component.spec.ts index 416a4d8..ea08e93 100644 --- a/client/src/app/modules/keys/components/archive/archive.component.spec.ts +++ b/client/src/app/modules/keys/components/archive/archive.component.spec.ts @@ -5,7 +5,7 @@ import { GridReadyEvent } from 'ag-grid-community'; import { MatDialog } from '@angular/material/dialog'; import { ArchiveComponent } from './archive.component'; import { ApiService } from '../../../../shared/api.service'; -import { MockApiService } from '../../../../../mocks/services/mock.api.service'; +import { MockApiService } from '../../../../../../mocks/services/mock.api.service'; // Mocking the dependencies jest.mock('@ngxpert/hot-toast', () => ({ diff --git a/client/src/app/modules/keys/keys.component.spec.ts b/client/src/app/modules/keys/keys.component.spec.ts index 17b3e1e..f28a456 100644 --- a/client/src/app/modules/keys/keys.component.spec.ts +++ b/client/src/app/modules/keys/keys.component.spec.ts @@ -1,51 +1,31 @@ import { ComponentFixture, TestBed } from '@angular/core/testing'; import { KeysComponent } from './keys.component'; -import { HotToastService, provideHotToastConfig } from '@ngxpert/hot-toast'; -import { from, map, of } from 'rxjs'; +import { HotToastService } from '@ngxpert/hot-toast'; +import { of } from 'rxjs'; import { ApiService } from '../../shared/api.service'; import { GridReadyEvent } from 'ag-grid-community'; import { MatDialog } from '@angular/material/dialog'; -import { CreateKeyComponent } from './create/create.component'; -import { MockApiService } from '../../../mocks/services/mock.api.service'; +import { MockApiService } from '../../../../mocks/services/mock.api.service'; -// Mocking the dependencies -jest.mock('@ngxpert/hot-toast', () => ({ - HotToastService: jest.fn(), -})); describe('KeysComponent', () => { let component: KeysComponent; let fixture: ComponentFixture; const mockGridReadyEvent: GridReadyEvent = { - api: { setGridOption: jest.fn(), addEventListener: jest.fn() }, + api: { setGridOption: jest.fn(), addEventListener: jest.fn(), getGridOption: jest.fn() }, columnApi: { someColumnApiMethod: jest.fn() }, type: 'gridReady', } as any; - const mockHotToastService = { - observe: jest.fn().mockImplementation(() => ({ - loading: 'speichern...', - success: 'Änderungen gespeichert', - error: 'Änderungen konnten nicht gespeichert werden!', - subscribe: jest.fn().mockReturnValue(of([])) - })) - }; - - const mockMatDialog = { - open: jest.fn().mockReturnValue({ - afterClosed: jest.fn().mockReturnValue(of(true)), - }), - }; - beforeEach(async () => { await TestBed.configureTestingModule({ imports: [KeysComponent, ], providers: [ - { provide: HotToastService, useValue: mockHotToastService }, { provide: ApiService, useClass: MockApiService }, - { provide: MatDialog, useValue: mockMatDialog }, + { provide: MatDialog, useClass: MockMatDialog }, + HotToastService ] }).compileComponents(); }); @@ -78,7 +58,8 @@ describe('KeysComponent', () => { expect(component['api'].deleteKey).toHaveBeenCalledWith(keyId); }); - it.skip('should call updateKey on cellEditEnd when a value is changed', () => { + it('should call updateKey on cellEditEnd when a value is changed', () => { + const mockEvent = { data: { id: '1', name: 'Old Name' }, oldValue: 'Old Name', @@ -100,5 +81,29 @@ describe('KeysComponent', () => { expect(component['api'].updateKey).not.toHaveBeenCalled(); }); + it('should reload Keys after creation', () => { + component['dialog'].open = jest.fn().mockReturnValue({ + afterClosed: jest.fn().mockReturnValue(of(true)), + }) + component.openCreateKey(); + expect(component['dialog'].open).toHaveBeenCalled(); + expect(component['api'].getKeys).toHaveBeenCalledTimes(2) + }) + + it('should not reload Keys after cancellation', () => { + component['dialog'].open = jest.fn().mockReturnValue({ + afterClosed: jest.fn().mockReturnValue(of(null)), + }) + component.openCreateKey(); + expect(component['dialog'].open).toHaveBeenCalled(); + expect(component['api'].getKeys).toHaveBeenCalledTimes(1) + }) + }); +class MockMatDialog { + open = jest.fn().mockReturnValue({ + afterClosed: jest.fn().mockReturnValue(of(true)), + }) +}; + diff --git a/client/src/app/modules/keys/keys.component.ts b/client/src/app/modules/keys/keys.component.ts index 09d5759..c14a33f 100644 --- a/client/src/app/modules/keys/keys.component.ts +++ b/client/src/app/modules/keys/keys.component.ts @@ -137,6 +137,7 @@ export class KeysComponent { this.gridApi.setGridOption("rowData", d); this.loadKeys(); } + } }) }