Testing
This commit is contained in:
@@ -3,6 +3,9 @@ import type { Config } from 'jest';
|
|||||||
const jestConfig: Config = {
|
const jestConfig: Config = {
|
||||||
preset: 'jest-preset-angular',
|
preset: 'jest-preset-angular',
|
||||||
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
|
setupFilesAfterEnv: ['<rootDir>/setup-jest.ts'],
|
||||||
|
moduleNameMapper: {
|
||||||
|
'@ngxpert/hot-toast': '<rootDir>/mocks/modules/hot-toast',
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export default jestConfig;
|
export default jestConfig;
|
||||||
5
client/mocks/modules/hot-toast.ts
Normal file
5
client/mocks/modules/hot-toast.ts
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
import { map } from "rxjs";
|
||||||
|
|
||||||
|
export class HotToastService {
|
||||||
|
observe = jest.fn().mockImplementation(() => map(x => x))
|
||||||
|
};
|
||||||
11
client/mocks/services/mock.hottoast.service.ts
Normal file
11
client/mocks/services/mock.hottoast.service.ts
Normal 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))
|
||||||
|
};
|
||||||
@@ -5,7 +5,7 @@ import { GridReadyEvent } from 'ag-grid-community';
|
|||||||
import { MatDialog } from '@angular/material/dialog';
|
import { MatDialog } from '@angular/material/dialog';
|
||||||
import { ArchiveComponent } from './archive.component';
|
import { ArchiveComponent } from './archive.component';
|
||||||
import { ApiService } from '../../../../shared/api.service';
|
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
|
// Mocking the dependencies
|
||||||
jest.mock('@ngxpert/hot-toast', () => ({
|
jest.mock('@ngxpert/hot-toast', () => ({
|
||||||
|
|||||||
@@ -1,51 +1,31 @@
|
|||||||
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
import { ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
import { KeysComponent } from './keys.component';
|
import { KeysComponent } from './keys.component';
|
||||||
import { HotToastService, provideHotToastConfig } from '@ngxpert/hot-toast';
|
import { HotToastService } from '@ngxpert/hot-toast';
|
||||||
import { from, map, of } from 'rxjs';
|
import { of } from 'rxjs';
|
||||||
import { ApiService } from '../../shared/api.service';
|
import { ApiService } from '../../shared/api.service';
|
||||||
import { GridReadyEvent } from 'ag-grid-community';
|
import { GridReadyEvent } from 'ag-grid-community';
|
||||||
import { MatDialog } from '@angular/material/dialog';
|
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', () => {
|
describe('KeysComponent', () => {
|
||||||
let component: KeysComponent;
|
let component: KeysComponent;
|
||||||
let fixture: ComponentFixture<KeysComponent>;
|
let fixture: ComponentFixture<KeysComponent>;
|
||||||
|
|
||||||
const mockGridReadyEvent: GridReadyEvent = {
|
const mockGridReadyEvent: GridReadyEvent = {
|
||||||
api: { setGridOption: jest.fn(), addEventListener: jest.fn() },
|
api: { setGridOption: jest.fn(), addEventListener: jest.fn(), getGridOption: jest.fn() },
|
||||||
columnApi: { someColumnApiMethod: jest.fn() },
|
columnApi: { someColumnApiMethod: jest.fn() },
|
||||||
type: 'gridReady',
|
type: 'gridReady',
|
||||||
} as any;
|
} 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 () => {
|
beforeEach(async () => {
|
||||||
await TestBed.configureTestingModule({
|
await TestBed.configureTestingModule({
|
||||||
imports: [KeysComponent, ],
|
imports: [KeysComponent, ],
|
||||||
providers: [
|
providers: [
|
||||||
{ provide: HotToastService, useValue: mockHotToastService },
|
|
||||||
{ provide: ApiService, useClass: MockApiService },
|
{ provide: ApiService, useClass: MockApiService },
|
||||||
{ provide: MatDialog, useValue: mockMatDialog },
|
{ provide: MatDialog, useClass: MockMatDialog },
|
||||||
|
HotToastService
|
||||||
]
|
]
|
||||||
}).compileComponents();
|
}).compileComponents();
|
||||||
});
|
});
|
||||||
@@ -78,7 +58,8 @@ describe('KeysComponent', () => {
|
|||||||
expect(component['api'].deleteKey).toHaveBeenCalledWith(keyId);
|
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 = {
|
const mockEvent = {
|
||||||
data: { id: '1', name: 'Old Name' },
|
data: { id: '1', name: 'Old Name' },
|
||||||
oldValue: 'Old Name',
|
oldValue: 'Old Name',
|
||||||
@@ -100,5 +81,29 @@ describe('KeysComponent', () => {
|
|||||||
expect(component['api'].updateKey).not.toHaveBeenCalled();
|
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)),
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -137,6 +137,7 @@ export class KeysComponent {
|
|||||||
this.gridApi.setGridOption("rowData", d);
|
this.gridApi.setGridOption("rowData", d);
|
||||||
this.loadKeys();
|
this.loadKeys();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user