-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example tests to Home.component.spec.ts
- Loading branch information
1 parent
5dfd379
commit dd53c7b
Showing
1 changed file
with
100 additions
and
0 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
templates/app/angular/src/app/home/home.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<HomeComponent>; | ||
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() | ||
}) | ||
}) | ||
}) | ||
|
||
}); |