|
| 1 | +import { DebugElement } from "@angular/core"; |
| 2 | +import { ComponentFixture, TestBed, waitForAsync } from "@angular/core/testing"; |
| 3 | +import { By } from "@angular/platform-browser"; |
| 4 | +import { NoopAnimationsModule } from "@angular/platform-browser/animations"; |
| 5 | +import { of } from "rxjs"; |
| 6 | +import { setupCourses } from "../common/setup-test-data"; |
| 7 | +import { CoursesModule } from "../courses.module"; |
| 8 | +import { CoursesService } from "../services/courses.service"; |
| 9 | +import { HomeComponent } from "./home.component"; |
| 10 | + |
| 11 | +describe("HomeComponent", () => { |
| 12 | + let fixture: ComponentFixture<HomeComponent>; |
| 13 | + let component: HomeComponent; |
| 14 | + let element: DebugElement; |
| 15 | + let coursesService: any; |
| 16 | + |
| 17 | + // This is the list of beginner courses |
| 18 | + const beginnerCourses = setupCourses().filter( |
| 19 | + (course) => course.category == "BEGINNER" |
| 20 | + ); |
| 21 | + |
| 22 | + const advancedCourses = setupCourses().filter( |
| 23 | + (course) => course.category == "ADVANCED" |
| 24 | + ); |
| 25 | + |
| 26 | + beforeEach(waitForAsync(() => { |
| 27 | + // Creating a jasmine spy to mock the coursesService dependency |
| 28 | + // We need to include the dependency also, in this case its the CoursesService |
| 29 | + // We also need to define a list of methods we will be calling in our test |
| 30 | + // findAllCourses is the only method from the service that is being used |
| 31 | + const coursesServiceSpy = jasmine.createSpyObj("CoursesService", [ |
| 32 | + "findAllCourses", |
| 33 | + ]); |
| 34 | + |
| 35 | + TestBed.configureTestingModule({ |
| 36 | + imports: [CoursesModule, NoopAnimationsModule], |
| 37 | + // Here we want to mock the CoursesService |
| 38 | + providers: [{ provide: CoursesService, useValue: coursesServiceSpy }], |
| 39 | + }) |
| 40 | + .compileComponents() |
| 41 | + .then(() => { |
| 42 | + fixture = TestBed.createComponent(HomeComponent); |
| 43 | + component = fixture.componentInstance; |
| 44 | + element = fixture.debugElement; |
| 45 | + coursesService = TestBed.inject<CoursesService>(CoursesService); |
| 46 | + }); |
| 47 | + })); |
| 48 | + |
| 49 | + it("should create the component", () => { |
| 50 | + expect(component).toBeTruthy(); |
| 51 | + }); |
| 52 | + |
| 53 | + // We want to check if the container is being shown only if the condition is met |
| 54 | + it("should display only beginner courses", () => { |
| 55 | + // First we want to get the data |
| 56 | + // This will emit an observable |
| 57 | + coursesService.findAllCourses.and.returnValue(of(beginnerCourses)); |
| 58 | + // This will apply the data to the DOM |
| 59 | + fixture.detectChanges(); |
| 60 | + // We should only have one tab in the DOM because only beginner classes should be displayed |
| 61 | + const tabs = element.queryAll(By.css(".mdc-tab")); |
| 62 | + // We expect only 1 tab to be in the DOM |
| 63 | + expect(tabs.length).toBe(1, "Unexpected number of tabs found"); |
| 64 | + }); |
| 65 | + it("should display only advanced courses", () => { |
| 66 | + coursesService.findAllCourses.and.returnValue(of(advancedCourses)); |
| 67 | + // This will apply the data to the DOM |
| 68 | + fixture.detectChanges(); |
| 69 | + const tabs = element.queryAll(By.css(".mdc-tab")); |
| 70 | + expect(tabs.length).toBe(1, "Unexpected number of tabs found"); |
| 71 | + }); |
| 72 | + it("should display both tabs", () => { |
| 73 | + // Note: Remember setupCourses is a helper method that returns all courses |
| 74 | + coursesService.findAllCourses.and.returnValue(of(setupCourses())); |
| 75 | + fixture.detectChanges(); |
| 76 | + const tabs = element.queryAll(By.css(".mdc-tab")); |
| 77 | + expect(tabs.length).toBe(2, "Unexpected number of tabs found"); |
| 78 | + }); |
| 79 | + it("should display advanced courses when tab clicked", () => { |
| 80 | + pending(); |
| 81 | + }); |
| 82 | +}); |
0 commit comments