Browse Source

test(admin-ui): Create testing helpers

Michael Bromley 7 years ago
parent
commit
77f13809ef

+ 0 - 43
admin-ui/src/app/catalog/components/create-option-group-dialog/create-option-group-dialog.component.spec.ts

@@ -1,43 +0,0 @@
-import { async, ComponentFixture, TestBed } from '@angular/core/testing';
-import { ReactiveFormsModule } from '@angular/forms';
-import { IconCustomTag, Tooltip, TooltipContent, TooltipTrigger } from '@clr/angular';
-
-import { MockTranslatePipe } from '../../../../testing/translate.pipe.mock';
-import { DataService } from '../../../data/providers/data.service';
-import { MockDataService } from '../../../data/providers/data.service.mock';
-import { FormFieldControlDirective } from '../../../shared/components/form-field/form-field-control.directive';
-import { FormFieldComponent } from '../../../shared/components/form-field/form-field.component';
-
-import { CreateOptionGroupDialogComponent } from './create-option-group-dialog.component';
-
-describe('CreateOptionGroupDialogComponent', () => {
-    let component: CreateOptionGroupDialogComponent;
-    let fixture: ComponentFixture<CreateOptionGroupDialogComponent>;
-
-    beforeEach(async(() => {
-        TestBed.configureTestingModule({
-            imports: [ReactiveFormsModule],
-            declarations: [
-                CreateOptionGroupDialogComponent,
-                FormFieldComponent,
-                FormFieldControlDirective,
-                MockTranslatePipe,
-                IconCustomTag,
-                TooltipContent,
-                TooltipTrigger,
-                Tooltip,
-            ],
-            providers: [{ provide: DataService, useClass: MockDataService }],
-        }).compileComponents();
-    }));
-
-    beforeEach(() => {
-        fixture = TestBed.createComponent(CreateOptionGroupDialogComponent);
-        component = fixture.componentInstance;
-    });
-
-    it('should create', () => {
-        fixture.detectChanges();
-        expect(component).toBeTruthy();
-    });
-});

+ 4 - 0
admin-ui/src/app/catalog/components/create-option-group-form/create-option-group-form.component.spec.ts

@@ -1,4 +1,7 @@
 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { ReactiveFormsModule } from '@angular/forms';
+
+import { TestingCommonModule } from '../../../../testing/testing-common.module';
 
 import { CreateOptionGroupFormComponent } from './create-option-group-form.component';
 
@@ -8,6 +11,7 @@ describe('CreateOptionGroupFormComponent', () => {
 
     beforeEach(async(() => {
         TestBed.configureTestingModule({
+            imports: [TestingCommonModule, ReactiveFormsModule],
             declarations: [CreateOptionGroupFormComponent],
         }).compileComponents();
     }));

+ 0 - 32
admin-ui/src/app/catalog/components/select-option-group-dialog/select-option-group-dialog.component.spec.ts

@@ -1,32 +0,0 @@
-import { async, ComponentFixture, TestBed } from '@angular/core/testing';
-import { ReactiveFormsModule } from '@angular/forms';
-import { ClrIconCustomTag } from '@clr/angular';
-
-import { MockTranslatePipe } from '../../../../testing/translate.pipe.mock';
-import { DataService } from '../../../data/providers/data.service';
-import { MockDataService } from '../../../data/providers/data.service.mock';
-
-import { SelectOptionGroupDialogComponent } from './select-option-group-dialog.component';
-
-describe('SelectOptionGroupDialogComponent', () => {
-    let component: SelectOptionGroupDialogComponent;
-    let fixture: ComponentFixture<SelectOptionGroupDialogComponent>;
-
-    beforeEach(async(() => {
-        TestBed.configureTestingModule({
-            imports: [ReactiveFormsModule],
-            declarations: [SelectOptionGroupDialogComponent, ClrIconCustomTag, MockTranslatePipe],
-            providers: [{ provide: DataService, useClass: MockDataService }],
-        }).compileComponents();
-    }));
-
-    beforeEach(() => {
-        fixture = TestBed.createComponent(SelectOptionGroupDialogComponent);
-        component = fixture.componentInstance;
-        fixture.detectChanges();
-    });
-
-    it('should create', () => {
-        expect(component).toBeTruthy();
-    });
-});

+ 10 - 1
admin-ui/src/app/catalog/components/select-option-group/select-option-group.component.spec.ts

@@ -1,4 +1,11 @@
 import { async, ComponentFixture, TestBed } from '@angular/core/testing';
+import { ReactiveFormsModule } from '@angular/forms';
+import { ClrIconCustomTag } from '@clr/angular';
+import { TranslateModule } from '@ngx-translate/core';
+
+import { DataService } from '../../../data/providers/data.service';
+import { MockDataService } from '../../../data/providers/data.service.mock';
+import { SelectToggleComponent } from '../../../shared/components/select-toggle/select-toggle.component';
 
 import { SelectOptionGroupComponent } from './select-option-group.component';
 
@@ -8,7 +15,9 @@ describe('SelectOptionGroupComponent', () => {
 
     beforeEach(async(() => {
         TestBed.configureTestingModule({
-            declarations: [SelectOptionGroupComponent],
+            imports: [ReactiveFormsModule, TranslateModule.forRoot()],
+            declarations: [SelectOptionGroupComponent, SelectToggleComponent, ClrIconCustomTag],
+            providers: [{ provide: DataService, useClass: MockDataService }],
         }).compileComponents();
     }));
 

+ 11 - 0
admin-ui/src/app/core/providers/notification/notification.service.mock.ts

@@ -0,0 +1,11 @@
+import { MockOf } from '../../../../testing/testing-types';
+
+import { NotificationService } from './notification.service';
+
+export class MockNotificationService implements MockOf<NotificationService> {
+    notify = jasmine.createSpy('notify');
+    error = jasmine.createSpy('error');
+    info = jasmine.createSpy('info');
+    success = jasmine.createSpy('success');
+    warning = jasmine.createSpy('warning');
+}

+ 27 - 0
admin-ui/src/testing/testing-common.module.ts

@@ -0,0 +1,27 @@
+import { CommonModule } from '@angular/common';
+import { NgModule, Provider } from '@angular/core';
+import { ClarityModule } from '@clr/angular';
+
+import { DataService } from '../app/data/providers/data.service';
+import { MockDataService } from '../app/data/providers/data.service.mock';
+import { FormFieldControlDirective } from '../app/shared/components/form-field/form-field-control.directive';
+import { FormFieldComponent } from '../app/shared/components/form-field/form-field.component';
+
+import { MockTranslatePipe } from './translate.pipe.mock';
+
+const DECLARATIONS = [MockTranslatePipe, FormFieldComponent, FormFieldControlDirective];
+
+const PROVIDERS: Provider[] = [{ provide: DataService, useClass: MockDataService }];
+
+/**
+ * This module is for use in unit testing, and provides common directives and providers
+ * that are used across most component, reducing the boilerplate needed to declare these
+ * in each individual test.
+ */
+@NgModule({
+    imports: [CommonModule, ClarityModule],
+    exports: [...DECLARATIONS, ClarityModule],
+    declarations: DECLARATIONS,
+    providers: PROVIDERS,
+})
+export class TestingCommonModule {}

+ 1 - 0
admin-ui/src/testing/testing-types.ts

@@ -0,0 +1 @@
+export type MockOf<T> = { [K in keyof T]: T[K] };