1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { By } from '@angular/platform-browser';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { ToastrModule } from 'ngx-toastr';
8 import { CephModule } from '~/app/ceph/ceph.module';
9 import { CoreModule } from '~/app/core/core.module';
10 import { HostService } from '~/app/shared/api/host.service';
11 import { ConfirmationModalComponent } from '~/app/shared/components/confirmation-modal/confirmation-modal.component';
12 import { AppConstants } from '~/app/shared/constants/app.constants';
13 import { ModalService } from '~/app/shared/services/modal.service';
14 import { WizardStepsService } from '~/app/shared/services/wizard-steps.service';
15 import { SharedModule } from '~/app/shared/shared.module';
16 import { configureTestBed } from '~/testing/unit-test-helper';
17 import { CreateClusterComponent } from './create-cluster.component';
19 describe('CreateClusterComponent', () => {
20 let component: CreateClusterComponent;
21 let fixture: ComponentFixture<CreateClusterComponent>;
22 let wizardStepService: WizardStepsService;
23 let hostService: HostService;
24 let modalServiceShowSpy: jasmine.Spy;
25 const projectConstants: typeof AppConstants = AppConstants;
29 HttpClientTestingModule,
31 ToastrModule.forRoot(),
39 fixture = TestBed.createComponent(CreateClusterComponent);
40 component = fixture.componentInstance;
41 wizardStepService = TestBed.inject(WizardStepsService);
42 hostService = TestBed.inject(HostService);
43 modalServiceShowSpy = spyOn(TestBed.inject(ModalService), 'show').and.returnValue({
44 // mock the close function, it might be called if there are async tests.
47 fixture.detectChanges();
50 it('should create', () => {
51 expect(component).toBeTruthy();
54 it('should have project name as heading in welcome screen', () => {
55 const heading = fixture.debugElement.query(By.css('h3')).nativeElement;
56 expect(heading.innerHTML).toBe(`Welcome to ${projectConstants.projectName}`);
59 it('should show confirmation modal when cluster creation is skipped', () => {
60 component.skipClusterCreation();
61 expect(modalServiceShowSpy.calls.any()).toBeTruthy();
62 expect(modalServiceShowSpy.calls.first().args[0]).toBe(ConfirmationModalComponent);
65 it('should show the wizard when cluster creation is started', () => {
66 component.createCluster();
67 fixture.detectChanges();
68 const nativeEl = fixture.debugElement.nativeElement;
69 expect(nativeEl.querySelector('cd-wizard')).not.toBe(null);
72 it('should have title Add Hosts', () => {
73 component.createCluster();
74 fixture.detectChanges();
75 const heading = fixture.debugElement.query(By.css('.title')).nativeElement;
76 expect(heading.innerHTML).toBe('Add Hosts');
79 it('should show the host list when cluster creation as first step', () => {
80 component.createCluster();
81 fixture.detectChanges();
82 const nativeEl = fixture.debugElement.nativeElement;
83 expect(nativeEl.querySelector('cd-hosts')).not.toBe(null);
86 it('should move to next step and show the second page', () => {
87 const wizardStepServiceSpy = spyOn(wizardStepService, 'moveToNextStep').and.callThrough();
88 const hostServiceSpy = spyOn(hostService, 'list').and.callThrough();
89 component.createCluster();
90 fixture.detectChanges();
91 component.onNextStep();
92 fixture.detectChanges();
93 const heading = fixture.debugElement.query(By.css('.title')).nativeElement;
94 expect(wizardStepServiceSpy).toHaveBeenCalledTimes(1);
95 expect(hostServiceSpy).toBeCalledTimes(1);
96 expect(heading.innerHTML).toBe('Review');
99 it('should show the button labels correctly', () => {
100 component.createCluster();
101 fixture.detectChanges();
102 let submitBtnLabel = component.showSubmitButtonLabel();
103 expect(submitBtnLabel).toEqual('Next');
104 let cancelBtnLabel = component.showCancelButtonLabel();
105 expect(cancelBtnLabel).toEqual('Cancel');
107 // Last page of the wizard
108 component.onNextStep();
109 fixture.detectChanges();
110 submitBtnLabel = component.showSubmitButtonLabel();
111 expect(submitBtnLabel).toEqual('Expand Cluster');
112 cancelBtnLabel = component.showCancelButtonLabel();
113 expect(cancelBtnLabel).toEqual('Back');