]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/blob
810a5c9625d82597c1e2f4e37d1e611d3cc50613
[ceph-ci.git] /
1 import { ComponentFixture, TestBed } from '@angular/core/testing';
2
3 import { SmbJoinAuthFormComponent } from './smb-join-auth-form.component';
4 import { ToastrModule } from 'ngx-toastr';
5 import { SharedModule } from '~/app/shared/shared.module';
6 import { provideHttpClient } from '@angular/common/http';
7 import { provideHttpClientTesting } from '@angular/common/http/testing';
8 import { provideRouter } from '@angular/router';
9 import { ReactiveFormsModule } from '@angular/forms';
10 import { SmbService } from '~/app/shared/api/smb.service';
11 import { JOIN_AUTH_RESOURCE } from '../smb.model';
12 import { of } from 'rxjs';
13
14 export const FOO_JOIN_AUTH = {
15   auth_id: 'foo',
16   auth: {
17     username: 'user',
18     password: 'pass'
19   },
20   resource_type: JOIN_AUTH_RESOURCE
21 };
22
23 describe('SmbJoinAuthFormComponent', () => {
24   let component: SmbJoinAuthFormComponent;
25   let fixture: ComponentFixture<SmbJoinAuthFormComponent>;
26   let createJoinAuth: jasmine.Spy;
27   let getJoinAuth: jasmine.Spy;
28
29   beforeEach(async () => {
30     await TestBed.configureTestingModule({
31       imports: [ToastrModule.forRoot(), SharedModule, ReactiveFormsModule],
32       declarations: [SmbJoinAuthFormComponent],
33       providers: [provideHttpClient(), provideHttpClientTesting(), provideRouter([])]
34     }).compileComponents();
35
36     fixture = TestBed.createComponent(SmbJoinAuthFormComponent);
37     component = fixture.componentInstance;
38     component.ngOnInit();
39     createJoinAuth = spyOn(TestBed.inject(SmbService), 'createJoinAuth');
40     getJoinAuth = spyOn(TestBed.inject(SmbService), 'getJoinAuth');
41     fixture.detectChanges();
42   });
43
44   it('should create', () => {
45     expect(component).toBeTruthy();
46   });
47
48   it('should set form invalid if any required fields are missing', () => {
49     component.form.controls['authId'].setValue('');
50     component.form.controls['username'].setValue('');
51     component.form.controls['password'].setValue('');
52     expect(component.form.valid).not.toBeNull();
53   });
54
55   it('should submit the form', () => {
56     component.form.controls['authId'].setValue('foo');
57     component.form.controls['username'].setValue('user');
58     component.form.controls['password'].setValue('pass');
59     component.form.controls['linkedToCluster'].setValue(undefined);
60
61     component.submit();
62
63     expect(createJoinAuth).toHaveBeenCalledWith(FOO_JOIN_AUTH);
64   });
65
66   describe('when editing', () => {
67     beforeEach(() => {
68       component.editing = true;
69       getJoinAuth.and.returnValue(of(FOO_JOIN_AUTH));
70       component.ngOnInit();
71       fixture.detectChanges();
72     });
73
74     it('should get resource data and set form fields with it', () => {
75       expect(getJoinAuth).toHaveBeenCalled();
76       expect(component.form.value).toEqual({
77         authId: 'foo',
78         username: 'user',
79         password: 'pass',
80         linkedToCluster: undefined
81       });
82     });
83   });
84 });