Skip to content

Commit f869ac4

Browse files
committed
Added tests
1 parent 4053374 commit f869ac4

5 files changed

Lines changed: 58 additions & 7 deletions

File tree

src/app/authentication/service/authentication.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export class AuthenticationService {
1414

1515
private loginUrl = environment.apiUrl + "/login";
1616

17-
public user: User = new User();
17+
private user: User = new User();
1818

1919
constructor(
2020
private http: HttpClient

src/app/login/components/login-form/login-form.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ <h4 class="card-header">Login</h4>
1919
Password is required
2020
</div>
2121
</div>
22-
<button [disabled]="!canLogin()" class="btn btn-primary">
22+
<button [disabled]="!canLogin()" class="btn btn-primary" [attr.aria-disabled]="!canLogin()" aria-label="Login">
2323
<span *ngIf="loading" class="spinner-border spinner-border-sm mr-1"></span>
2424
Login
2525
</button>

src/app/login/components/login-form/login-form.component.spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,19 @@ describe('LoginFormComponent', () => {
3131
it('should create', () => {
3232
expect(component).toBeTruthy();
3333
});
34+
35+
it('should disable the login button by default', () => {
36+
const button = fixture.nativeElement.querySelector('form button');
37+
expect(button.disabled).toEqual(true);
38+
});
39+
40+
it('should enable the login button when the form is valid', () => {
41+
component.form.controls['username'].setValue('username');
42+
component.form.controls['password'].setValue('password');
43+
fixture.detectChanges();
44+
45+
const button = fixture.nativeElement.querySelector('form button');
46+
expect(button.disabled).toEqual(false);
47+
});
48+
3449
});

src/app/login/containers/logout-button/logout-button.component.spec.ts

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,36 @@
11
import { HttpClientTestingModule } from '@angular/common/http/testing';
22
import { ComponentFixture, TestBed } from '@angular/core/testing';
33
import { RouterTestingModule } from '@angular/router/testing';
4+
import { User } from '@app/authentication/model/user';
5+
import { AuthenticationService } from '@app/authentication/service/authentication.service';
46
import { LogoutButtonComponent } from './logout-button.component';
57

68
describe('LogoutButtonComponent', () => {
79
let component: LogoutButtonComponent;
810
let fixture: ComponentFixture<LogoutButtonComponent>;
11+
let authenticationServiceStub: Partial<AuthenticationService>;
12+
let user: User;
913

1014
beforeEach(async () => {
15+
user = new User();
16+
17+
authenticationServiceStub = {
18+
getUser(): User {
19+
return user;
20+
}
21+
};
22+
1123
await TestBed.configureTestingModule({
1224
imports: [
1325
HttpClientTestingModule,
1426
RouterTestingModule
1527
],
16-
declarations: [
28+
declarations: [
1729
LogoutButtonComponent
18-
]
30+
],
31+
providers: [{ provide: AuthenticationService, useValue: authenticationServiceStub }]
1932
})
20-
.compileComponents();
33+
.compileComponents();
2134
});
2235

2336
beforeEach(() => {
@@ -29,4 +42,18 @@ describe('LogoutButtonComponent', () => {
2942
it('should create', () => {
3043
expect(component).toBeTruthy();
3144
});
45+
46+
it('should be disabled by default', () => {
47+
const button = fixture.nativeElement.querySelector('button');
48+
expect(button.disabled).toEqual(true);
49+
});
50+
51+
it('if the user is logged the button is enabled', () => {
52+
user.logged = true;
53+
fixture.detectChanges();
54+
55+
const button = fixture.nativeElement.querySelector('button');
56+
expect(button.disabled).toEqual(false);
57+
});
58+
3259
});

src/app/login/view/login-view/login-view.component.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
import { HttpClientTestingModule } from '@angular/common/http/testing';
12
import { ComponentFixture, TestBed } from '@angular/core/testing';
3+
import { RouterTestingModule } from '@angular/router/testing';
24

35
import { LoginViewComponent } from './login-view.component';
46

@@ -8,9 +10,15 @@ describe('LoginViewComponent', () => {
810

911
beforeEach(async () => {
1012
await TestBed.configureTestingModule({
11-
declarations: [ LoginViewComponent ]
13+
imports: [
14+
HttpClientTestingModule,
15+
RouterTestingModule
16+
],
17+
declarations: [
18+
LoginViewComponent
19+
]
1220
})
13-
.compileComponents();
21+
.compileComponents();
1422
});
1523

1624
beforeEach(() => {
@@ -22,4 +30,5 @@ describe('LoginViewComponent', () => {
2230
it('should create', () => {
2331
expect(component).toBeTruthy();
2432
});
33+
2534
});

0 commit comments

Comments
 (0)