Skip to content

Commit

Permalink
added more jest tests and mock data
Browse files Browse the repository at this point in the history
  • Loading branch information
hofiorg committed Dec 3, 2024
1 parent 406a99f commit 5532ef3
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {Component} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {By} from '@angular/platform-browser';
import {NgxDatatableDblClickDirective} from './ngx-datatable-doubleclick.directive';
import {Model} from '@swimlane/ngx-datatable';

@Component({
template: `
<div
onDblClick
(onDblClick)="onRowDblClick($event)">
</div>
`,
})
class TestComponent {
onRowDblClick = jasmine.createSpy('onRowDblClick');
}

describe('NgxDatatableDblClickDirective', () => {
let fixture: ComponentFixture<TestComponent>;

beforeEach(() => {
TestBed.configureTestingModule({
imports: [NgxDatatableDblClickDirective], // Use `imports` for standalone directives
declarations: [TestComponent],
});
fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
});

it('should add "onDblClick" class to the host element', () => {
const hostElement = fixture.debugElement.query(By.directive(NgxDatatableDblClickDirective));
expect(hostElement.nativeElement.classList).toContain('onDblClick');
});

it('should emit "dblClick" event when a dblclick event with a row is received', () => {
const hostElement = fixture.debugElement.query(By.directive(NgxDatatableDblClickDirective));
const directive = hostElement.injector.get(NgxDatatableDblClickDirective);

const testRow = {id: 1, name: 'Test Row'};
const mockEvent: Model = {
type: 'dblclick',
row: testRow,
event: new MouseEvent('dblclick'),
rowElement: document.createElement('tr'),
cellElement: document.createElement('td'),
cellIndex: 0,
};

directive.onActivate(mockEvent);

fixture.detectChanges();
const testComponent = fixture.componentInstance;
expect(testComponent.onRowDblClick).toHaveBeenCalledWith(testRow);
});

it('should not emit "dblClick" event for non-dblclick events', () => {
const hostElement = fixture.debugElement.query(By.directive(NgxDatatableDblClickDirective));
const directive = hostElement.injector.get(NgxDatatableDblClickDirective);

const mockEvent: Model = {
type: 'click',
row: {id: 1, name: 'Test Row'},
event: new MouseEvent('click'),
rowElement: document.createElement('tr'),
cellElement: document.createElement('td'),
cellIndex: 0,
};

directive.onActivate(mockEvent);

fixture.detectChanges();
const testComponent = fixture.componentInstance;
expect(testComponent.onRowDblClick).not.toHaveBeenCalled();
});
});
16 changes: 16 additions & 0 deletions src/main/js/app/model/demo.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Demo, DemoFilter} from './demo.model';

export const demoFilterMock : DemoFilter = {
eingangVon: '20241224',
eingangBis: '20241230',
system: 'ABC',
nachrichtentyp: 'BCD',
nummer1: '12345678',
nummer2: '23456789',
};

export const demoListMock : Demo[] = [{
name: 'Name 1',
gender: 'Gender 1',
company: 'Company 1'
}];
5 changes: 5 additions & 0 deletions src/main/js/app/model/problemdetail.mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import {ProblemDetail} from './problemdetail.model';

export const problemdetailMock : ProblemDetail = {
todo: 'TODO'
}
77 changes: 77 additions & 0 deletions src/main/js/app/providers/german-date.provider.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import {TestBed} from '@angular/core/testing';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
import {provideGermanDate} from './german-date.provider';
import {GermanDateAdapter} from './german-date.provider';

describe('GermanDateAdapter', () => {
let adapter: GermanDateAdapter;

beforeEach(() => {
TestBed.configureTestingModule({
providers: provideGermanDate(),
});

adapter = TestBed.inject(DateAdapter) as GermanDateAdapter;
});

describe('parse', () => {
it('should parse a valid German date string', () => {
const date = adapter.parse('25.12.2024');
expect(date).toEqual(new Date(2024, 11, 25)); // Months are 0-indexed in JavaScript
});

it('should return null for an invalid German date string', () => {
const date = adapter.parse('31.02.2024'); // Invalid date
expect(date).toBeNull();
});

it('should parse a timestamp', () => {
const timestamp = new Date(2024, 11, 25).getTime();
const date = adapter.parse(timestamp);
expect(date).toEqual(new Date(2024, 11, 25));
});

it('should return null for an invalid value', () => {
const date = adapter.parse('invalid-date');
expect(date).toBeNull();
});
});

describe('format', () => {
it('should format a date into German format', () => {
const date = new Date(2024, 11, 25); // December 25, 2024
const formatted = adapter.format(date, {});
expect(formatted).toBe('25.12.2024');
});
});
});

describe('provideGermanDate', () => {
it('should provide the correct providers', () => {
const providers = provideGermanDate(); // This is an array of providers
const matDateLocaleProvider = providers.find(
(p): p is { provide: typeof MAT_DATE_LOCALE; useValue: string } =>
(p as any).provide === MAT_DATE_LOCALE
);
const dateAdapterProvider = providers.find(
(p): p is { provide: typeof DateAdapter; useClass: typeof GermanDateAdapter } =>
(p as any).provide === DateAdapter
);
const matDateFormatsProvider = providers.find(
(p): p is { provide: typeof MAT_DATE_FORMATS; useValue: any } =>
(p as any).provide === MAT_DATE_FORMATS
);

expect(matDateLocaleProvider?.useValue).toBe('de-DE');
expect(dateAdapterProvider?.useClass).toBe(GermanDateAdapter);
expect(matDateFormatsProvider?.useValue).toEqual({
parse: { dateInput: ['dd.MM.yyyy'] },
display: {
dateInput: 'dd.MM.yyyy',
monthYearLabel: 'DD.MM.yyyy',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM yyyy',
},
});
});
});
2 changes: 1 addition & 1 deletion src/main/js/app/providers/german-date.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE, NativeDateAdapter} from
import {Provider} from '@angular/core';
import moment from 'moment';

class GermanDateAdapter extends NativeDateAdapter {
export class GermanDateAdapter extends NativeDateAdapter {
override parse(value: any): Date | null {
if (typeof value === 'string' && value.indexOf('.') > -1) {
const parsedDate = moment(value, 'DD.MM.YYYY', true);
Expand Down
29 changes: 29 additions & 0 deletions src/main/js/app/store/demo/demo.action.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { searchDemosAction, searchDemosActionSuccess, searchDemosActionFail, DemoActionTypes } from './demo.actions';
import {demoFilterMock, demoListMock} from '../../model/demo.mock';
import {problemdetailMock} from '../../model/problemdetail.mock';

describe('Demo Actions', () => {
it('should create the SEARCH_DEMOS action with the correct payload', () => {
const filter = demoFilterMock;
const action = searchDemosAction({ filter });

expect(action.type).toBe(DemoActionTypes.SEARCH_DEMOS);
expect(action.filter).toEqual(filter);
});

it('should create the SEARCH_DEMOS_SUCCESS action with the correct payload', () => {
const demoList = demoListMock;
const action = searchDemosActionSuccess({ demoList });

expect(action.type).toBe(DemoActionTypes.SEARCH_DEMOS_SUCCESS);
expect(action.demoList).toEqual(demoList);
});

it('should create the SEARCH_DEMOS_FAIL action with the correct payload', () => {
const error = problemdetailMock;
const action = searchDemosActionFail({ error });

expect(action.type).toBe(DemoActionTypes.SEARCH_DEMOS_FAIL);
expect(action.error).toEqual(error);
});
});

0 comments on commit 5532ef3

Please sign in to comment.