Skip to content

Commit 5532ef3

Browse files
committed
added more jest tests and mock data
1 parent 406a99f commit 5532ef3

File tree

6 files changed

+204
-1
lines changed

6 files changed

+204
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
import {Component} from '@angular/core';
2+
import {ComponentFixture, TestBed} from '@angular/core/testing';
3+
import {By} from '@angular/platform-browser';
4+
import {NgxDatatableDblClickDirective} from './ngx-datatable-doubleclick.directive';
5+
import {Model} from '@swimlane/ngx-datatable';
6+
7+
@Component({
8+
template: `
9+
<div
10+
onDblClick
11+
(onDblClick)="onRowDblClick($event)">
12+
</div>
13+
`,
14+
})
15+
class TestComponent {
16+
onRowDblClick = jasmine.createSpy('onRowDblClick');
17+
}
18+
19+
describe('NgxDatatableDblClickDirective', () => {
20+
let fixture: ComponentFixture<TestComponent>;
21+
22+
beforeEach(() => {
23+
TestBed.configureTestingModule({
24+
imports: [NgxDatatableDblClickDirective], // Use `imports` for standalone directives
25+
declarations: [TestComponent],
26+
});
27+
fixture = TestBed.createComponent(TestComponent);
28+
fixture.detectChanges();
29+
});
30+
31+
it('should add "onDblClick" class to the host element', () => {
32+
const hostElement = fixture.debugElement.query(By.directive(NgxDatatableDblClickDirective));
33+
expect(hostElement.nativeElement.classList).toContain('onDblClick');
34+
});
35+
36+
it('should emit "dblClick" event when a dblclick event with a row is received', () => {
37+
const hostElement = fixture.debugElement.query(By.directive(NgxDatatableDblClickDirective));
38+
const directive = hostElement.injector.get(NgxDatatableDblClickDirective);
39+
40+
const testRow = {id: 1, name: 'Test Row'};
41+
const mockEvent: Model = {
42+
type: 'dblclick',
43+
row: testRow,
44+
event: new MouseEvent('dblclick'),
45+
rowElement: document.createElement('tr'),
46+
cellElement: document.createElement('td'),
47+
cellIndex: 0,
48+
};
49+
50+
directive.onActivate(mockEvent);
51+
52+
fixture.detectChanges();
53+
const testComponent = fixture.componentInstance;
54+
expect(testComponent.onRowDblClick).toHaveBeenCalledWith(testRow);
55+
});
56+
57+
it('should not emit "dblClick" event for non-dblclick events', () => {
58+
const hostElement = fixture.debugElement.query(By.directive(NgxDatatableDblClickDirective));
59+
const directive = hostElement.injector.get(NgxDatatableDblClickDirective);
60+
61+
const mockEvent: Model = {
62+
type: 'click',
63+
row: {id: 1, name: 'Test Row'},
64+
event: new MouseEvent('click'),
65+
rowElement: document.createElement('tr'),
66+
cellElement: document.createElement('td'),
67+
cellIndex: 0,
68+
};
69+
70+
directive.onActivate(mockEvent);
71+
72+
fixture.detectChanges();
73+
const testComponent = fixture.componentInstance;
74+
expect(testComponent.onRowDblClick).not.toHaveBeenCalled();
75+
});
76+
});

