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';
6 import { ToastrModule } from 'ngx-toastr';
8 import { NgbActiveModal, NgbTypeaheadModule } from '@ng-bootstrap/ng-bootstrap';
10 import { CdFormGroup } from '~/app/shared/forms/cd-form-group';
11 import { SharedModule } from '~/app/shared/shared.module';
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';
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';
26 beforeEach(async () => {
27 spyOn(Date, 'now').and.returnValue(mockTimestamp);
28 await TestBed.configureTestingModule({
29 declarations: [NvmeofNamespacesFormComponent],
30 providers: [NgbActiveModal],
32 HttpClientTestingModule,
37 ToastrModule.forRoot()
39 }).compileComponents();
41 fixture = TestBed.createComponent(NvmeofNamespacesFormComponent);
42 component = fixture.componentInstance;
44 form = component.nsForm;
45 formHelper = new FormHelper(form);
46 fixture.detectChanges();
49 it('should create', () => {
50 expect(component).toBeTruthy();
53 describe('should test form', () => {
55 component.subsystemNQN = mockSubsystemNQN;
56 nvmeofService = TestBed.inject(NvmeofService);
57 spyOn(nvmeofService, 'createNamespace').and.stub();
60 it('should be creating request correctly', () => {
61 const image = 'nvme_ns_image:' + mockTimestamp;
63 expect(nvmeofService.createNamespace).toHaveBeenCalledWith(mockSubsystemNQN, {
64 rbd_image_name: image,
66 rbd_image_size: 1073741824
70 it('should give error on invalid image name', () => {
71 formHelper.setValue('image', '/ghfhdlk;kd;@');
73 formHelper.expectError('image', 'pattern');
76 it('should give error on invalid image size', () => {
77 formHelper.setValue('image_size', -56);
79 formHelper.expectError('image_size', 'pattern');
82 it('should give error on 0 image size', () => {
83 formHelper.setValue('image_size', 0);
85 formHelper.expectError('image_size', 'min');