]> git.apps.os.sepia.ceph.com Git - ceph.git/blob
1cb2b0167069a1f5f72b1b24b73ab20504c7edf9
[ceph.git] /
1 import { HttpClientTestingModule } from '@angular/common/http/testing';
2 import { ReactiveFormsModule } from '@angular/forms';
3 import { RouterTestingModule } from '@angular/router/testing';
4 import { ComponentFixture, TestBed } from '@angular/core/testing';
5
6 import { ToastrModule } from 'ngx-toastr';
7
8 import { NgbActiveModal, NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
9
10 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
11 import { SharedModule } from '~/app/shared/shared.module';
12
13 import { NvmeofNamespacesFormComponent } from './nvmeof-namespaces-form.component';
14 import { FormHelper } from '~/testing/unit-test-helper';
15 import { NvmeofService } from '~/app/shared/api/nvmeof.service';
16
17 describe('NvmeofNamespacesFormComponent', () => {
18   let component: NvmeofNamespacesFormComponent;
19   let fixture: ComponentFixture<NvmeofNamespacesFormComponent>;
20   let nvmeofService: NvmeofService;
21   let form: CdFormGroup;
22   let formHelper: FormHelper;
23   const mockTimestamp = 1720693470789;
24   const mockSubsystemNQN = 'nqn.2021-11.com.example:subsystem';
25
26   beforeEach(async () => {
27     spyOn(Date, 'now').and.returnValue(mockTimestamp);
28     await TestBed.configureTestingModule({
29       declarations: [NvmeofNamespacesFormComponent],
30       providers: [NgbActiveModal],
31       imports: [
32         HttpClientTestingModule,
33         NgbTypeaheadModule,
34         ReactiveFormsModule,
35         RouterTestingModule,
36         SharedModule,
37         ToastrModule.forRoot()
38       ]
39     }).compileComponents();
40
41     fixture = TestBed.createComponent(NvmeofNamespacesFormComponent);
42     component = fixture.componentInstance;
43     component.ngOnInit();
44     form = component.nsForm;
45     formHelper = new FormHelper(form);
46     fixture.detectChanges();
47   });
48
49   it('should create', () => {
50     expect(component).toBeTruthy();
51   });
52
53   describe('should test form', () => {
54     beforeEach(() => {
55       component.subsystemNQN = mockSubsystemNQN;
56       nvmeofService = TestBed.inject(NvmeofService);
57       spyOn(nvmeofService, 'createNamespace').and.stub();
58     });
59
60     it('should be creating request correctly', () => {
61       const image = 'nvme_ns_image:' + mockTimestamp;
62       component.onSubmit();
63       expect(nvmeofService.createNamespace).toHaveBeenCalledWith(mockSubsystemNQN, {
64         rbd_image_name: image,
65         rbd_pool: null,
66         rbd_image_size: 1073741824
67       });
68     });
69
70     it('should give error on invalid image name', () => {
71       formHelper.setValue('image', '/ghfhdlk;kd;@');
72       component.onSubmit();
73       formHelper.expectError('image', 'pattern');
74     });
75
76     it('should give error on invalid image size', () => {
77       formHelper.setValue('image_size', -56);
78       component.onSubmit();
79       formHelper.expectError('image_size', 'pattern');
80     });
81
82     it('should give error on 0 image size', () => {
83       formHelper.setValue('image_size', 0);
84       component.onSubmit();
85       formHelper.expectError('image_size', 'min');
86     });
87   });
88 });