From 5dfd379dfbee94f4c3bf40b6df852222c9475509 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Wed, 1 Jun 2022 16:21:48 +0300 Subject: [PATCH 1/4] Fix angular 12 testing issue --- templates/app-nolayers/angular/src/test.ts | 3 +-- templates/app/angular/src/test.ts | 4 ++-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/templates/app-nolayers/angular/src/test.ts b/templates/app-nolayers/angular/src/test.ts index 61925f9803f..85c74fe5319 100644 --- a/templates/app-nolayers/angular/src/test.ts +++ b/templates/app-nolayers/angular/src/test.ts @@ -1,11 +1,10 @@ // This file is required by karma.conf.js and loads recursively all the .spec and framework files - +import 'zone.js/testing'; import { getTestBed } from '@angular/core/testing'; import { BrowserDynamicTestingModule, platformBrowserDynamicTesting, } from '@angular/platform-browser-dynamic/testing'; -import 'zone.js/testing'; declare const require: { context( diff --git a/templates/app/angular/src/test.ts b/templates/app/angular/src/test.ts index 61925f9803f..5b643ec44dd 100644 --- a/templates/app/angular/src/test.ts +++ b/templates/app/angular/src/test.ts @@ -1,11 +1,11 @@ // This file is required by karma.conf.js and loads recursively all the .spec and framework files - +import 'zone.js/testing'; import { getTestBed } from '@angular/core/testing'; import { BrowserDynamicTestingModule, platformBrowserDynamicTesting, } from '@angular/platform-browser-dynamic/testing'; -import 'zone.js/testing'; + declare const require: { context( From dd53c7b9960ce86a1e59c7226e5ec7819fa93a3b Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Wed, 1 Jun 2022 18:11:33 +0300 Subject: [PATCH 2/4] Add example tests to Home.component.spec.ts --- .../src/app/home/home.component.spec.ts | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 templates/app/angular/src/app/home/home.component.spec.ts diff --git a/templates/app/angular/src/app/home/home.component.spec.ts b/templates/app/angular/src/app/home/home.component.spec.ts new file mode 100644 index 00000000000..3ceff9410b7 --- /dev/null +++ b/templates/app/angular/src/app/home/home.component.spec.ts @@ -0,0 +1,100 @@ +import { CoreTestingModule } from "@abp/ng.core/testing"; +import { ThemeBasicTestingModule } from "@abp/ng.theme.basic/testing"; +import { ThemeSharedTestingModule } from "@abp/ng.theme.shared/testing"; +import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing"; +import { NgxValidateCoreModule } from "@ngx-validate/core"; +import { HomeComponent } from "./home.component"; +import { OAuthService } from 'angular-oauth2-oidc'; +import { AuthService } from '@abp/ng.core'; + + +describe("HomeComponent", () => { + let fixture: ComponentFixture; + const mockOAuthService = jasmine.createSpyObj('OAuthService', ['hasValidAccessToken']) + const mockAuthService = jasmine.createSpyObj('AuthService', ['navigateToLogin']) + beforeEach( + waitForAsync(() => { + TestBed.configureTestingModule({ + declarations: [HomeComponent], + imports: [ + CoreTestingModule.withConfig(), + ThemeSharedTestingModule.withConfig(), + ThemeBasicTestingModule.withConfig(), + NgxValidateCoreModule, + ], + providers: [ + /* mock providers here */ + { + provide: OAuthService, + useValue: mockOAuthService + }, + { + provide: AuthService, + useValue: mockAuthService + } + ], + }).compileComponents(); + }) + ); + + beforeEach(() => { + fixture = TestBed.createComponent(HomeComponent); + fixture.detectChanges(); + }); + + it("should be initiated", () => { + expect(fixture.componentInstance).toBeTruthy(); + }); + + + + describe('when login state is true', () => { + beforeAll(() => { + mockOAuthService.hasValidAccessToken.and.returnValue(true) + }); + + it("hasLoggedIn should be true", () => { + + expect(fixture.componentInstance.hasLoggedIn).toBeTrue(); + expect(mockOAuthService.hasValidAccessToken).toHaveBeenCalled() + }) + + it("button should not be exists", () => { + const element = fixture.nativeElement + const button = element.querySelector('[role="button"]') + expect(button).toBeNull() + }) + + }) + + describe('when login state is false', () => { + beforeAll(() => { + mockOAuthService.hasValidAccessToken.and.returnValue(false) + }); + + it("hasLoggedIn should be false", () => { + + expect(fixture.componentInstance.hasLoggedIn).toBeFalse(); + expect(mockOAuthService.hasValidAccessToken).toHaveBeenCalled() + }) + + it("button should be exists", () => { + const element = fixture.nativeElement + const button = element.querySelector('[role="button"]') + expect(button).toBeDefined() + }) + describe('when button clicked', () => { + + beforeEach(() => { + const element = fixture.nativeElement + const button = element.querySelector('[role="button"]') + button.click() + }); + + it("navigateToLogin have been called", () => { + expect(mockAuthService.navigateToLogin).toHaveBeenCalled() + }) + }) + }) + +}); \ No newline at end of file From adfd61c6e85465d3a4ab7d4a106edbfed5be0138 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Tue, 11 Oct 2022 15:10:30 +0300 Subject: [PATCH 3/4] Fix exists test issues on Module Template --- .../src/lib/my-project-name.component.spec.ts | 20 +++++++++++++------ .../src/lib/my-project-name.service.spec.ts | 15 ++++++++++---- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/templates/module/angular/projects/my-project-name/src/lib/my-project-name.component.spec.ts b/templates/module/angular/projects/my-project-name/src/lib/my-project-name.component.spec.ts index 88f67b1e654..4d6aae3ac8d 100644 --- a/templates/module/angular/projects/my-project-name/src/lib/my-project-name.component.spec.ts +++ b/templates/module/angular/projects/my-project-name/src/lib/my-project-name.component.spec.ts @@ -1,16 +1,24 @@ import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; - -import { MyProjectNameComponent } from './my-project-name.component'; +import { MyProjectNameComponent } from './components/my-project-name.component'; +import { MyProjectNameService } from '@my-company-name/my-project-name'; +import { of } from 'rxjs'; describe('MyProjectNameComponent', () => { let component: MyProjectNameComponent; let fixture: ComponentFixture; - + const mockMyProjectNameService = jasmine.createSpyObj('MyProjectNameService', { + sample: of([]), + }); beforeEach(waitForAsync(() => { TestBed.configureTestingModule({ - declarations: [ MyProjectNameComponent ] - }) - .compileComponents(); + declarations: [MyProjectNameComponent], + providers: [ + { + provide: MyProjectNameService, + useValue: mockMyProjectNameService, + }, + ], + }).compileComponents(); })); beforeEach(() => { diff --git a/templates/module/angular/projects/my-project-name/src/lib/my-project-name.service.spec.ts b/templates/module/angular/projects/my-project-name/src/lib/my-project-name.service.spec.ts index 7955ce073ca..ec7aeb6c68d 100644 --- a/templates/module/angular/projects/my-project-name/src/lib/my-project-name.service.spec.ts +++ b/templates/module/angular/projects/my-project-name/src/lib/my-project-name.service.spec.ts @@ -1,12 +1,19 @@ import { TestBed } from '@angular/core/testing'; - -import { MyProjectNameService } from './my-project-name.service'; +import { MyProjectNameService } from './services/my-project-name.service'; +import { RestService } from '@abp/ng.core'; describe('MyProjectNameService', () => { let service: MyProjectNameService; - + const mockRestService = jasmine.createSpyObj('RestService', ['request']); beforeEach(() => { - TestBed.configureTestingModule({}); + TestBed.configureTestingModule({ + providers: [ + { + provide: RestService, + useValue: mockRestService, + }, + ], + }); service = TestBed.inject(MyProjectNameService); }); From 0af911c9959b2e25a822482b37be2ad56754f6d1 Mon Sep 17 00:00:00 2001 From: Mahmut Gundogdu Date: Tue, 11 Oct 2022 15:48:16 +0300 Subject: [PATCH 4/4] Fix exists test issues on app-nolayers Template --- .../src/app/home/home.component.spec.ts | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 templates/app-nolayers/angular/src/app/home/home.component.spec.ts diff --git a/templates/app-nolayers/angular/src/app/home/home.component.spec.ts b/templates/app-nolayers/angular/src/app/home/home.component.spec.ts new file mode 100644 index 00000000000..9622abc9f6d --- /dev/null +++ b/templates/app-nolayers/angular/src/app/home/home.component.spec.ts @@ -0,0 +1,62 @@ +import { CoreTestingModule } from '@abp/ng.core/testing'; +import { ThemeBasicTestingModule } from '@abp/ng.theme.basic/testing'; +import { ThemeSharedTestingModule } from '@abp/ng.theme.shared/testing'; +import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing'; +import { NgxValidateCoreModule } from '@ngx-validate/core'; +import { HomeComponent } from './home.component'; +import { OAuthService } from 'angular-oauth2-oidc'; +import { AuthService } from '@abp/ng.core'; + +describe('HomeComponent', () => { + let fixture: ComponentFixture; + const mockOAuthService = jasmine.createSpyObj('OAuthService', ['hasValidAccessToken']); + const mockAuthService = jasmine.createSpyObj('AuthService', ['navigateToLogin']); + beforeEach(waitForAsync(() => { + TestBed.configureTestingModule({ + declarations: [HomeComponent], + imports: [ + CoreTestingModule.withConfig(), + ThemeSharedTestingModule.withConfig(), + ThemeBasicTestingModule.withConfig(), + NgxValidateCoreModule, + ], + providers: [ + /* mock providers here */ + { + provide: OAuthService, + useValue: mockOAuthService, + }, + { + provide: AuthService, + useValue: mockAuthService, + }, + ], + }).compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(HomeComponent); + fixture.detectChanges(); + }); + + it('should be initiated', () => { + expect(fixture.componentInstance).toBeTruthy(); + }); + + describe('when login state is true', () => { + beforeAll(() => { + mockOAuthService.hasValidAccessToken.and.returnValue(true); + }); + + it('hasLoggedIn should be true', () => { + expect(fixture.componentInstance.hasLoggedIn).toBeTrue(); + expect(mockOAuthService.hasValidAccessToken).toHaveBeenCalled(); + }); + + it('button should not be exists', () => { + const element = fixture.nativeElement; + const cardTitle = element.querySelector('.card-title'); + expect(cardTitle).toBeTruthy(); + }); + }); +});