Skip to content
This repository was archived by the owner on Jan 23, 2022. It is now read-only.

Commit 3b13809

Browse files
committed
Basic Unit test implementations.
1 parent 356a0a5 commit 3b13809

File tree

5 files changed

+132
-91
lines changed

5 files changed

+132
-91
lines changed
Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,34 @@
11
/* tslint:disable:no-unused-variable */
2-
3-
import { TestBed, async } from '@angular/core/testing';
4-
import { AppComponent } from './app.component';
5-
import { AuthService } from './shared/auth.service';
6-
import { FormsModule } from '@angular/forms';
7-
import {AsyncSubject} from "rxjs";
2+
import { CUSTOM_ELEMENTS_SCHEMA } from '@angular/core';
3+
import {TestBed, async} from "@angular/core/testing";
4+
import {AppComponent} from "./app.component";
85

96
describe('AppComponent', () => {
10-
beforeEach(() => {
11-
let authServiceStub = {
12-
login: function () { return true },
13-
logout: function () { return true },
14-
isLoggedIn: function () {
15-
return new AsyncSubject<boolean>();
16-
}
17-
};
18-
19-
TestBed.configureTestingModule({
20-
imports: [ FormsModule ],
21-
declarations: [
22-
AppComponent
23-
],
24-
providers: [
25-
{ provide: AuthService, useValue: authServiceStub }
26-
]
7+
beforeEach(() => {
8+
TestBed.configureTestingModule({
9+
declarations: [
10+
AppComponent
11+
],
12+
schemas: [CUSTOM_ELEMENTS_SCHEMA]
13+
});
2714
});
28-
});
2915

30-
it('should create the app', async(() => {
31-
let fixture = TestBed.createComponent(AppComponent);
32-
let app = fixture.debugElement.componentInstance;
33-
expect(app).toBeTruthy();
34-
}));
16+
it('should create the app', async(() => {
17+
let fixture = TestBed.createComponent(AppComponent);
18+
let app = fixture.debugElement.componentInstance;
19+
expect(app).toBeTruthy();
20+
}));
3521

36-
it(`should have as title 'app works!'`, async(() => {
37-
let fixture = TestBed.createComponent(AppComponent);
38-
let app = fixture.debugElement.componentInstance;
39-
expect(app.title).toEqual('app works! - <%= name %>');
40-
}));
22+
it(`should have as title 'app works!'`, async(() => {
23+
let fixture = TestBed.createComponent(AppComponent);
24+
let app = fixture.debugElement.componentInstance;
25+
expect(app.title).toEqual('app works! - <%= name %>');
26+
}));
4127

42-
it('should render title in a h1 tag', async(() => {
43-
let fixture = TestBed.createComponent(AppComponent);
44-
fixture.detectChanges();
45-
let compiled = fixture.debugElement.nativeElement;
46-
expect(compiled.querySelector('h1').textContent).toContain('app works! - <%= name %>');
47-
}));
28+
it('should render title in a h1 tag', async(() => {
29+
let fixture = TestBed.createComponent(AppComponent);
30+
fixture.detectChanges();
31+
let compiled = fixture.debugElement.nativeElement;
32+
expect(compiled.querySelector('h1').textContent).toContain('app works! - <%= name %>');
33+
}));
4834
});
Lines changed: 68 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,73 @@
11
/* tslint:disable:no-unused-variable */
2-
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3-
import { By } from '@angular/platform-browser';
4-
import { DebugElement } from '@angular/core';
2+
import {async, ComponentFixture, TestBed} from "@angular/core/testing";
3+
import {DisplayUserComponent} from "./display-user.component";
4+
import {AsyncSubject, Observable, ReplaySubject} from "rxjs";
5+
import {FormsModule} from "@angular/forms";
6+
import {AuthService} from "app/shared/auth.service";
7+
import {UserInfo} from "app/shared/user-info";
8+
import {By} from "@angular/platform-browser";
59

6-
import { DisplayUserComponent } from './display-user.component';
10+
class AuthServiceStub {
11+
12+
constructor(private loggedin: boolean) {
13+
}
14+
15+
login(email: string, password: string) {
16+
}
17+
18+
currentUser(): Observable<UserInfo> {
19+
let userInfo = new UserInfo();
20+
userInfo.displayName = "my-display-name";
21+
userInfo.email = "my-email";
22+
userInfo.uid = "my-uid";
23+
userInfo.isAnonymous = false;
24+
userInfo.photoURL = "my-photo-url";
25+
userInfo.providerId = "my-provider-id";
26+
27+
let replaySubject = new ReplaySubject();
28+
if (this.loggedin) {
29+
replaySubject.next(userInfo);
30+
}
31+
return replaySubject;
32+
}
33+
34+
isLoggedIn(): Observable<boolean> {
35+
let isLoggedInBS = new AsyncSubject<boolean>();
36+
isLoggedInBS.next(true);
37+
isLoggedInBS.complete();
38+
return isLoggedInBS;
39+
}
40+
}
741

842
describe('DisplayUserComponent', () => {
9-
let component: DisplayUserComponent;
10-
let fixture: ComponentFixture<DisplayUserComponent>;
11-
12-
beforeEach(async(() => {
13-
TestBed.configureTestingModule({
14-
declarations: [ DisplayUserComponent ]
15-
})
16-
.compileComponents();
17-
}));
18-
19-
beforeEach(() => {
20-
fixture = TestBed.createComponent(DisplayUserComponent);
21-
component = fixture.componentInstance;
22-
fixture.detectChanges();
23-
});
24-
25-
it('should create', () => {
26-
expect(component).toBeTruthy();
27-
});
43+
44+
let component: DisplayUserComponent;
45+
let fixture: ComponentFixture<DisplayUserComponent>;
46+
47+
beforeEach(async(() => {
48+
let authServiceStub = new AuthServiceStub(true);
49+
50+
TestBed.configureTestingModule({
51+
imports: [FormsModule],
52+
declarations: [DisplayUserComponent],
53+
providers: [
54+
{provide: AuthService, useValue: authServiceStub}
55+
]
56+
}).compileComponents();
57+
}));
58+
59+
beforeEach(() => {
60+
fixture = TestBed.createComponent(DisplayUserComponent);
61+
component = fixture.componentInstance;
62+
fixture.detectChanges();
63+
});
64+
65+
it('should display userinfo when logged in', () => {
66+
expect(component).toBeTruthy();
67+
let elements = fixture.debugElement.queryAll(By.css(".list-group-item"));
68+
expect(elements[0].nativeElement.textContent).toContain("Email: my-email");
69+
expect(elements[1].nativeElement.textContent).toContain("Display Name: my-display-name");
70+
expect(elements[2].nativeElement.textContent).toContain("Uid: my-uid");
71+
expect(elements[3].nativeElement.textContent).toContain("Provider Id: my-provider-id");
72+
});
2873
});

generators/app/templates/src/app/login-user/login-user.component.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,5 @@ <h3>Please Log In
3030
Log In
3131
</button>
3232
</form>
33-
3433
</div>
3534
</div>
Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,45 @@
11
/* tslint:disable:no-unused-variable */
2-
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
3-
import { By } from '@angular/platform-browser';
4-
import { DebugElement } from '@angular/core';
5-
6-
import { LoginUserComponent } from './login-user.component';
2+
import {async, ComponentFixture, TestBed} from "@angular/core/testing";
3+
import {LoginUserComponent} from "./login-user.component";
4+
import {AuthService} from "app/shared/auth.service";
5+
import {FormsModule} from "@angular/forms";
6+
import {AsyncSubject, Observable, ReplaySubject} from "rxjs";
7+
import {UserInfo} from "app/shared/user-info";
78

89
describe('LoginUserComponent', () => {
9-
let component: LoginUserComponent;
10-
let fixture: ComponentFixture<LoginUserComponent>;
10+
let component: LoginUserComponent;
11+
let fixture: ComponentFixture<LoginUserComponent>;
12+
13+
beforeEach(async(() => {
14+
let authServiceStub = {
15+
login: function () {
16+
return true
17+
},
18+
logout: function () {
19+
return true
20+
},
21+
isLoggedIn: function () {
22+
return new AsyncSubject<boolean>();
23+
}
24+
};
1125

12-
beforeEach(async(() => {
13-
TestBed.configureTestingModule({
14-
declarations: [ LoginUserComponent ]
15-
})
16-
.compileComponents();
17-
}));
26+
TestBed.configureTestingModule({
27+
imports: [FormsModule],
28+
declarations: [LoginUserComponent],
29+
providers: [
30+
{provide: AuthService, useValue: authServiceStub}
31+
]
32+
})
33+
.compileComponents();
34+
}));
1835

19-
beforeEach(() => {
20-
fixture = TestBed.createComponent(LoginUserComponent);
21-
component = fixture.componentInstance;
22-
fixture.detectChanges();
23-
});
36+
beforeEach(() => {
37+
fixture = TestBed.createComponent(LoginUserComponent);
38+
component = fixture.componentInstance;
39+
fixture.detectChanges();
40+
});
2441

25-
it('should create', () => {
26-
expect(component).toBeTruthy();
27-
});
42+
it('should create', () => {
43+
expect(component).toBeTruthy();
44+
});
2845
});

generators/app/templates/src/app/shared/auth.service.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,11 @@ export class AuthService {
1818
let userInfo = new UserInfo();
1919
if (auth != null) {
2020
this.auth = auth.auth;
21-
//noinspection TypeScriptUnresolvedVariable
2221
userInfo.isAnonymous = auth.auth.isAnonymous;
23-
//noinspection TypeScriptUnresolvedVariable
2422
userInfo.email = auth.auth.email;
25-
//noinspection TypeScriptUnresolvedVariable
2623
userInfo.displayName = auth.auth.displayName;
27-
//noinspection TypeScriptUnresolvedVariable
2824
userInfo.providerId = auth.auth.providerId;
29-
//noinspection TypeScriptUnresolvedVariable
3025
userInfo.photoURL = auth.auth.photoURL;
31-
//noinspection TypeScriptUnresolvedVariable
3226
userInfo.uid = auth.auth.uid;
3327
} else {
3428
this.auth = null;

0 commit comments

Comments
 (0)