|
| 1 | +import {async, ComponentFixture, fakeAsync, flush, flushMicrotasks, TestBed, waitForAsync} from '@angular/core/testing'; |
| 2 | +import {CoursesModule} from '../courses.module'; |
| 3 | +import {DebugElement} from '@angular/core'; |
| 4 | + |
| 5 | +import {HomeComponent} from './home.component'; |
| 6 | +import {HttpClientTestingModule, HttpTestingController} from '@angular/common/http/testing'; |
| 7 | +import {CoursesService} from '../services/courses.service'; |
| 8 | +import {HttpClient} from '@angular/common/http'; |
| 9 | +import {COURSES} from '../../../../server/db-data'; |
| 10 | +import {setupCourses} from '../common/setup-test-data'; |
| 11 | +import {By} from '@angular/platform-browser'; |
| 12 | +import {of} from 'rxjs'; |
| 13 | +import {NoopAnimationsModule} from '@angular/platform-browser/animations'; |
| 14 | +import {click} from '../common/test-utils'; |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +describe('HomeComponent', () => { |
| 20 | + |
| 21 | + let fixture: ComponentFixture<HomeComponent>; |
| 22 | + let component: HomeComponent; |
| 23 | + let el: DebugElement; |
| 24 | + let coursesService: any; |
| 25 | + |
| 26 | + const beginnerCourses = setupCourses().filter(course => course.category === 'BEGINNER'); |
| 27 | + const advancedCourses = setupCourses().filter(course => course.category === 'ADVANCED'); |
| 28 | + |
| 29 | + beforeEach(waitForAsync( ((() => { |
| 30 | + const coursesServiceSpy = jasmine.createSpyObj('CoursesService', ['findAllCourses']); |
| 31 | + TestBed.configureTestingModule({ |
| 32 | + imports: [ |
| 33 | + CoursesModule, |
| 34 | + NoopAnimationsModule, |
| 35 | + ], |
| 36 | + providers: [ |
| 37 | + {provide: CoursesService, useValue: coursesServiceSpy} |
| 38 | + ] |
| 39 | + }).compileComponents().then(() => { |
| 40 | + fixture = TestBed.createComponent(HomeComponent); |
| 41 | + component = fixture.componentInstance; |
| 42 | + el = fixture.debugElement; |
| 43 | + coursesService = TestBed.get(CoursesService); |
| 44 | + }); |
| 45 | + })))); |
| 46 | + |
| 47 | + it('should create the component', () => { |
| 48 | + expect(component).toBeTruthy(); |
| 49 | + |
| 50 | + }); |
| 51 | + |
| 52 | + |
| 53 | + it('should display only beginner courses', () => { |
| 54 | + |
| 55 | + coursesService.findAllCourses.and.returnValue(of(beginnerCourses)); |
| 56 | + fixture.detectChanges(); |
| 57 | + |
| 58 | + const tabs = el.queryAll(By.css('.mat-tab-label')); |
| 59 | + |
| 60 | + expect(tabs.length).toBe(1, 'Unexpected number of tabs found'); |
| 61 | + |
| 62 | + }); |
| 63 | + |
| 64 | + |
| 65 | + it('should display only advanced courses', () => { |
| 66 | + |
| 67 | + coursesService.findAllCourses.and.returnValue(of(advancedCourses)); |
| 68 | + fixture.detectChanges(); |
| 69 | + |
| 70 | + const tabs = el.queryAll(By.css('.mat-tab-label')); |
| 71 | + |
| 72 | + expect(tabs.length).toBe(1, 'Unexpected number of tabs found'); |
| 73 | + }); |
| 74 | + |
| 75 | + |
| 76 | + it('should display both tabs', () => { |
| 77 | + |
| 78 | + coursesService.findAllCourses.and.returnValue(of(setupCourses())); |
| 79 | + fixture.detectChanges(); |
| 80 | + |
| 81 | + const tabs = el.queryAll(By.css('.mat-tab-label')); |
| 82 | + |
| 83 | + expect(tabs.length).toBe(2, 'Unexpected number of tabs found'); |
| 84 | + |
| 85 | + }); |
| 86 | + |
| 87 | + |
| 88 | + it('should display advanced courses when tab clicked', (done: DoneFn) => { |
| 89 | + |
| 90 | + coursesService.findAllCourses.and.returnValue(of(setupCourses())); |
| 91 | + fixture.detectChanges(); |
| 92 | + const tabs = el.queryAll(By.css('.mat-tab-label')); |
| 93 | + click(tabs[1]); |
| 94 | + fixture.detectChanges(); |
| 95 | + setTimeout(() => { |
| 96 | + |
| 97 | + const cardTitles = el.queryAll(By.css('.mat-tab-body-active .mat-card-title')); |
| 98 | + expect(cardTitles.length).toBeGreaterThan(0, 'could not find card titles'); |
| 99 | + expect(cardTitles[0].nativeElement.textContent).toContain('Angular Security Course'); |
| 100 | + done(); |
| 101 | + }, 500); |
| 102 | + }); |
| 103 | + |
| 104 | +}); |
| 105 | + |
| 106 | + |
0 commit comments