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';
10 import { RbdMirroringService } from '~/app/shared/api/rbd-mirroring.service';
11 import { NotificationService } from '~/app/shared/services/notification.service';
12 import { SharedModule } from '~/app/shared/shared.module';
13 import { configureTestBed, FormHelper } from '~/testing/unit-test-helper';
14 import { BootstrapCreateModalComponent } from './bootstrap-create-modal.component';
16 describe('BootstrapCreateModalComponent', () => {
17 let component: BootstrapCreateModalComponent;
18 let fixture: ComponentFixture<BootstrapCreateModalComponent>;
19 let notificationService: NotificationService;
20 let rbdMirroringService: RbdMirroringService;
21 let formHelper: FormHelper;
24 declarations: [BootstrapCreateModalComponent],
26 HttpClientTestingModule,
30 ToastrModule.forRoot()
32 providers: [NgbActiveModal]
36 fixture = TestBed.createComponent(BootstrapCreateModalComponent);
37 component = fixture.componentInstance;
38 component.siteName = 'site-A';
40 notificationService = TestBed.inject(NotificationService);
41 spyOn(notificationService, 'show').and.stub();
43 rbdMirroringService = TestBed.inject(RbdMirroringService);
45 formHelper = new FormHelper(component.createBootstrapForm);
47 spyOn(rbdMirroringService, 'getSiteName').and.callFake(() => of({ site_name: 'site-A' }));
48 spyOn(rbdMirroringService, 'subscribeSummary').and.callFake((call) =>
52 { name: 'pool1', mirror_mode: 'disabled' },
53 { name: 'pool2', mirror_mode: 'disabled' },
54 { name: 'pool3', mirror_mode: 'disabled' }
61 it('should create', () => {
62 expect(component).toBeTruthy();
65 describe('generate token', () => {
67 spyOn(rbdMirroringService, 'refresh').and.stub();
68 spyOn(component.activeModal, 'close').and.callThrough();
69 fixture.detectChanges();
73 expect(rbdMirroringService.getSiteName).toHaveBeenCalledTimes(1);
74 expect(rbdMirroringService.subscribeSummary).toHaveBeenCalledTimes(1);
75 expect(rbdMirroringService.refresh).toHaveBeenCalledTimes(1);
78 it('should generate a bootstrap token', () => {
79 spyOn(rbdMirroringService, 'setSiteName').and.callFake(() => of({ site_name: 'new-site-A' }));
80 spyOn(rbdMirroringService, 'updatePool').and.callFake(() => of({}));
81 spyOn(rbdMirroringService, 'createBootstrapToken').and.callFake(() => of({ token: 'token' }));
83 component.createBootstrapForm.patchValue({
84 siteName: 'new-site-A',
85 pools: { pool1: true, pool3: true }
88 expect(rbdMirroringService.setSiteName).toHaveBeenCalledWith('new-site-A');
89 expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool1', {
92 expect(rbdMirroringService.updatePool).toHaveBeenCalledWith('pool3', {
95 expect(rbdMirroringService.createBootstrapToken).toHaveBeenCalledWith('pool3');
96 expect(component.createBootstrapForm.getValue('token')).toBe('token');
100 describe('form validation', () => {
102 fixture.detectChanges();
105 it('should require a site name', () => {
106 formHelper.expectErrorChange('siteName', '', 'required');
109 it('should require at least one pool', () => {
110 formHelper.expectError(component.createBootstrapForm.get('pools'), 'requirePool');