]> git.ipfire.org Git - thirdparty/paperless-ngx.git/blob
a03a1ca625e97d5c94764abc710dc33ece1d3771
[thirdparty/paperless-ngx.git] /
1 import { ComponentFixture, TestBed } from '@angular/core/testing'
2
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'
13
14 describe('CustomFieldsBulkEditDialogComponent', () => {
15 let component: CustomFieldsBulkEditDialogComponent
16 let fixture: ComponentFixture<CustomFieldsBulkEditDialogComponent>
17 let documentService: DocumentService
18 let activeModal: NgbActiveModal
19
20 beforeEach(async () => {
21 TestBed.configureTestingModule({
22 declarations: [CustomFieldsBulkEditDialogComponent, SelectComponent],
23 imports: [FormsModule, ReactiveFormsModule, NgbModule, NgSelectModule],
24 providers: [
25 NgbActiveModal,
26 provideHttpClient(),
27 provideHttpClientTesting(),
28 ],
29 }).compileComponents()
30
31 fixture = TestBed.createComponent(CustomFieldsBulkEditDialogComponent)
32 component = fixture.componentInstance
33 documentService = TestBed.inject(DocumentService)
34 activeModal = TestBed.inject(NgbActiveModal)
35 fixture.detectChanges()
36 })
37
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 },
42 ]
43 component.fieldsToAddIds = [1, 2]
44 expect(component.form.contains('1')).toBeTruthy()
45 expect(component.form.contains('2')).toBeTruthy()
46 })
47
48 it('should emit succeeded event and close modal on successful save', () => {
49 const editSpy = jest
50 .spyOn(documentService, 'bulkEdit')
51 .mockReturnValue(of('Success'))
52 const successSpy = jest.spyOn(component.succeeded, 'emit')
53
54 component.documents = [1, 2]
55 component.fieldsToAddIds = [1]
56 component.form.controls['1'].setValue('Value 1')
57 component.save()
58
59 expect(editSpy).toHaveBeenCalled()
60 expect(successSpy).toHaveBeenCalled()
61 })
62
63 it('should emit failed event on save error', () => {
64 const editSpy = jest
65 .spyOn(documentService, 'bulkEdit')
66 .mockReturnValue(throwError(new Error('Error')))
67 const failSpy = jest.spyOn(component.failed, 'emit')
68
69 component.documents = [1, 2]
70 component.fieldsToAddIds = [1]
71 component.form.controls['1'].setValue('Value 1')
72 component.save()
73
74 expect(editSpy).toHaveBeenCalled()
75 expect(failSpy).toHaveBeenCalled()
76 })
77
78 it('should close modal on cancel', () => {
79 const activeModalSpy = jest.spyOn(activeModal, 'close')
80 component.cancel()
81 expect(activeModalSpy).toHaveBeenCalled()
82 })
83
84 it('should remove field from selected fields', () => {
85 component.fieldsToAddIds = [1, 2]
86 component.removeField(1)
87 expect(component.fieldsToAddIds).toEqual([2])
88 })
89 })