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 { OsdService } from '~/app/shared/api/osd.service';
12 import { ConfirmationModalComponent } from '~/app/shared/components/confirmation-modal/confirmation-modal.component';
13 import { LoadingPanelComponent } from '~/app/shared/components/loading-panel/loading-panel.component';
14 import { AppConstants } from '~/app/shared/constants/app.constants';
15 import { ModalService } from '~/app/shared/services/modal.service';
16 import { WizardStepsService } from '~/app/shared/services/wizard-steps.service';
17 import { SharedModule } from '~/app/shared/shared.module';
18 import { configureTestBed } from '~/testing/unit-test-helper';
19 import { CreateClusterComponent } from './create-cluster.component';
21 describe('CreateClusterComponent', () => {
22 let component: CreateClusterComponent;
23 let fixture: ComponentFixture<CreateClusterComponent>;
24 let wizardStepService: WizardStepsService;
25 let hostService: HostService;
26 let osdService: OsdService;
27 let modalServiceShowSpy: jasmine.Spy;
28 const projectConstants: typeof AppConstants = AppConstants;
33 HttpClientTestingModule,
35 ToastrModule.forRoot(),
41 [LoadingPanelComponent]
45 fixture = TestBed.createComponent(CreateClusterComponent);
46 component = fixture.componentInstance;
47 wizardStepService = TestBed.inject(WizardStepsService);
48 hostService = TestBed.inject(HostService);
49 osdService = TestBed.inject(OsdService);
50 modalServiceShowSpy = spyOn(TestBed.inject(ModalService), 'show').and.returnValue({
51 // mock the close function, it might be called if there are async tests.
54 fixture.detectChanges();
57 it('should create', () => {
58 expect(component).toBeTruthy();
61 it('should have project name as heading in welcome screen', () => {
62 component.startClusterCreation = true;
63 fixture.detectChanges();
64 const heading = fixture.debugElement.query(By.css('h3')).nativeElement;
65 expect(heading.innerHTML).toBe(`Welcome to ${projectConstants.projectName}`);
68 // @TODO: Opening modals in unit testing is broken since carbon.
69 // Need to fix it properly
70 it.skip('should show confirmation modal when cluster creation is skipped', () => {
71 component.skipClusterCreation();
72 expect(modalServiceShowSpy.calls.any()).toBeTruthy();
73 expect(modalServiceShowSpy.calls.first().args[0]).toBe(ConfirmationModalComponent);
76 it('should show the wizard when cluster creation is started', () => {
77 component.createCluster();
78 fixture.detectChanges();
79 const nativeEl = fixture.debugElement.nativeElement;
80 expect(nativeEl.querySelector('cd-wizard')).not.toBe(null);
83 it('should have title Add Hosts', () => {
84 component.createCluster();
85 fixture.detectChanges();
86 const heading = fixture.debugElement.query(By.css('.title')).nativeElement;
87 expect(heading.innerHTML).toBe('Add Hosts');
90 it('should show the host list when cluster creation as first step', () => {
91 component.createCluster();
92 fixture.detectChanges();
93 const nativeEl = fixture.debugElement.nativeElement;
94 expect(nativeEl.querySelector('cd-hosts')).not.toBe(null);
97 it('should move to next step and show the second page', () => {
98 const wizardStepServiceSpy = spyOn(wizardStepService, 'moveToNextStep').and.callThrough();
99 component.createCluster();
100 fixture.detectChanges();
101 component.onNextStep();
102 fixture.detectChanges();
103 expect(wizardStepServiceSpy).toHaveBeenCalledTimes(1);
106 it('should show the button labels correctly', () => {
107 component.createCluster();
108 fixture.detectChanges();
109 let submitBtnLabel = component.showSubmitButtonLabel();
110 expect(submitBtnLabel).toEqual('Next');
111 let cancelBtnLabel = component.showCancelButtonLabel();
112 expect(cancelBtnLabel).toEqual('Cancel');
114 component.onNextStep();
115 fixture.detectChanges();
116 submitBtnLabel = component.showSubmitButtonLabel();
117 expect(submitBtnLabel).toEqual('Next');
118 cancelBtnLabel = component.showCancelButtonLabel();
119 expect(cancelBtnLabel).toEqual('Back');
121 component.onNextStep();
122 fixture.detectChanges();
123 submitBtnLabel = component.showSubmitButtonLabel();
124 expect(submitBtnLabel).toEqual('Next');
125 cancelBtnLabel = component.showCancelButtonLabel();
126 expect(cancelBtnLabel).toEqual('Back');
128 // Last page of the wizard
129 component.onNextStep();
130 fixture.detectChanges();
131 submitBtnLabel = component.showSubmitButtonLabel();
132 expect(submitBtnLabel).toEqual('Expand Cluster');
133 cancelBtnLabel = component.showCancelButtonLabel();
134 expect(cancelBtnLabel).toEqual('Back');
137 it('should ensure osd creation did not happen when no devices are selected', () => {
138 component.simpleDeployment = false;
139 const osdServiceSpy = spyOn(osdService, 'create').and.callThrough();
140 component.onSubmit();
141 fixture.detectChanges();
142 expect(osdServiceSpy).toBeCalledTimes(0);
145 it('should ensure osd creation did happen when devices are selected', () => {
146 const osdServiceSpy = spyOn(osdService, 'create').and.callThrough();
147 osdService.osdDevices['totalDevices'] = 1;
148 component.onSubmit();
149 fixture.detectChanges();
150 expect(osdServiceSpy).toBeCalledTimes(1);
153 it('should ensure host list call happened', () => {
154 const hostServiceSpy = spyOn(hostService, 'list').and.callThrough();
155 component.onSubmit();
156 expect(hostServiceSpy).toHaveBeenCalledTimes(1);
159 it('should show skip button in the Create OSDs Steps', () => {
160 component.createCluster();
161 fixture.detectChanges();
163 component.onNextStep();
164 fixture.detectChanges();
165 const skipBtn = fixture.debugElement.query(By.css('#skipStepBtn')).nativeElement;
166 expect(skipBtn).not.toBe(null);
167 expect(skipBtn.innerHTML).toBe('Skip');
170 it('should skip the Create OSDs Steps', () => {
171 component.createCluster();
172 fixture.detectChanges();
174 component.onNextStep();
175 fixture.detectChanges();
176 const skipBtn = fixture.debugElement.query(By.css('#skipStepBtn')).nativeElement;
178 fixture.detectChanges();
180 expect(component.stepsToSkip['Create OSDs']).toBe(true);