This commit is contained in:
Bastian Wagner
2024-11-28 16:59:23 +01:00
parent 6b933a0400
commit 819af7455b
7 changed files with 53 additions and 28 deletions

View File

@@ -3,6 +3,9 @@ import type { Config } from 'jest';
const jestConfig: Config = {
preset: 'jest-preset-angular',
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
moduleNameMapper: {
'@ngxpert/hot-toast': '<rootDir>/mocks/modules/hot-toast',
},
};
export default jestConfig;

View File

@@ -0,0 +1,5 @@
import { map } from "rxjs";
export class HotToastService {
observe = jest.fn().mockImplementation(() => map(x => x))
};

View File

@@ -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))
};

View File

@@ -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', () => ({

View File

@@ -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<KeysComponent>;
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)),
})
};

View File

@@ -137,6 +137,7 @@ export class KeysComponent {
this.gridApi.setGridOption("rowData", d);
this.loadKeys();
}
}
})
}