Skip to content

Commit 47289b5

Browse files
committed
Enable unit tests for components
1 parent ac79946 commit 47289b5

File tree

7 files changed

+59
-10
lines changed

7 files changed

+59
-10
lines changed

src/app/users/dialog/dialog.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ <h3 mat-dialog-title>Add a user</h3>
99
</mat-form-field>
1010

1111
<mat-form-field>
12-
<input matInput ngModel name="bs" placeholder="Corporate Jargon BS" required>
12+
<input matInput ngModel name="bs" placeholder="Corporate jargon BS" required>
1313
</mat-form-field>
1414
</div>
1515
</form>

src/app/users/dialog/dialog.component.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
22
import { NO_ERRORS_SCHEMA } from '@angular/core';
33

44
import { DialogComponent } from './dialog.component';
5+
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material';
6+
import { FormsModule } from '@angular/forms';
57

68
describe('DialogComponent', () => {
79
let component: DialogComponent;
810
let fixture: ComponentFixture<DialogComponent>;
911

1012
beforeEach(async(() => {
1113
TestBed.configureTestingModule({
14+
imports: [
15+
FormsModule
16+
],
1217
declarations: [ DialogComponent ],
13-
schemas: [ NO_ERRORS_SCHEMA ]
18+
schemas: [ NO_ERRORS_SCHEMA ],
19+
providers: [
20+
{ provide: MatDialogRef, useValue: {} },
21+
{ provide: MAT_DIALOG_DATA, useValue: {}}
22+
]
1423
})
1524
.compileComponents();
1625
}));
@@ -21,7 +30,7 @@ describe('DialogComponent', () => {
2130
fixture.detectChanges();
2231
});
2332

24-
xit('should be created', () => {
33+
it('should be created', () => {
2534
expect(component).toBeTruthy();
2635
});
2736
});

src/app/users/users.component.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
<div fxLayout="column" fxFlex class="users">
2-
<mat-toolbar color="primary">
3-
Angular HttpClient
4-
</mat-toolbar>
5-
62

73
<mat-nav-list>
84
<mat-list-item *ngFor="let user of users" (click)="selectedUser = user">

src/app/users/users.component.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,24 @@ import { async, ComponentFixture, TestBed } from '@angular/core/testing';
22
import { NO_ERRORS_SCHEMA } from '@angular/core';
33

44
import { UsersComponent } from './users.component';
5+
import { UserService } from '../user.service';
6+
import { FakeUserService } from '@testing/fake-user.service';
7+
import { MatDialogModule } from '@angular/material';
58

6-
xdescribe('UsersComponent', () => {
9+
describe('UsersComponent', () => {
710
let component: UsersComponent;
811
let fixture: ComponentFixture<UsersComponent>;
912

1013
beforeEach(async(() => {
1114
TestBed.configureTestingModule({
15+
imports: [
16+
MatDialogModule,
17+
],
1218
declarations: [ UsersComponent ],
13-
schemas: [ NO_ERRORS_SCHEMA ]
19+
schemas: [ NO_ERRORS_SCHEMA ],
20+
providers: [
21+
{ provide: UserService, useClass: FakeUserService }
22+
]
1423
})
1524
.compileComponents();
1625
}));

src/testing/fake-user.service.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Observable, of } from 'rxjs';
2+
import { User } from '../app/user';
3+
4+
export const FAKE_USERS: User[] = [
5+
{
6+
name: 'Test 1',
7+
bs: 'BS 1',
8+
id: 1,
9+
avatar: 'avatar-1'
10+
},
11+
{
12+
name: 'Test 2',
13+
bs: 'BS 2',
14+
id: 2
15+
},
16+
];
17+
18+
export class FakeUserService {
19+
public apiEndpoint = 'api/';
20+
21+
public getUsers(): Observable<User[]> {
22+
return of(FAKE_USERS);
23+
}
24+
25+
public addUser(user: User): Observable<User> {
26+
return of({
27+
name: user.name,
28+
bs: user.bs
29+
});
30+
}
31+
}

src/tsconfig.app.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
},
88
"exclude": [
99
"test.ts",
10+
"**/testing/*",
1011
"**/*.spec.ts"
1112
]
1213
}

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
"lib": [
1616
"es2017",
1717
"dom"
18-
]
18+
],
19+
"paths": {
20+
"@testing/*": ["src/testing/*"]
21+
}
1922
}
2023
}

0 commit comments

Comments
 (0)