]> git.ipfire.org Git - thirdparty/paperless-ngx.git/blob
76d1162f17b69f968bfa7889fd9f07451dde7275
[thirdparty/paperless-ngx.git] /
1 import { provideHttpClient, withInterceptorsFromDi } from '@angular/common/http'
2 import { provideHttpClientTesting } from '@angular/common/http/testing'
3 import { ComponentFixture, TestBed } from '@angular/core/testing'
4 import { FormsModule, ReactiveFormsModule } from '@angular/forms'
5 import { By } from '@angular/platform-browser'
6 import {
7 NgbAccordionButton,
8 NgbActiveModal,
9 NgbModule,
10 } from '@ng-bootstrap/ng-bootstrap'
11 import { NgSelectModule } from '@ng-select/ng-select'
12 import { of, throwError } from 'rxjs'
13 import { FILTER_TITLE } from 'src/app/data/filter-rule-type'
14 import { IfOwnerDirective } from 'src/app/directives/if-owner.directive'
15 import { IfPermissionsDirective } from 'src/app/directives/if-permissions.directive'
16 import { SafeHtmlPipe } from 'src/app/pipes/safehtml.pipe'
17 import { DocumentService } from 'src/app/services/rest/document.service'
18 import { StoragePathService } from 'src/app/services/rest/storage-path.service'
19 import { SettingsService } from 'src/app/services/settings.service'
20 import { PermissionsFormComponent } from '../../input/permissions/permissions-form/permissions-form.component'
21 import { SelectComponent } from '../../input/select/select.component'
22 import { TextComponent } from '../../input/text/text.component'
23 import { TextAreaComponent } from '../../input/textarea/textarea.component'
24 import { EditDialogMode } from '../edit-dialog.component'
25 import { StoragePathEditDialogComponent } from './storage-path-edit-dialog.component'
26
27 describe('StoragePathEditDialogComponent', () => {
28 let component: StoragePathEditDialogComponent
29 let settingsService: SettingsService
30 let documentService: DocumentService
31 let fixture: ComponentFixture<StoragePathEditDialogComponent>
32
33 beforeEach(async () => {
34 TestBed.configureTestingModule({
35 declarations: [
36 StoragePathEditDialogComponent,
37 IfPermissionsDirective,
38 IfOwnerDirective,
39 SelectComponent,
40 TextComponent,
41 TextAreaComponent,
42 PermissionsFormComponent,
43 SafeHtmlPipe,
44 ],
45 imports: [FormsModule, ReactiveFormsModule, NgSelectModule, NgbModule],
46 providers: [
47 NgbActiveModal,
48 provideHttpClient(withInterceptorsFromDi()),
49 provideHttpClientTesting(),
50 ],
51 }).compileComponents()
52
53 documentService = TestBed.inject(DocumentService)
54 fixture = TestBed.createComponent(StoragePathEditDialogComponent)
55 settingsService = TestBed.inject(SettingsService)
56 settingsService.currentUser = { id: 99, username: 'user99' }
57 component = fixture.componentInstance
58
59 fixture.detectChanges()
60 })
61
62 it('should support create and edit modes', () => {
63 component.dialogMode = EditDialogMode.CREATE
64 const createTitleSpy = jest.spyOn(component, 'getCreateTitle')
65 const editTitleSpy = jest.spyOn(component, 'getEditTitle')
66 fixture.detectChanges()
67 expect(createTitleSpy).toHaveBeenCalled()
68 expect(editTitleSpy).not.toHaveBeenCalled()
69 component.dialogMode = EditDialogMode.EDIT
70 fixture.detectChanges()
71 expect(editTitleSpy).toHaveBeenCalled()
72 })
73
74 it('should support test path', () => {
75 const testSpy = jest.spyOn(
76 component['service'] as StoragePathService,
77 'testPath'
78 )
79 testSpy.mockReturnValueOnce(of('test/abc123'))
80 component.objectForm.patchValue({ path: 'test/{{title}}' })
81 fixture.detectChanges()
82 component.testPath({ id: 1 })
83 expect(testSpy).toHaveBeenCalledWith('test/{{title}}', 1)
84 expect(component.testResult).toBe('test/abc123')
85 expect(component.testFailed).toBeFalsy()
86
87 // test failed
88 testSpy.mockReturnValueOnce(of(''))
89 component.testPath({ id: 1 })
90 expect(component.testResult).toBeNull()
91 expect(component.testFailed).toBeTruthy()
92
93 component.testPath(null)
94 expect(component.testResult).toBeNull()
95 })
96
97 it('should compare two documents by id', () => {
98 const doc1 = { id: 1 }
99 const doc2 = { id: 2 }
100 expect(component.compareDocuments(doc1, doc1)).toBeTruthy()
101 expect(component.compareDocuments(doc1, doc2)).toBeFalsy()
102 })
103
104 it('should use id as trackBy', () => {
105 expect(component.trackByFn({ id: 1 })).toBe(1)
106 })
107
108 it('should search on select text input', () => {
109 fixture.debugElement
110 .query(By.directive(NgbAccordionButton))
111 .triggerEventHandler('click', null)
112 fixture.detectChanges()
113 const documents = [
114 { id: 1, title: 'foo' },
115 { id: 2, title: 'bar' },
116 ]
117 const listSpy = jest.spyOn(documentService, 'listFiltered')
118 listSpy.mockReturnValueOnce(
119 of({
120 count: 1,
121 results: documents[0],
122 all: [1],
123 } as any)
124 )
125 component.documentsInput$.next('bar')
126 expect(listSpy).toHaveBeenCalledWith(
127 1,
128 null,
129 'created',
130 true,
131 [{ rule_type: FILTER_TITLE, value: 'bar' }],
132 { truncate_content: true }
133 )
134 listSpy.mockReturnValueOnce(
135 of({
136 count: 2,
137 results: [...documents],
138 all: [1, 2],
139 } as any)
140 )
141 component.documentsInput$.next('ba')
142 listSpy.mockReturnValueOnce(throwError(() => new Error()))
143 component.documentsInput$.next('foo')
144 })
145
146 it('should run path test on path change', () => {
147 const testSpy = jest.spyOn(component, 'testPath')
148 component['testDocument'] = { id: 1 } as any
149 component.objectForm.patchValue(
150 { path: 'test/{{title}}' },
151 { emitEvent: true }
152 )
153 fixture.detectChanges()
154 expect(testSpy).toHaveBeenCalled()
155 })
156 })