1 import { ComponentFixture, TestBed } from '@angular/core/testing'
3 import { provideHttpClient } from '@angular/common/http'
4 import { provideHttpClientTesting } from '@angular/common/http/testing'
5 import { FormsModule, ReactiveFormsModule } from '@angular/forms'
6 import { NgbActiveModal, NgbModule } from '@ng-bootstrap/ng-bootstrap'
7 import { NgSelectModule } from '@ng-select/ng-select'
8 import { of, throwError } from 'rxjs'
9 import { SelectComponent } from 'src/app/components/common/input/select/select.component'
10 import { CustomFieldDataType } from 'src/app/data/custom-field'
11 import { DocumentService } from 'src/app/services/rest/document.service'
12 import { CustomFieldsBulkEditDialogComponent } from './custom-fields-bulk-edit-dialog.component'
14 describe('CustomFieldsBulkEditDialogComponent', () => {
15 let component: CustomFieldsBulkEditDialogComponent
16 let fixture: ComponentFixture<CustomFieldsBulkEditDialogComponent>
17 let documentService: DocumentService
18 let activeModal: NgbActiveModal
20 beforeEach(async () => {
21 TestBed.configureTestingModule({
22 declarations: [CustomFieldsBulkEditDialogComponent, SelectComponent],
23 imports: [FormsModule, ReactiveFormsModule, NgbModule, NgSelectModule],
27 provideHttpClientTesting(),
29 }).compileComponents()
31 fixture = TestBed.createComponent(CustomFieldsBulkEditDialogComponent)
32 component = fixture.componentInstance
33 documentService = TestBed.inject(DocumentService)
34 activeModal = TestBed.inject(NgbActiveModal)
35 fixture.detectChanges()
38 it('should initialize form controls based on selected field ids', () => {
39 component.customFields = [
40 { id: 1, name: 'Field 1', data_type: CustomFieldDataType.String },
41 { id: 2, name: 'Field 2', data_type: CustomFieldDataType.Integer },
43 component.fieldsToAddIds = [1, 2]
44 expect(component.form.contains('1')).toBeTruthy()
45 expect(component.form.contains('2')).toBeTruthy()
48 it('should emit succeeded event and close modal on successful save', () => {
50 .spyOn(documentService, 'bulkEdit')
51 .mockReturnValue(of('Success'))
52 const successSpy = jest.spyOn(component.succeeded, 'emit')
54 component.documents = [1, 2]
55 component.fieldsToAddIds = [1]
56 component.form.controls['1'].setValue('Value 1')
59 expect(editSpy).toHaveBeenCalled()
60 expect(successSpy).toHaveBeenCalled()
63 it('should emit failed event on save error', () => {
65 .spyOn(documentService, 'bulkEdit')
66 .mockReturnValue(throwError(new Error('Error')))
67 const failSpy = jest.spyOn(component.failed, 'emit')
69 component.documents = [1, 2]
70 component.fieldsToAddIds = [1]
71 component.form.controls['1'].setValue('Value 1')
74 expect(editSpy).toHaveBeenCalled()
75 expect(failSpy).toHaveBeenCalled()
78 it('should close modal on cancel', () => {
79 const activeModalSpy = jest.spyOn(activeModal, 'close')
81 expect(activeModalSpy).toHaveBeenCalled()
84 it('should remove field from selected fields', () => {
85 component.fieldsToAddIds = [1, 2]
86 component.removeField(1)
87 expect(component.fieldsToAddIds).toEqual([2])