]> git.ipfire.org Git - thirdparty/paperless-ngx.git/blob
b47101f6e7ec0c82b98a8d7822ca2f301a57f11d
[thirdparty/paperless-ngx.git] /
1 import { ComponentFixture, TestBed } from '@angular/core/testing'
2
3 import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
4 import { provideHttpClientTesting } from '@angular/common/http/testing'
5 import { FormsModule, ReactiveFormsModule } from '@angular/forms'
6 import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
7 import { PdfViewerModule } from 'ng2-pdf-viewer'
8 import { NgxBootstrapIconsModule, allIcons } from 'ngx-bootstrap-icons'
9 import { of } from 'rxjs'
10 import { DocumentService } from 'src/app/services/rest/document.service'
11 import { SplitConfirmDialogComponent } from './split-confirm-dialog.component'
12
13 describe('SplitConfirmDialogComponent', () => {
14 let component: SplitConfirmDialogComponent
15 let fixture: ComponentFixture<SplitConfirmDialogComponent>
16 let documentService: DocumentService
17
18 beforeEach(async () => {
19 await TestBed.configureTestingModule({
20 imports: [
21 NgxBootstrapIconsModule.pick(allIcons),
22 ReactiveFormsModule,
23 FormsModule,
24 PdfViewerModule,
25 SplitConfirmDialogComponent,
26 ],
27 providers: [
28 NgbActiveModal,
29 provideHttpClient(withInterceptorsFromDi()),
30 provideHttpClientTesting(),
31 ],
32 }).compileComponents()
33
34 fixture = TestBed.createComponent(SplitConfirmDialogComponent)
35 documentService = TestBed.inject(DocumentService)
36 component = fixture.componentInstance
37 fixture.detectChanges()
38 })
39
40 it('should load document on init', () => {
41 const getSpy = jest.spyOn(documentService, 'get')
42 component.documentID = 1
43 getSpy.mockReturnValue(of({ id: 1 } as any))
44 component.ngOnInit()
45 expect(documentService.get).toHaveBeenCalledWith(1)
46 })
47
48 it('should update pagesString when pages are added', () => {
49 component.totalPages = 5
50 component.page = 2
51 component.addSplit()
52 expect(component.pagesString).toEqual('1-2,3-5')
53 component.page = 4
54 component.addSplit()
55 expect(component.pagesString).toEqual('1-2,3-4,5')
56 })
57
58 it('should update pagesString when pages are removed', () => {
59 component.totalPages = 5
60 component.page = 2
61 component.addSplit()
62 component.page = 4
63 component.addSplit()
64 expect(component.pagesString).toEqual('1-2,3-4,5')
65 component.removeSplit(0)
66 expect(component.pagesString).toEqual('1-4,5')
67 })
68
69 it('should enable confirm button when pages are added', () => {
70 component.totalPages = 5
71 component.page = 2
72 component.addSplit()
73 expect(component.confirmButtonEnabled).toBeTruthy()
74 })
75
76 it('should disable confirm button when all pages are removed', () => {
77 component.totalPages = 5
78 component.page = 2
79 component.addSplit()
80 component.removeSplit(0)
81 expect(component.confirmButtonEnabled).toBeFalsy()
82 })
83
84 it('should not add split if page is the last page', () => {
85 component.totalPages = 5
86 component.page = 5
87 component.addSplit()
88 expect(component.pagesString).toEqual('1-5')
89 })
90
91 it('should update totalPages when pdf is loaded', () => {
92 component.pdfPreviewLoaded({ numPages: 5 } as any)
93 expect(component.totalPages).toEqual(5)
94 })
95
96 it('should correctly disable split button', () => {
97 component.totalPages = 5
98 component.page = 1
99 expect(component.canSplit).toBeTruthy()
100 component.page = 5
101 expect(component.canSplit).toBeFalsy()
102 component.page = 4
103 expect(component.canSplit).toBeTruthy()
104 component['pages'] = new Set([1, 2, 3, 4])
105 expect(component.canSplit).toBeFalsy()
106 })
107 })