]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
1ebdfb3a59d2f93ea46012b5929ece2723804e02
[ceph.git] /
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';
5
6 import { ToastrModule } from 'ngx-toastr';
7
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';
18
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;
26
27   configureTestBed({
28     imports: [
29       HttpClientTestingModule,
30       RouterTestingModule,
31       ToastrModule.forRoot(),
32       SharedModule,
33       CoreModule,
34       CephModule
35     ]
36   });
37
38   beforeEach(() => {
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.
45       close: jest.fn()
46     });
47     fixture.detectChanges();
48   });
49
50   it('should create', () => {
51     expect(component).toBeTruthy();
52   });
53
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}`);
57   });
58
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);
63   });
64
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);
70   });
71
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');
77   });
78
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);
84   });
85
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');
97   });
98
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');
106
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');
114   });
115 });