1 import { Component } from '@angular/core';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
5 import { NgbAlertModule } from '@ng-bootstrap/ng-bootstrap';
7 import { configureTestBed } from '~/testing/unit-test-helper';
8 import { AlertPanelComponent } from '../components/alert-panel/alert-panel.component';
9 import { LoadingPanelComponent } from '../components/loading-panel/loading-panel.component';
10 import { CdForm } from '../forms/cd-form';
11 import { SharedModule } from '../shared.module';
12 import { FormLoadingDirective } from './form-loading.directive';
14 @Component({ selector: 'cd-test-cmp', template: '<span *cdFormLoading="loading">foo</span>' })
15 class TestComponent extends CdForm {
21 describe('FormLoadingDirective', () => {
22 let component: TestComponent;
23 let fixture: ComponentFixture<any>;
25 const expectShown = (elem: number, error: number, loading: number) => {
26 expect(fixture.debugElement.queryAll(By.css('span')).length).toEqual(elem);
27 expect(fixture.debugElement.queryAll(By.css('cd-alert-panel')).length).toEqual(error);
28 expect(fixture.debugElement.queryAll(By.css('cd-loading-panel')).length).toEqual(loading);
33 declarations: [TestComponent],
34 imports: [SharedModule, NgbAlertModule]
36 [LoadingPanelComponent, AlertPanelComponent]
44 fixture = TestBed.createComponent(TestComponent);
45 component = fixture.componentInstance;
46 fixture.detectChanges();
49 it('should create an instance', () => {
50 const directive = new FormLoadingDirective(null, null, null);
51 expect(directive).toBeTruthy();
54 it('should show loading component by default', () => {
57 const alert = fixture.debugElement.nativeElement.querySelector('cd-loading-panel ngb-alert');
58 expect(alert.textContent).toBe('Loading form data...');
61 it('should show error component when calling loadingError()', () => {
62 component.loadingError();
63 fixture.detectChanges();
67 const alert = fixture.debugElement.nativeElement.querySelector(
68 'cd-alert-panel .alert-panel-text'
70 expect(alert.textContent).toBe('Form data could not be loaded.');
73 it('should show original component when calling loadingReady()', () => {
74 component.loadingReady();
75 fixture.detectChanges();
79 const alert = fixture.debugElement.nativeElement.querySelector('span');
80 expect(alert.textContent).toBe('foo');
83 it('should show nothing when calling loadingNone()', () => {
84 component.loadingNone();
85 fixture.detectChanges();