Skip to content

Commit 7b8ae78

Browse files
committed
Implement form preview tests
1 parent 7ca4a57 commit 7b8ae78

File tree

2 files changed

+128
-22
lines changed

2 files changed

+128
-22
lines changed
Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,85 @@
11
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { MatDialogModule, MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
3-
2+
import { MatDialogModule, MAT_DIALOG_DATA } from '@angular/material/dialog';
3+
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
44
import { AdminEventFormPreviewDialogComponent } from './admin-event-form-preview-dialog.component';
55

6-
describe('AdminEventFormPreviewDialogComponent', () => {
7-
let component: AdminEventFormPreviewDialogComponent;
8-
let fixture: ComponentFixture<AdminEventFormPreviewDialogComponent>;
6+
import { AdminEventFormPreviewComponent } from './admin-event-form-preview.component';
7+
8+
describe('AdminEventFormPreviewComponent', () => {
9+
let component: AdminEventFormPreviewDialogComponent
10+
let fixture: ComponentFixture<AdminEventFormPreviewDialogComponent>
911

1012
beforeEach(async(() => {
1113
TestBed.configureTestingModule({
12-
declarations: [ AdminEventFormPreviewDialogComponent ],
13-
imports: [MatDialogModule],
14+
imports: [BrowserAnimationsModule, MatDialogModule],
15+
declarations: [AdminEventFormPreviewComponent],
1416
providers: [{
15-
provide: MatDialogRef, useValue: {}
16-
}, {
17-
provide: MAT_DIALOG_DATA, useValue: {
18-
fields: []
17+
provide: MAT_DIALOG_DATA,
18+
useValue: {
19+
name: 'Mock Form',
20+
fields: [{
21+
id: 1,
22+
archived: true,
23+
name: 'field1',
24+
title: 'Field 1',
25+
type: 'textfield'
26+
},{
27+
id: 3,
28+
archived: false,
29+
name: 'field3',
30+
title: 'Field 3',
31+
type: 'textfield'
32+
},{
33+
id: 2,
34+
archived: false,
35+
name: 'field2',
36+
title: 'Field 2',
37+
type: 'textfield'
38+
}]
1939
}
2040
}]
2141
})
22-
.compileComponents();
42+
.compileComponents()
2343
}));
2444

2545
beforeEach(() => {
2646
fixture = TestBed.createComponent(AdminEventFormPreviewDialogComponent);
2747
component = fixture.componentInstance;
28-
fixture.detectChanges();
48+
fixture.detectChanges()
2949
});
3050

3151
it('should create', () => {
3252
expect(component).toBeTruthy();
3353
});
54+
55+
it('should have id', () => {
56+
const id = component.formGroup.get('id')
57+
expect(id).toBeDefined()
58+
expect(id.value).toEqual(0)
59+
});
60+
61+
it('should have formId', () => {
62+
const formId = component.formGroup.get('formId')
63+
expect(formId).toBeDefined()
64+
expect(formId.value).toEqual(0)
65+
});
66+
67+
it('should filter archived fields', () => {
68+
const nonArchivedFieldNames = component.formDefinition.fields.filter(field => {
69+
return !field.archived
70+
}).map(field => field.name)
71+
72+
const controlPaths = Object.keys(component.formGroup.controls).filter(path => {
73+
return path !== 'id' && path !== 'formId'
74+
})
75+
76+
controlPaths.forEach(path => {
77+
expect(nonArchivedFieldNames).toContain(path)
78+
})
79+
});
80+
81+
it('should sort fields', () => {
82+
const controlPaths = Object.keys(component.formGroup.controls)
83+
expect(controlPaths).toEqual(['id', 'formId', 'field2', 'field3'])
84+
});
3485
});
Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,82 @@
1+
import { Component, ViewChild } from '@angular/core';
12
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
2-
import { MatDialogModule } from '@angular/material/dialog';
3+
import { MatDialog, MatDialogModule } from '@angular/material/dialog';
4+
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
5+
import { Observable, of } from 'rxjs';
36

47
import { AdminEventFormPreviewComponent } from './admin-event-form-preview.component';
58

9+
@Component({
10+
template: '<admin-event-form-preview [formDefinition]="formDefinition"></admin-event-form-preview>'
11+
})
12+
class TestHostComponent {
13+
formDefinition: any
14+
15+
@ViewChild(AdminEventFormPreviewComponent, { static: true })
16+
public adminEventFormPreviewComponent: AdminEventFormPreviewComponent
17+
}
18+
19+
class MatDialogMock {
20+
open(): any {
21+
return {
22+
afterClosed: (): Observable<boolean> => of(true)
23+
}
24+
}
25+
}
26+
627
describe('AdminEventFormPreviewComponent', () => {
7-
let component: AdminEventFormPreviewComponent;
8-
let fixture: ComponentFixture<AdminEventFormPreviewComponent>;
28+
let component: AdminEventFormPreviewComponent
29+
let hostComponent: TestHostComponent
30+
let fixture: ComponentFixture<TestHostComponent>
31+
let dialog: MatDialog
932

1033
beforeEach(async(() => {
1134
TestBed.configureTestingModule({
12-
imports: [MatDialogModule],
13-
declarations: [ AdminEventFormPreviewComponent ]
35+
imports: [BrowserAnimationsModule, MatDialogModule],
36+
declarations: [AdminEventFormPreviewComponent, TestHostComponent],
37+
providers: [{
38+
provide: MatDialog, useClass: MatDialogMock
39+
}]
1440
})
15-
.compileComponents();
41+
.compileComponents()
1642
}));
1743

1844
beforeEach(() => {
19-
fixture = TestBed.createComponent(AdminEventFormPreviewComponent);
20-
component = fixture.componentInstance;
21-
fixture.detectChanges();
45+
fixture = TestBed.createComponent(TestHostComponent)
46+
hostComponent = fixture.componentInstance
47+
component = hostComponent.adminEventFormPreviewComponent
48+
dialog = TestBed.inject(MatDialog)
49+
fixture.detectChanges()
2250
});
2351

2452
it('should create', () => {
2553
expect(component).toBeTruthy();
2654
});
55+
56+
it('should open dialog', () => {
57+
spyOn(dialog, 'open').and.callThrough()
58+
59+
hostComponent.formDefinition = {
60+
fields: []
61+
}
62+
63+
fixture.detectChanges()
64+
65+
expect(dialog.open).toHaveBeenCalled()
66+
});
67+
68+
it('should close dialog', async(async () => {
69+
spyOn(component.onClose, 'emit')
70+
spyOn(component.dialog, 'open').and.callThrough()
71+
72+
hostComponent.formDefinition = {
73+
name: 'Mock Form 1',
74+
fields: []
75+
}
76+
77+
fixture.detectChanges()
78+
79+
expect(dialog.open).toHaveBeenCalled()
80+
expect(component.onClose.emit).toHaveBeenCalled()
81+
}));
2782
});

0 commit comments

Comments
 (0)