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 { AppConstants } from '~/app/shared/constants/app.constants';
14 import { ModalService } from '~/app/shared/services/modal.service';
15 import { WizardStepsService } from '~/app/shared/services/wizard-steps.service';
16 import { SharedModule } from '~/app/shared/shared.module';
17 import { configureTestBed } from '~/testing/unit-test-helper';
18 import { CreateClusterComponent } from './create-cluster.component';
20 describe('CreateClusterComponent', () => {
21 let component: CreateClusterComponent;
22 let fixture: ComponentFixture<CreateClusterComponent>;
23 let wizardStepService: WizardStepsService;
24 let hostService: HostService;
25 let osdService: OsdService;
26 let modalServiceShowSpy: jasmine.Spy;
27 const projectConstants: typeof AppConstants = AppConstants;
31 HttpClientTestingModule,
33 ToastrModule.forRoot(),
41 fixture = TestBed.createComponent(CreateClusterComponent);
42 component = fixture.componentInstance;
43 wizardStepService = TestBed.inject(WizardStepsService);
44 hostService = TestBed.inject(HostService);
45 osdService = TestBed.inject(OsdService);
46 modalServiceShowSpy = spyOn(TestBed.inject(ModalService), 'show').and.returnValue({
47 // mock the close function, it might be called if there are async tests.
50 fixture.detectChanges();
53 it('should create', () => {
54 expect(component).toBeTruthy();
57 it('should have project name as heading in welcome screen', () => {
58 component.startClusterCreation = true;
59 fixture.detectChanges();
60 const heading = fixture.debugElement.query(By.css('h3')).nativeElement;
61 expect(heading.innerHTML).toBe(`Welcome to ${projectConstants.projectName}`);
64 // @TODO: Opening modals in unit testing is broken since carbon.
65 // Need to fix it properly
66 it.skip('should show confirmation modal when cluster creation is skipped', () => {
67 component.skipClusterCreation();
68 expect(modalServiceShowSpy.calls.any()).toBeTruthy();
69 expect(modalServiceShowSpy.calls.first().args[0]).toBe(ConfirmationModalComponent);
72 it('should show the wizard when cluster creation is started', () => {
73 component.createCluster();
74 fixture.detectChanges();
75 const nativeEl = fixture.debugElement.nativeElement;
76 expect(nativeEl.querySelector('cd-wizard')).not.toBe(null);
79 it('should have title Add Hosts', () => {
80 component.createCluster();
81 fixture.detectChanges();
82 const heading = fixture.debugElement.query(By.css('.title')).nativeElement;
83 expect(heading.innerHTML).toBe('Add Hosts');
86 it('should show the host list when cluster creation as first step', () => {
87 component.createCluster();
88 fixture.detectChanges();
89 const nativeEl = fixture.debugElement.nativeElement;
90 expect(nativeEl.querySelector('cd-hosts')).not.toBe(null);
93 it('should move to next step and show the second page', () => {
94 const wizardStepServiceSpy = spyOn(wizardStepService, 'moveToNextStep').and.callThrough();
95 component.createCluster();
96 fixture.detectChanges();
97 component.onNextStep();
98 fixture.detectChanges();
99 expect(wizardStepServiceSpy).toHaveBeenCalledTimes(1);
102 it('should show the button labels correctly', () => {
103 component.createCluster();
104 fixture.detectChanges();
105 let submitBtnLabel = component.showSubmitButtonLabel();
106 expect(submitBtnLabel).toEqual('Next');
107 let cancelBtnLabel = component.showCancelButtonLabel();
108 expect(cancelBtnLabel).toEqual('Cancel');
110 component.onNextStep();
111 fixture.detectChanges();
112 submitBtnLabel = component.showSubmitButtonLabel();
113 expect(submitBtnLabel).toEqual('Next');
114 cancelBtnLabel = component.showCancelButtonLabel();
115 expect(cancelBtnLabel).toEqual('Back');
117 component.onNextStep();
118 fixture.detectChanges();
119 submitBtnLabel = component.showSubmitButtonLabel();
120 expect(submitBtnLabel).toEqual('Next');
121 cancelBtnLabel = component.showCancelButtonLabel();
122 expect(cancelBtnLabel).toEqual('Back');
124 // Last page of the wizard
125 component.onNextStep();
126 fixture.detectChanges();
127 submitBtnLabel = component.showSubmitButtonLabel();
128 expect(submitBtnLabel).toEqual('Expand Cluster');
129 cancelBtnLabel = component.showCancelButtonLabel();
130 expect(cancelBtnLabel).toEqual('Back');
133 it('should ensure osd creation did not happen when no devices are selected', () => {
134 component.simpleDeployment = false;
135 const osdServiceSpy = spyOn(osdService, 'create').and.callThrough();
136 component.onSubmit();
137 fixture.detectChanges();
138 expect(osdServiceSpy).toBeCalledTimes(0);
141 it('should ensure osd creation did happen when devices are selected', () => {
142 const osdServiceSpy = spyOn(osdService, 'create').and.callThrough();
143 osdService.osdDevices['totalDevices'] = 1;
144 component.onSubmit();
145 fixture.detectChanges();
146 expect(osdServiceSpy).toBeCalledTimes(1);
149 it('should ensure host list call happened', () => {
150 const hostServiceSpy = spyOn(hostService, 'list').and.callThrough();
151 component.onSubmit();
152 expect(hostServiceSpy).toHaveBeenCalledTimes(1);
155 it('should show skip button in the Create OSDs Steps', () => {
156 component.createCluster();
157 fixture.detectChanges();
159 component.onNextStep();
160 fixture.detectChanges();
161 const skipBtn = fixture.debugElement.query(By.css('#skipStepBtn')).nativeElement;
162 expect(skipBtn).not.toBe(null);
163 expect(skipBtn.innerHTML).toBe('Skip');
166 it('should skip the Create OSDs Steps', () => {
167 component.createCluster();
168 fixture.detectChanges();
170 component.onNextStep();
171 fixture.detectChanges();
172 const skipBtn = fixture.debugElement.query(By.css('#skipStepBtn')).nativeElement;
174 fixture.detectChanges();
176 expect(component.stepsToSkip['Create OSDs']).toBe(true);