src/main/js/app/model/demo.mock.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import {Demo, DemoFilter} from './demo.model';
2+
3+
export const demoFilterMock : DemoFilter = {
4+
eingangVon: '20241224',
5+
eingangBis: '20241230',
6+
system: 'ABC',
7+
nachrichtentyp: 'BCD',
8+
nummer1: '12345678',
9+
nummer2: '23456789',
10+
};
11+
12+
export const demoListMock : Demo[] = [{
13+
name: 'Name 1',
14+
gender: 'Gender 1',
15+
company: 'Company 1'
16+
}];
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import {ProblemDetail} from './problemdetail.model';
2+
3+
export const problemdetailMock : ProblemDetail = {
4+
todo: 'TODO'
5+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import {TestBed} from '@angular/core/testing';
2+
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';
3+
import {provideGermanDate} from './german-date.provider';
4+
import {GermanDateAdapter} from './german-date.provider';
5+
6+
describe('GermanDateAdapter', () => {
7+
let adapter: GermanDateAdapter;
8+
9+
beforeEach(() => {
10+
TestBed.configureTestingModule({
11+
providers: provideGermanDate(),
12+
});
13+
14+
adapter = TestBed.inject(DateAdapter) as GermanDateAdapter;
15+
});
16+
17+
describe('parse', () => {
18+
it('should parse a valid German date string', () => {
19+
const date = adapter.parse('25.12.2024');
20+
expect(date).toEqual(new Date(2024, 11, 25)); // Months are 0-indexed in JavaScript
21+
});
22+
23+
it('should return null for an invalid German date string', () => {
24+
const date = adapter.parse('31.02.2024'); // Invalid date
25+
expect(date).toBeNull();
26+
});
27+
28+
it('should parse a timestamp', () => {
29+
const timestamp = new Date(2024, 11, 25).getTime();
30+
const date = adapter.parse(timestamp);
31+
expect(date).toEqual(new Date(2024, 11, 25));
32+
});
33+
34+
it('should return null for an invalid value', () => {
35+
const date = adapter.parse('invalid-date');
36+
expect(date).toBeNull();
37+
});
38+
});
39+
40+
describe('format', () => {
41+
it('should format a date into German format', () => {
42+
const date = new Date(2024, 11, 25); // December 25, 2024
43+
const formatted = adapter.format(date, {});
44+
expect(formatted).toBe('25.12.2024');
45+
});
46+
});
47+
});
48+
49+
describe('provideGermanDate', () => {
50+
it('should provide the correct providers', () => {
51+
const providers = provideGermanDate(); // This is an array of providers
52+
const matDateLocaleProvider = providers.find(
53+
(p): p is { provide: typeof MAT_DATE_LOCALE; useValue: string } =>
54+
(p as any).provide === MAT_DATE_LOCALE
55+
);
56+
const dateAdapterProvider = providers.find(
57+
(p): p is { provide: typeof DateAdapter; useClass: typeof GermanDateAdapter } =>
58+
(p as any).provide === DateAdapter
59+
);
60+
const matDateFormatsProvider = providers.find(
61+
(p): p is { provide: typeof MAT_DATE_FORMATS; useValue: any } =>
62+
(p as any).provide === MAT_DATE_FORMATS
63+
);
64+
65+
expect(matDateLocaleProvider?.useValue).toBe('de-DE');
66+
expect(dateAdapterProvider?.useClass).toBe(GermanDateAdapter);
67+
expect(matDateFormatsProvider?.useValue).toEqual({
68+
parse: { dateInput: ['dd.MM.yyyy'] },
69+
display: {
70+
dateInput: 'dd.MM.yyyy',
71+
monthYearLabel: 'DD.MM.yyyy',
72+
dateA11yLabel: 'LL',
73+
monthYearA11yLabel: 'MMMM yyyy',
74+
},
75+
});
76+
});
77+
});

src/main/js/app/providers/german-date.provider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE, NativeDateAdapter} from
22
import {Provider} from '@angular/core';
33
import moment from 'moment';
44

5-
class GermanDateAdapter extends NativeDateAdapter {
5+
export class GermanDateAdapter extends NativeDateAdapter {
66
override parse(value: any): Date | null {
77
if (typeof value === 'string' && value.indexOf('.') > -1) {
88
const parsedDate = moment(value, 'DD.MM.YYYY', true);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { searchDemosAction, searchDemosActionSuccess, searchDemosActionFail, DemoActionTypes } from './demo.actions';
2+
import {demoFilterMock, demoListMock} from '../../model/demo.mock';
3+
import {problemdetailMock} from '../../model/problemdetail.mock';
4+
5+
describe('Demo Actions', () => {
6+
it('should create the SEARCH_DEMOS action with the correct payload', () => {
7+
const filter = demoFilterMock;
8+
const action = searchDemosAction({ filter });
9+
10+
expect(action.type).toBe(DemoActionTypes.SEARCH_DEMOS);
11+
expect(action.filter).toEqual(filter);
12+
});
13+
14+
it('should create the SEARCH_DEMOS_SUCCESS action with the correct payload', () => {
15+
const demoList = demoListMock;
16+
const action = searchDemosActionSuccess({ demoList });
17+
18+
expect(action.type).toBe(DemoActionTypes.SEARCH_DEMOS_SUCCESS);
19+
expect(action.demoList).toEqual(demoList);
20+
});
21+
22+
it('should create the SEARCH_DEMOS_FAIL action with the correct payload', () => {
23+
const error = problemdetailMock;
24+
const action = searchDemosActionFail({ error });
25+
26+
expect(action.type).toBe(DemoActionTypes.SEARCH_DEMOS_FAIL);
27+
expect(action.error).toEqual(error);
28+
});
29+
});

0 commit comments

Comments
 (0)