Skip to content

Commit fdf3ab8

Browse files
committed
Testing html elements
Added unit test for checking if a html element exists. This is done by query the DOM and by checking for the element based on the css class
1 parent fb931b6 commit fdf3ab8

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/app/courses/courses-card-list/courses-card-list.component.spec.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ describe("CoursesCardListComponent", () => {
4444
});
4545
// Asserting the content of the list is correct by checking the first course
4646
it("should display the first course", () => {
47-
pending();
47+
// This helper method returns a mock data of courses that are ordered by sequence number
48+
component.courses = setupCourses();
49+
fixture.detectChanges();
50+
// This is the first course in the mock data
51+
const course = component.courses[0];
52+
// These are the html element we want to check for
53+
const card = element.query(By.css(".course-card:first-child"));
54+
const title = card.query(By.css("mat-card-title"));
55+
const image = card.query(By.css("img"));
56+
57+
// Expectations
58+
59+
expect(card).toBeTruthy("Could not find course card");
60+
// This will return the text content of the mat-card-title element
61+
// We want to check if the title matches the same one from the mock dataset
62+
expect(title.nativeElement.textContent).toBe(course.titles.description);
63+
// Check the source element of the image
64+
expect(image.nativeElement.src).toBe(course.iconUrl);
4865
});
4966
});

0 commit comments

Comments
 (0)