]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph-ci.git/blob
45e5a59b9d7001359b41a99a75961dfee9cd7327
[ceph-ci.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 import { NvmeofSubsystemsFormComponent } from './nvmeof-subsystems-form.component';
13 import { FormHelper } from '~/testing/unit-test-helper';
14 import {
15   DEFAULT_MAX_NAMESPACE_PER_SUBSYSTEM,
16   NvmeofService
17 } from '~/app/shared/api/nvmeof.service';
18
19 describe('NvmeofSubsystemsFormComponent', () => {
20   let component: NvmeofSubsystemsFormComponent;
21   let fixture: ComponentFixture<NvmeofSubsystemsFormComponent>;
22   let nvmeofService: NvmeofService;
23   let form: CdFormGroup;
24   let formHelper: FormHelper;
25   const mockTimestamp = 1720693470789;
26   const mockGroupName = 'default';
27
28   beforeEach(async () => {
29     spyOn(Date, 'now').and.returnValue(mockTimestamp);
30     await TestBed.configureTestingModule({
31       declarations: [NvmeofSubsystemsFormComponent],
32       providers: [NgbActiveModal],
33       imports: [
34         HttpClientTestingModule,
35         NgbTypeaheadModule,
36         ReactiveFormsModule,
37         RouterTestingModule,
38         SharedModule,
39         ToastrModule.forRoot()
40       ]
41     }).compileComponents();
42
43     fixture = TestBed.createComponent(NvmeofSubsystemsFormComponent);
44     component = fixture.componentInstance;
45     component.ngOnInit();
46     form = component.subsystemForm;
47     formHelper = new FormHelper(form);
48     fixture.detectChanges();
49     component.group = mockGroupName;
50   });
51
52   it('should create', () => {
53     expect(component).toBeTruthy();
54   });
55
56   describe('should test form', () => {
57     beforeEach(() => {
58       nvmeofService = TestBed.inject(NvmeofService);
59       spyOn(nvmeofService, 'createSubsystem').and.stub();
60     });
61
62     it('should be creating request correctly', () => {
63       const expectedNqn = 'nqn.2001-07.com.ceph:' + mockTimestamp;
64       component.onSubmit();
65       expect(nvmeofService.createSubsystem).toHaveBeenCalledWith({
66         nqn: expectedNqn,
67         max_namespaces: DEFAULT_MAX_NAMESPACE_PER_SUBSYSTEM,
68         enable_ha: true,
69         gw_group: mockGroupName
70       });
71     });
72
73     it('should give error on invalid nqn', () => {
74       formHelper.setValue('nqn', 'nqn:2001-07.com.ceph:');
75       component.onSubmit();
76       formHelper.expectError('nqn', 'pattern');
77     });
78
79     it('should give error on invalid max_namespaces', () => {
80       formHelper.setValue('max_namespaces', -56);
81       component.onSubmit();
82       formHelper.expectError('max_namespaces', 'pattern');
83     });
84
85     it(`should not give error on max_namespaces greater than ${DEFAULT_MAX_NAMESPACE_PER_SUBSYSTEM}`, () => {
86       const expectedNqn = 'nqn.2001-07.com.ceph:' + mockTimestamp;
87       formHelper.setValue('max_namespaces', 600);
88       component.onSubmit();
89       expect(nvmeofService.createSubsystem).toHaveBeenCalledWith({
90         nqn: expectedNqn,
91         max_namespaces: DEFAULT_MAX_NAMESPACE_PER_SUBSYSTEM,
92         enable_ha: true,
93         gw_group: mockGroupName
94       });
95     });
96
97     it('should give error on max_namespaces lesser than 1', () => {
98       formHelper.setValue('max_namespaces', 0);
99       component.onSubmit();
100       formHelper.expectError('max_namespaces', 'min');
101     });
102   });
103 });