From 9acf888135aeb79b7278120c2f945f44f5c7a576 Mon Sep 17 00:00:00 2001 From: mehmet-erim Date: Mon, 4 Nov 2019 16:41:21 +0300 Subject: [PATCH] test(template): add home.component.spec --- .../angular/src/app/home/home.component.html | 3 +- .../src/app/home/home.component.spec.ts | 51 +++++++++++++++++++ 2 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 templates/app/angular/src/app/home/home.component.spec.ts diff --git a/templates/app/angular/src/app/home/home.component.html b/templates/app/angular/src/app/home/home.component.html index 2733150b71..f4d41daf4f 100644 --- a/templates/app/angular/src/app/home/home.component.html +++ b/templates/app/angular/src/app/home/home.component.html @@ -14,6 +14,7 @@ [state]="{ redirectUrl: '/' }" class="px-4 btn btn-primary ml-1" role="button" - > {{ 'AbpIdentity::Login' | abpLocalization }} {{ 'AbpAccount::Login' | abpLocalization }} 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 0000000000..c0c7686591 --- /dev/null +++ b/templates/app/angular/src/app/home/home.component.spec.ts @@ -0,0 +1,51 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; +import { OAuthService } from 'angular-oauth2-oidc'; +import { HomeComponent } from './home.component'; +import { HomeModule } from './home.module'; +import { RouterTestingModule } from '@angular/router/testing'; +import { NgxsModule } from '@ngxs/store'; +import { By } from '@angular/platform-browser'; + +describe('HomeComponent', () => { + let component: HomeComponent; + let fixture: ComponentFixture; + let mockOAuthService: { hasValidAccessToken: () => boolean }; + + beforeEach(() => { + TestBed.configureTestingModule({ + imports: [HomeModule, NgxsModule.forRoot(), RouterTestingModule], + providers: [{ provide: OAuthService, useValue: { hasValidAccessToken: () => false } }], + }); + fixture = TestBed.createComponent(HomeComponent); + component = fixture.componentInstance; + mockOAuthService = TestBed.get(OAuthService); + fixture.detectChanges(); + }); + + describe('#hasLoggedIn', () => { + it('should return the hasValidAccessToken method of oAuthService', () => { + const spy = spyOn(mockOAuthService, 'hasValidAccessToken'); + spy.and.returnValue(false); + + expect(component.hasLoggedIn).toBe(false); + expect(spy).toHaveBeenCalled(); + }); + }); + + describe('login button', () => { + it('should display', () => { + const button = fixture.debugElement.query(By.css('[routerLink="/account/login"]')); + expect(button).toBeTruthy(); + expect(button.nativeElement.textContent).toContain('AbpAccount::Login'); + }); + + it('should not display when user logged in', () => { + const spy = spyOn(mockOAuthService, 'hasValidAccessToken'); + spy.and.returnValue(true); + fixture.detectChanges(); + + const button = fixture.debugElement.query(By.css('#login-button')); + expect(button).toBeFalsy(); + }); + }); +});