|
| 1 | +/* |
| 2 | + * Copyright French Prime minister Office/SGMAP/DINSIC/Vitam Program (2019-2022) |
| 3 | + * and the signatories of the "VITAM - Accord du Contributeur" agreement. |
| 4 | + * |
| 5 | + * contact@programmevitam.fr |
| 6 | + * |
| 7 | + * This software is a computer program whose purpose is to implement |
| 8 | + * implement a digital archiving front-office system for the secure and |
| 9 | + * efficient high volumetry VITAM solution. |
| 10 | + * |
| 11 | + * This software is governed by the CeCILL-C license under French law and |
| 12 | + * abiding by the rules of distribution of free software. You can use, |
| 13 | + * modify and/ or redistribute the software under the terms of the CeCILL-C |
| 14 | + * license as circulated by CEA, CNRS and INRIA at the following URL |
| 15 | + * "http://www.cecill.info". |
| 16 | + * |
| 17 | + * As a counterpart to the access to the source code and rights to copy, |
| 18 | + * modify and redistribute granted by the license, users are provided only |
| 19 | + * with a limited warranty and the software's author, the holder of the |
| 20 | + * economic rights, and the successive licensors have only limited |
| 21 | + * liability. |
| 22 | + * |
| 23 | + * In this respect, the user's attention is drawn to the risks associated |
| 24 | + * with loading, using, modifying and/or developing or reproducing the |
| 25 | + * software by the user in light of its specific status of free software, |
| 26 | + * that may mean that it is complicated to manipulate, and that also |
| 27 | + * therefore means that it is reserved for developers and experienced |
| 28 | + * professionals having in-depth computer knowledge. Users are therefore |
| 29 | + * encouraged to load and test the software's suitability as regards their |
| 30 | + * requirements in conditions enabling the security of their systems and/or |
| 31 | + * data to be ensured and, more generally, to use and operate it in the |
| 32 | + * same conditions as regards security. |
| 33 | + * |
| 34 | + * The fact that you are presently reading this means that you have had |
| 35 | + * knowledge of the CeCILL-C license and that you accept its terms. |
| 36 | + */ |
| 37 | +import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing'; |
| 38 | + |
| 39 | +import { CreateProfilNoticeComponent } from './create-profil-notice.component'; |
| 40 | +import { MatDialogRef } from '@angular/material/dialog'; |
| 41 | +import { ProfileService } from '../../core/services/profile.service'; |
| 42 | +import { ApplicationService, InputComponent, SelectComponent, SlideToggleComponent } from 'vitamui-library'; |
| 43 | +import { TranslateModule, TranslateService } from '@ngx-translate/core'; |
| 44 | +import { BehaviorSubject, of } from 'rxjs'; |
| 45 | +import { FormBuilder, ReactiveFormsModule, Validators } from '@angular/forms'; |
| 46 | +import { ProfileType } from '../../models/profile-type.enum'; |
| 47 | +import { MatRadioModule } from '@angular/material/radio'; |
| 48 | +import { NoopAnimationsModule } from '@angular/platform-browser/animations'; |
| 49 | + |
| 50 | +describe('CreateProfilNoticeComponent', () => { |
| 51 | + let component: CreateProfilNoticeComponent; |
| 52 | + let fixture: ComponentFixture<CreateProfilNoticeComponent>; |
| 53 | + let mockDialogRef: jasmine.SpyObj<MatDialogRef<CreateProfilNoticeComponent>>; |
| 54 | + let mockProfileService: jasmine.SpyObj<ProfileService>; |
| 55 | + let mockAppService: jasmine.SpyObj<ApplicationService>; |
| 56 | + let mockTranslate: jasmine.SpyObj<TranslateService>; |
| 57 | + let externalIdSubject: BehaviorSubject<boolean>; |
| 58 | + |
| 59 | + beforeEach(() => { |
| 60 | + mockDialogRef = jasmine.createSpyObj('MatDialogRef', ['close']); |
| 61 | + mockProfileService = jasmine.createSpyObj('ProfileService', ['checkPuaProfile', 'checkPaProfile', 'controlSchema']); |
| 62 | + mockAppService = jasmine.createSpyObj('ApplicationService', ['isApplicationExternalIdentifierEnabled']); |
| 63 | + mockProfileService.controlSchema = new BehaviorSubject<string>(JSON.stringify({ additionalProperties: true })); |
| 64 | + mockTranslate = jasmine.createSpyObj('TranslateService', ['instant']); |
| 65 | + |
| 66 | + mockTranslate.instant.and.callFake((key: string) => key); |
| 67 | + externalIdSubject = new BehaviorSubject<boolean>(true); |
| 68 | + mockAppService.isApplicationExternalIdentifierEnabled.and.returnValue(externalIdSubject.asObservable()); |
| 69 | + |
| 70 | + TestBed.configureTestingModule({ |
| 71 | + imports: [ |
| 72 | + ReactiveFormsModule, |
| 73 | + TranslateModule.forRoot(), |
| 74 | + MatRadioModule, |
| 75 | + InputComponent, |
| 76 | + SelectComponent, |
| 77 | + SlideToggleComponent, |
| 78 | + NoopAnimationsModule, |
| 79 | + CreateProfilNoticeComponent, |
| 80 | + ], |
| 81 | + providers: [ |
| 82 | + FormBuilder, |
| 83 | + { provide: MatDialogRef, useValue: mockDialogRef }, |
| 84 | + { provide: ProfileService, useValue: mockProfileService }, |
| 85 | + { provide: ApplicationService, useValue: mockAppService }, |
| 86 | + ], |
| 87 | + }).compileComponents(); |
| 88 | + |
| 89 | + fixture = TestBed.createComponent(CreateProfilNoticeComponent); |
| 90 | + component = fixture.componentInstance; |
| 91 | + }); |
| 92 | + |
| 93 | + it('should validate identifier via checkPaProfile when not PUA', fakeAsync(() => { |
| 94 | + component.profileTypeSignal.set(ProfileType.PA); |
| 95 | + fixture.detectChanges(); |
| 96 | + mockProfileService.checkPaProfile.and.returnValue(of(true)); |
| 97 | + component.noticeForm.controls.identifier?.setValue('DUPLICATE'); |
| 98 | + |
| 99 | + expect(mockProfileService.checkPaProfile).toHaveBeenCalledWith(jasmine.objectContaining({ identifier: 'DUPLICATE' })); |
| 100 | + })); |
| 101 | + |
| 102 | + it('should disable identifier validators if External ID is disabled', fakeAsync(() => { |
| 103 | + fixture.detectChanges(); |
| 104 | + component.noticeForm.controls.profileType?.setValue(ProfileType.PUA); |
| 105 | + |
| 106 | + externalIdSubject.next(false); |
| 107 | + |
| 108 | + component.profileTypeSignal.set(ProfileType.PA); |
| 109 | + component.profileTypeSignal.set(ProfileType.PUA); |
| 110 | + fixture.detectChanges(); |
| 111 | + |
| 112 | + const identifierCtrl = component.noticeForm.controls.identifier; |
| 113 | + |
| 114 | + expect(component.externalIdentifierEnabledSignal()).toBeFalse(); |
| 115 | + |
| 116 | + expect(identifierCtrl?.hasValidator(Validators.required)).toBeFalse(); |
| 117 | + expect(identifierCtrl?.hasValidator(Validators.minLength(2))).toBeFalse(); |
| 118 | + expect(identifierCtrl?.hasValidator(Validators.maxLength(100))).toBeFalse(); |
| 119 | + |
| 120 | + identifierCtrl?.setValue(''); |
| 121 | + expect(identifierCtrl?.valid).toBeTrue(); |
| 122 | + expect(identifierCtrl?.validator).toBeNull(); |
| 123 | + })); |
| 124 | + |
| 125 | + it('should check PA and set isIdentifierOK to true if unique', fakeAsync(() => { |
| 126 | + component.profileTypeSignal.set(ProfileType.PA); |
| 127 | + fixture.detectChanges(); |
| 128 | + mockProfileService.checkPaProfile.and.returnValue(of(false)); |
| 129 | + component.noticeForm.controls.identifier?.setValue('UNIQUE_TEST'); |
| 130 | + |
| 131 | + expect(mockProfileService.checkPaProfile).toHaveBeenCalledWith(jasmine.objectContaining({ identifier: 'UNIQUE_TEST' })); |
| 132 | + expect(component.noticeForm.controls.identifier.invalid).toBeFalse(); |
| 133 | + })); |
| 134 | + |
| 135 | + it('should check PA and alert if duplicated', fakeAsync(() => { |
| 136 | + component.profileTypeSignal.set(ProfileType.PA); |
| 137 | + fixture.detectChanges(); |
| 138 | + mockAppService.isApplicationExternalIdentifierEnabled.and.returnValue(of(true)); |
| 139 | + mockProfileService.checkPaProfile.and.returnValue(of(true)); |
| 140 | + |
| 141 | + component.noticeForm.controls.identifier?.setValue('DUPLICATE_PA'); |
| 142 | + |
| 143 | + expect(mockProfileService.checkPaProfile).toHaveBeenCalledWith(jasmine.objectContaining({ identifier: 'DUPLICATE_PA' })); |
| 144 | + expect(component.noticeForm.controls.identifier.invalid).toBeTrue(); |
| 145 | + })); |
| 146 | + |
| 147 | + it('should check PUA and set isIdentifierOK to true if unique', fakeAsync(() => { |
| 148 | + component.profileTypeSignal.set(ProfileType.PUA); |
| 149 | + fixture.detectChanges(); |
| 150 | + mockProfileService.checkPuaProfile.and.returnValue(of(false)); |
| 151 | + component.noticeForm.controls.identifier?.setValue('UNIQUE_PUA'); |
| 152 | + |
| 153 | + expect(mockProfileService.checkPuaProfile).toHaveBeenCalledWith(jasmine.objectContaining({ identifier: 'UNIQUE_PUA' })); |
| 154 | + expect(component.noticeForm.controls.identifier.invalid).toBeFalse(); |
| 155 | + })); |
| 156 | + |
| 157 | + it('should check PUA and alert if duplicated', fakeAsync(() => { |
| 158 | + component.profileTypeSignal.set(ProfileType.PUA); |
| 159 | + fixture.detectChanges(); |
| 160 | + mockProfileService.checkPuaProfile.and.returnValue(of(true)); |
| 161 | + component.noticeForm.controls.identifier?.setValue('DUPLICATE_TEST'); |
| 162 | + |
| 163 | + expect(mockProfileService.checkPuaProfile).toHaveBeenCalledWith(jasmine.objectContaining({ identifier: 'DUPLICATE_TEST' })); |
| 164 | + component.noticeForm.controls.profileType?.setValue(ProfileType.PUA); |
| 165 | + expect(component.noticeForm.controls.identifier.invalid).toBeTrue(); |
| 166 | + })); |
| 167 | + |
| 168 | + it('should create', () => { |
| 169 | + expect(component).toBeTruthy(); |
| 170 | + }); |
| 171 | +}); |
0 commit comments