]> git.ipfire.org Git - thirdparty/paperless-ngx.git/blob
d2fe17813e96656fdcdb009ec291867b830a63f2
[thirdparty/paperless-ngx.git] /
1 import { ComponentFixture, TestBed } from '@angular/core/testing'
2 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
3 import { MergeConfirmDialogComponent } from './merge-confirm-dialog.component'
4 import { provideHttpClientTesting } from '@angular/common/http/testing'
5 import { NgxBootstrapIconsModule, allIcons } from 'ngx-bootstrap-icons'
6 import { of } from 'rxjs'
7 import { DocumentService } from 'src/app/services/rest/document.service'
8 import { FormsModule, ReactiveFormsModule } from '@angular/forms'
9 import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
10
11 describe('MergeConfirmDialogComponent', () => {
12 let component: MergeConfirmDialogComponent
13 let fixture: ComponentFixture<MergeConfirmDialogComponent>
14 let documentService: DocumentService
15
16 beforeEach(async () => {
17 await TestBed.configureTestingModule({
18 declarations: [MergeConfirmDialogComponent],
19 imports: [
20 NgxBootstrapIconsModule.pick(allIcons),
21 ReactiveFormsModule,
22 FormsModule,
23 ],
24 providers: [
25 NgbActiveModal,
26 provideHttpClient(withInterceptorsFromDi()),
27 provideHttpClientTesting(),
28 ],
29 }).compileComponents()
30
31 fixture = TestBed.createComponent(MergeConfirmDialogComponent)
32 documentService = TestBed.inject(DocumentService)
33 component = fixture.componentInstance
34 fixture.detectChanges()
35 })
36
37 it('should fetch documents on ngOnInit', () => {
38 const documents = [
39 { id: 1, name: 'Document 1' },
40 { id: 2, name: 'Document 2' },
41 { id: 3, name: 'Document 3' },
42 ]
43 jest.spyOn(documentService, 'getFew').mockReturnValue(
44 of({
45 all: documents.map((d) => d.id),
46 count: documents.length,
47 results: documents,
48 })
49 )
50
51 component.ngOnInit()
52
53 expect(component.documents).toEqual(documents)
54 expect(documentService.getFew).toHaveBeenCalledWith(component.documentIDs)
55 })
56
57 it('should move documentIDs on drop', () => {
58 component.documentIDs = [1, 2, 3]
59 const event = {
60 previousIndex: 1,
61 currentIndex: 2,
62 }
63
64 component.onDrop(event as any)
65
66 expect(component.documentIDs).toEqual([1, 3, 2])
67 })
68
69 it('should get document by ID', () => {
70 const documents = [
71 { id: 1, name: 'Document 1' },
72 { id: 2, name: 'Document 2' },
73 { id: 3, name: 'Document 3' },
74 ]
75 jest.spyOn(documentService, 'getFew').mockReturnValue(
76 of({
77 all: documents.map((d) => d.id),
78 count: documents.length,
79 results: documents,
80 })
81 )
82
83 component.ngOnInit()
84
85 expect(component.getDocument(2)).toEqual({ id: 2, name: 'Document 2' })
86 })
87 })