Skip to content

Commit fb931b6

Browse files
committed
Component creation and DOM check
1 parent 1b0041a commit fb931b6

File tree

3 files changed

+70
-31
lines changed

3 files changed

+70
-31
lines changed

src/app/courses/courses-card-list/courses-card-list.component.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111

1212
.course-actions button {
1313
margin-right: 5px;
14-
}
14+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { DebugElement } from "@angular/core";
2+
import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing";
3+
import { By } from "@angular/platform-browser";
4+
import { setupCourses } from "../common/setup-test-data";
5+
import { CoursesModule } from "../courses.module";
6+
import { CoursesCardListComponent } from "./courses-card-list.component";
7+
8+
describe("CoursesCardListComponent", () => {
9+
let component: CoursesCardListComponent;
10+
let fixture: ComponentFixture<CoursesCardListComponent>;
11+
let element: DebugElement;
12+
13+
beforeEach(waitForAsync(() => {
14+
TestBed.configureTestingModule({
15+
// We need to declare the component we are testing and any other components that component uses
16+
// Instead of writing out all the declarations we can just import the module containing the imports
17+
imports: [CoursesModule],
18+
})
19+
.compileComponents()
20+
.then(() => {
21+
fixture = TestBed.createComponent(CoursesCardListComponent);
22+
component = fixture.componentInstance;
23+
element = fixture.debugElement;
24+
});
25+
}));
26+
27+
// Asserting the component is created correctly
28+
it("should create the component", () => {
29+
expect(component).toBeTruthy();
30+
});
31+
// Assert the component is display a list of courses
32+
it("should display the course list", () => {
33+
// Pass some data to the component
34+
component.courses = setupCourses();
35+
36+
// Trigger the component change detection
37+
// This will notify the component some changes were made so the DOM is updated
38+
fixture.detectChanges();
39+
40+
// Query the dom for a css class name for the mat card element
41+
const cards = element.queryAll(By.css(".course-card"));
42+
expect(cards).toBeTruthy("Could not find cards");
43+
expect(cards.length).toBe(12, "Unexpected number of courses");
44+
});
45+
// Asserting the content of the list is correct by checking the first course
46+
it("should display the first course", () => {
47+
pending();
48+
});
49+
});
Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
1-
import {Component, EventEmitter, Input, OnInit, Output, ViewEncapsulation} from '@angular/core';
2-
import {Course} from '../model/course';
3-
import { MatDialog, MatDialogConfig } from '@angular/material/dialog';
4-
import {CourseDialogComponent} from '../course-dialog/course-dialog.component';
5-
import {filter, tap} from 'rxjs/operators';
1+
import {
2+
Component,
3+
EventEmitter,
4+
Input,
5+
OnInit,
6+
Output,
7+
ViewEncapsulation,
8+
} from "@angular/core";
9+
import { Course } from "../model/course";
10+
import { MatDialog, MatDialogConfig } from "@angular/material/dialog";
11+
import { CourseDialogComponent } from "../course-dialog/course-dialog.component";
12+
import { filter, tap } from "rxjs/operators";
613

714
@Component({
8-
selector: 'courses-card-list',
9-
templateUrl: './courses-card-list.component.html',
10-
styleUrls: ['./courses-card-list.component.css']
15+
selector: "courses-card-list",
16+
templateUrl: "./courses-card-list.component.html",
17+
styleUrls: ["./courses-card-list.component.css"],
1118
})
1219
export class CoursesCardListComponent implements OnInit {
13-
1420
@Input()
1521
courses: Course[];
1622

1723
@Output()
1824
courseEdited = new EventEmitter();
1925

20-
constructor(private dialog: MatDialog) {
21-
22-
}
26+
constructor(private dialog: MatDialog) {}
2327

24-
ngOnInit() {
25-
26-
}
28+
ngOnInit() {}
2729

2830
editCourse(course: Course) {
29-
3031
const dialogConfig = new MatDialogConfig();
3132

3233
dialogConfig.disableClose = true;
@@ -36,23 +37,12 @@ export class CoursesCardListComponent implements OnInit {
3637

3738
const dialogRef = this.dialog.open(CourseDialogComponent, dialogConfig);
3839

39-
40-
dialogRef.afterClosed()
40+
dialogRef
41+
.afterClosed()
4142
.pipe(
42-
filter(val => !!val),
43+
filter((val) => !!val),
4344
tap(() => this.courseEdited.emit())
4445
)
4546
.subscribe();
46-
4747
}
48-
4948
}
50-
51-
52-
53-
54-
55-
56-
57-
58-

0 commit comments

Comments
 (0)