1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
3 import { ReactiveFormsModule } from '@angular/forms';
4 import { RouterTestingModule } from '@angular/router/testing';
6 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
7 import { ToastrModule } from 'ngx-toastr';
8 import { of } from 'rxjs';
14 } from '../../../../../testing/unit-test-helper';
15 import { RbdMirroringService } from '../../../../shared/api/rbd-mirroring.service';
16 import { NotificationService } from '../../../../shared/services/notification.service';
17 import { SharedModule } from '../../../../shared/shared.module';
18 import { BootstrapCreateModalComponent } from './bootstrap-create-modal.component';
20 describe('BootstrapCreateModalComponent', () => {
21 let component: BootstrapCreateModalComponent;
22 let fixture: ComponentFixture<BootstrapCreateModalComponent>;
23 let notificationService: NotificationService;
24 let rbdMirroringService: RbdMirroringService;
25 let formHelper: FormHelper;
28 declarations: [BootstrapCreateModalComponent],
30 HttpClientTestingModule,
34 ToastrModule.forRoot()
36 providers: [NgbActiveModal, i18nProviders]
40 fixture = TestBed.createComponent(BootstrapCreateModalComponent);
41 component = fixture.componentInstance;
42 component.siteName = 'site-A';
44 notificationService = TestBed.inject(NotificationService);
45 spyOn(notificationService, 'show').and.stub();
47 rbdMirroringService = TestBed.inject(RbdMirroringService);
49 formHelper = new FormHelper(component.createBootstrapForm);
51 spyOn(rbdMirroringService, 'getSiteName').and.callFake(() => of({ site_name: 'site-A' }));
52 spyOn(rbdMirroringService, 'subscribeSummary').and.callFake((call) =>
56 { name: 'pool1', mirror_mode: 'disabled' },
57 { name: 'pool2', mirror_mode: 'disabled' },
58 { name: 'pool3', mirror_mode: 'disabled' }
65 it('should create', () => {
66 expect(component).toBeTruthy();
69 describe('generate token', () => {
71 spyOn(rbdMirroringService, 'refresh').and.stub();
72 spyOn(component.activeModal, 'close').and.callThrough();
73 fixture.detectChanges();
77 expect(rbdMirroringService.getSiteName).toHaveBeenCalledTimes(1);
78 expect(rbdMirroringService.subscribeSummary).toHaveBeenCalledTimes(1);
79 expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
82 it('should generate a bootstrap token', () => {
83 spyOn(rbdMirroringService, 'setSiteName').and.callFake(() => of({ site_name: 'new-site-A' }));
84 spyOn(rbdMirroringService, 'updatePool').and.callFake(() => of({}));
85 spyOn(rbdMirroringService, 'createBootstrapToken').and.callFake(() => of({ token: 'token' }));
87 component.createBootstrapForm.patchValue({
88 siteName: 'new-site-A',
89 pools: { pool1: true, pool3: true }
92 expect(rbdMirroringService.setSiteName).toHaveBeenCalledWith('new-site-A');
93 expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool1', {
96 expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool3', {
99 expect(rbdMirroringService.createBootstrapToken).toHaveBeenCalledWith('pool3');
100 expect(component.createBootstrapForm.getValue('token')).toBe('token');
104 describe('form validation', () => {
106 fixture.detectChanges();
109 it('should require a site name', () => {
110 formHelper.expectErrorChange('siteName', '', 'required');
113 it('should require at least one pool', () => {
114 formHelper.expectError(component.createBootstrapForm.get('pools'), 'requirePool');