From f4a784909bbfdea59972e1734c4546b285ab7425 Mon Sep 17 00:00:00 2001 From: Meina Zhou Date: Wed, 25 Sep 2019 13:03:57 +0800 Subject: [PATCH] add more unit test Signed-off-by: Meina Zhou --- src/portal/karma.conf.js | 12 ++-- src/portal/lib/karma.conf.js | 6 +- .../gc-history/gc-history.component.spec.ts | 49 +++++++++++++++ .../change-password.component.spec.ts | 41 ++++++++++++ .../change-password.component.ts | 12 ++-- .../app/user/new-user-modal.component.spec.ts | 48 ++++++++++++++ .../src/app/user/user.component.spec.ts | 62 +++++++++++++++++++ src/portal/src/app/user/user.service.spec.ts | 18 ++++++ 8 files changed, 233 insertions(+), 15 deletions(-) create mode 100644 src/portal/lib/src/config/gc/gc-history/gc-history.component.spec.ts create mode 100644 src/portal/src/app/user/change-password/change-password.component.spec.ts create mode 100644 src/portal/src/app/user/new-user-modal.component.spec.ts create mode 100644 src/portal/src/app/user/user.component.spec.ts create mode 100644 src/portal/src/app/user/user.service.spec.ts diff --git a/src/portal/karma.conf.js b/src/portal/karma.conf.js index 51746db1a..56f21893b 100644 --- a/src/portal/karma.conf.js +++ b/src/portal/karma.conf.js @@ -31,7 +31,7 @@ module.exports = function (config) { fixWebpackSourcePaths: true, // Omit files with no statements, no functions and no branches from the report - skipFilesWithNoCoverage: true, + skipFilesWithNoCoverage: false, // Most reporters accept additional config options. You can pass these through the `report-config` option 'report-config': { @@ -45,13 +45,13 @@ module.exports = function (config) { // enforce percentage thresholds // anything under these percentages will cause karma to fail with an exit code of 1 if not running in watch mode thresholds: { - emitWarning: false, // set to `true` to not fail the test command when thresholds are not met + emitWarning: true, // set to `true` to not fail the test command when thresholds are not met // thresholds for all files global: { - statements: 47, - branches: 18, - functions: 27, - lines: 47 + statements: 40, + branches: 13, + functions: 26, + lines: 41 }, // thresholds per file each: { diff --git a/src/portal/lib/karma.conf.js b/src/portal/lib/karma.conf.js index e0158eda8..00f8ceff3 100644 --- a/src/portal/lib/karma.conf.js +++ b/src/portal/lib/karma.conf.js @@ -31,7 +31,7 @@ module.exports = function (config) { fixWebpackSourcePaths: true, // Omit files with no statements, no functions and no branches from the report - skipFilesWithNoCoverage: true, + skipFilesWithNoCoverage: false, // Most reporters accept additional config options. You can pass these through the `report-config` option 'report-config': { @@ -45,11 +45,11 @@ module.exports = function (config) { // enforce percentage thresholds // anything under these percentages will cause karma to fail with an exit code of 1 if not running in watch mode thresholds: { - emitWarning: false, // set to `true` to not fail the test command when thresholds are not met + emitWarning: true, // set to `true` to not fail the test command when thresholds are not met // thresholds for all files global: { statements: 37, - branches: 20, + branches: 19, functions: 28, lines: 36 }, diff --git a/src/portal/lib/src/config/gc/gc-history/gc-history.component.spec.ts b/src/portal/lib/src/config/gc/gc-history/gc-history.component.spec.ts new file mode 100644 index 000000000..4e6ef5a00 --- /dev/null +++ b/src/portal/lib/src/config/gc/gc-history/gc-history.component.spec.ts @@ -0,0 +1,49 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { SharedModule } from '../../../shared/shared.module'; +import { GcRepoService } from "../gc.service"; +import { of } from 'rxjs'; +import { GcViewModelFactory } from "../gc.viewmodel.factory"; +import { TranslateModule, TranslateService } from '@ngx-translate/core'; +import { ErrorHandler } from '../../../error-handler'; +import { GcHistoryComponent } from './gc-history.component'; + +describe('GcHistoryComponent', () => { + let component: GcHistoryComponent; + let fixture: ComponentFixture; + let fakeGcRepoService = { + getJobs: function () { + return of([]); + } + }; + let fakeGcViewModelFactory = { + createJobViewModel: function (data) { + return data; + } + }; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [GcHistoryComponent], + imports: [ + SharedModule, + TranslateModule.forRoot() + ], + providers: [ + ErrorHandler, + TranslateService, + { provide: GcRepoService, useValue: fakeGcRepoService }, + { provide: GcViewModelFactory, useValue: fakeGcViewModelFactory } + ] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(GcHistoryComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/portal/src/app/user/change-password/change-password.component.spec.ts b/src/portal/src/app/user/change-password/change-password.component.spec.ts new file mode 100644 index 000000000..0cbc0ef1f --- /dev/null +++ b/src/portal/src/app/user/change-password/change-password.component.spec.ts @@ -0,0 +1,41 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { MessageHandlerService } from '../../shared/message-handler/message-handler.service'; +import { ClarityModule } from '@clr/angular'; +import { TranslateModule, TranslateService } from '@ngx-translate/core'; +import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; +import { UserService } from "../user.service"; +import { SharedModule } from '../../shared/shared.module'; +import { ChangePasswordComponent } from './change-password.component'; + +describe('ChangePasswordComponent', () => { + let component: ChangePasswordComponent; + let fixture: ComponentFixture; + let fakeUserService = null; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [ChangePasswordComponent], + schemas: [ + CUSTOM_ELEMENTS_SCHEMA + ], + imports: [ + ClarityModule, + SharedModule, + TranslateModule.forRoot() + ], + providers: [ + { provide: UserService, useValue: fakeUserService } + ] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(ChangePasswordComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/portal/src/app/user/change-password/change-password.component.ts b/src/portal/src/app/user/change-password/change-password.component.ts index 115b760ad..7aa21b46c 100644 --- a/src/portal/src/app/user/change-password/change-password.component.ts +++ b/src/portal/src/app/user/change-password/change-password.component.ts @@ -11,13 +11,13 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -import {Component, ViewChild, ChangeDetectorRef} from '@angular/core'; +import { Component, ViewChild, ChangeDetectorRef } from '@angular/core'; import { NgForm } from '@angular/forms'; import { InlineAlertComponent } from '../../shared/inline-alert/inline-alert.component'; import { MessageHandlerService } from '../../shared/message-handler/message-handler.service'; -import {UserService} from "../user.service"; -import {TranslateService} from "@ngx-translate/core"; +import { UserService } from "../user.service"; +import { TranslateService } from "@ngx-translate/core"; @Component({ selector: 'change-password', @@ -35,8 +35,8 @@ export class ChangePasswordComponent { confirmPwd: string = ""; userId: number; - @ViewChild("resetPwdForm", {static: false}) resetPwdForm: NgForm; - @ViewChild(InlineAlertComponent, {static: false}) + @ViewChild("resetPwdForm", { static: false }) resetPwdForm: NgForm; + @ViewChild(InlineAlertComponent, { static: false }) inlineAlert: InlineAlertComponent; constructor( @@ -109,7 +109,7 @@ export class ChangePasswordComponent { this.onGoing = false; if (error.status === 400) { this.translateService.get("USER.EXISTING_PASSWORD").subscribe( - res => {this.inlineAlert.showInlineError(res); }); + res => { this.inlineAlert.showInlineError(res); }); } else { this.inlineAlert.showInlineError(error); } diff --git a/src/portal/src/app/user/new-user-modal.component.spec.ts b/src/portal/src/app/user/new-user-modal.component.spec.ts new file mode 100644 index 000000000..da3787622 --- /dev/null +++ b/src/portal/src/app/user/new-user-modal.component.spec.ts @@ -0,0 +1,48 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { ClarityModule } from '@clr/angular'; +import { TranslateModule, TranslateService } from '@ngx-translate/core'; +import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; +import { UserService } from './user.service'; +import { SessionService } from '../shared/session.service'; +import { MessageHandlerService } from '../shared/message-handler/message-handler.service'; +import { SharedModule } from '../shared/shared.module'; +import { NewUserModalComponent } from './new-user-modal.component'; + +describe('NewUserModalComponent', () => { + let component: NewUserModalComponent; + let fixture: ComponentFixture; + let fakeSessionService = null; + let fakeUserService = null; + let fakeMessageHandlerService = { + handleError: function () { } + }; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [NewUserModalComponent], + imports: [ + ClarityModule, + SharedModule, + TranslateModule.forRoot() + ], + schemas: [ + CUSTOM_ELEMENTS_SCHEMA + ], + providers: [ + { provide: MessageHandlerService, useValue: fakeMessageHandlerService }, + { provide: UserService, useValue: fakeUserService }, + { provide: SessionService, useValue: fakeSessionService } + ] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(NewUserModalComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/portal/src/app/user/user.component.spec.ts b/src/portal/src/app/user/user.component.spec.ts new file mode 100644 index 000000000..a3c78ffc5 --- /dev/null +++ b/src/portal/src/app/user/user.component.spec.ts @@ -0,0 +1,62 @@ +import { async, ComponentFixture, TestBed } from '@angular/core/testing'; +import { ClarityModule } from '@clr/angular'; +import { TranslateModule, TranslateService } from '@ngx-translate/core'; +import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core'; +import { UserService } from './user.service'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { ConfirmationDialogService } from '../shared/confirmation-dialog/confirmation-dialog.service'; +import { MessageHandlerService } from '../shared/message-handler/message-handler.service'; +import { AppConfigService } from '../app-config.service'; +import { SessionService } from '../shared/session.service'; +import { OperationService } from '@harbor/ui'; +import { UserComponent } from './user.component'; + +describe('UserComponent', () => { + let component: UserComponent; + let fixture: ComponentFixture; + let fakeSessionService = null; + let fakeAppConfigService = { + getConfig: function () { + return { + auth_mode: 'ldap_auth' + }; + } + }; + let fakeUserService = null; + let fakeMessageHandlerService = { + handleError: function () { } + }; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [UserComponent], + imports: [ + ClarityModule, + TranslateModule.forRoot(), + HttpClientTestingModule + ], + schemas: [ + CUSTOM_ELEMENTS_SCHEMA + ], + providers: [ + TranslateService, + ConfirmationDialogService, + OperationService, + { provide: MessageHandlerService, useValue: fakeMessageHandlerService }, + { provide: UserService, useValue: fakeUserService }, + { provide: SessionService, useValue: fakeSessionService }, + { provide: AppConfigService, useValue: fakeAppConfigService } + ] + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(UserComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/portal/src/app/user/user.service.spec.ts b/src/portal/src/app/user/user.service.spec.ts new file mode 100644 index 000000000..58c912ccf --- /dev/null +++ b/src/portal/src/app/user/user.service.spec.ts @@ -0,0 +1,18 @@ +import { TestBed, inject } from '@angular/core/testing'; +import { HttpClientTestingModule } from '@angular/common/http/testing'; +import { UserService } from './user.service'; + +describe('UserService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [ + HttpClientTestingModule + ], + providers: [UserService] + }); + }); + + it('should be created', inject([UserService], (service: UserService) => { + expect(service).toBeTruthy(); + })); +});