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