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 it('should show confirmation modal when cluster creation is skipped', () => {
69 component.skipClusterCreation();
70 expect(modalServiceShowSpy.calls.any()).toBeTruthy();
71 expect(modalServiceShowSpy.calls.first().args[0]).toBe(ConfirmationModalComponent);
74 it('should show the wizard when cluster creation is started', () => {
75 component.createCluster();
76 fixture.detectChanges();
77 const nativeEl = fixture.debugElement.nativeElement;
78 expect(nativeEl.querySelector('cd-wizard')).not.toBe(null);
81 it('should have title Add Hosts', () => {
82 component.createCluster();
83 fixture.detectChanges();
84 const heading = fixture.debugElement.query(By.css('.title')).nativeElement;
85 expect(heading.innerHTML).toBe('Add Hosts');
88 it('should show the host list when cluster creation as first step', () => {
89 component.createCluster();
90 fixture.detectChanges();
91 const nativeEl = fixture.debugElement.nativeElement;
92 expect(nativeEl.querySelector('cd-hosts')).not.toBe(null);
95 it('should move to next step and show the second page', () => {
96 const wizardStepServiceSpy = spyOn(wizardStepService, 'moveToNextStep').and.callThrough();
97 component.createCluster();
98 fixture.detectChanges();
99 component.onNextStep();
100 fixture.detectChanges();
101 expect(wizardStepServiceSpy).toHaveBeenCalledTimes(1);
104 it('should show the button labels correctly', () => {
105 component.createCluster();
106 fixture.detectChanges();
107 let submitBtnLabel = component.showSubmitButtonLabel();
108 expect(submitBtnLabel).toEqual('Next');
109 let cancelBtnLabel = component.showCancelButtonLabel();
110 expect(cancelBtnLabel).toEqual('Cancel');
112 component.onNextStep();
113 fixture.detectChanges();
114 submitBtnLabel = component.showSubmitButtonLabel();
115 expect(submitBtnLabel).toEqual('Next');
116 cancelBtnLabel = component.showCancelButtonLabel();
117 expect(cancelBtnLabel).toEqual('Back');
119 component.onNextStep();
120 fixture.detectChanges();
121 submitBtnLabel = component.showSubmitButtonLabel();
122 expect(submitBtnLabel).toEqual('Next');
123 cancelBtnLabel = component.showCancelButtonLabel();
124 expect(cancelBtnLabel).toEqual('Back');
126 // Last page of the wizard
127 component.onNextStep();
128 fixture.detectChanges();
129 submitBtnLabel = component.showSubmitButtonLabel();
130 expect(submitBtnLabel).toEqual('Expand Cluster');
131 cancelBtnLabel = component.showCancelButtonLabel();
132 expect(cancelBtnLabel).toEqual('Back');
135 it('should ensure osd creation did not happen when no devices are selected', () => {
136 component.simpleDeployment = false;
137 const osdServiceSpy = spyOn(osdService, 'create').and.callThrough();
138 component.onSubmit();
139 fixture.detectChanges();
140 expect(osdServiceSpy).toBeCalledTimes(0);
143 it('should ensure osd creation did happen when devices are selected', () => {
144 const osdServiceSpy = spyOn(osdService, 'create').and.callThrough();
145 osdService.osdDevices['totalDevices'] = 1;
146 component.onSubmit();
147 fixture.detectChanges();
148 expect(osdServiceSpy).toBeCalledTimes(1);
151 it('should ensure host list call happened', () => {
152 const hostServiceSpy = spyOn(hostService, 'list').and.callThrough();
153 component.onSubmit();
154 expect(hostServiceSpy).toHaveBeenCalledTimes(1);
157 it('should show skip button in the Create OSDs Steps', () => {
158 component.createCluster();
159 fixture.detectChanges();
161 component.onNextStep();
162 fixture.detectChanges();
163 const skipBtn = fixture.debugElement.query(By.css('#skipStepBtn')).nativeElement;
164 expect(skipBtn).not.toBe(null);
165 expect(skipBtn.innerHTML).toBe('Skip');
168 it('should skip the Create OSDs Steps', () => {
169 component.createCluster();
170 fixture.detectChanges();
172 component.onNextStep();
173 fixture.detectChanges();
174 const skipBtn = fixture.debugElement.query(By.css('#skipStepBtn')).nativeElement;
176 fixture.detectChanges();
178 expect(component.stepsToSkip['Create OSDs']).toBe(true);