]> git.ipfire.org Git - thirdparty/paperless-ngx.git/blob
31222441540db4ccdb53755e23cce1fac28cf792
[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 { NgbActiveModal, NgbModule } from '@ng-bootstrap/ng-bootstrap'
6 import { NgSelectModule } from '@ng-select/ng-select'
7 import { of } from 'rxjs'
8 import {
9 MailAction,
10 MailMetadataCorrespondentOption,
11 } from 'src/app/data/mail-rule'
12 import { IfOwnerDirective } from 'src/app/directives/if-owner.directive'
13 import { IfPermissionsDirective } from 'src/app/directives/if-permissions.directive'
14 import { SafeHtmlPipe } from 'src/app/pipes/safehtml.pipe'
15 import { CorrespondentService } from 'src/app/services/rest/correspondent.service'
16 import { DocumentTypeService } from 'src/app/services/rest/document-type.service'
17 import { MailAccountService } from 'src/app/services/rest/mail-account.service'
18 import { SettingsService } from 'src/app/services/settings.service'
19 import { CheckComponent } from '../../input/check/check.component'
20 import { NumberComponent } from '../../input/number/number.component'
21 import { PermissionsFormComponent } from '../../input/permissions/permissions-form/permissions-form.component'
22 import { SelectComponent } from '../../input/select/select.component'
23 import { SwitchComponent } from '../../input/switch/switch.component'
24 import { TagsComponent } from '../../input/tags/tags.component'
25 import { TextComponent } from '../../input/text/text.component'
26 import { EditDialogMode } from '../edit-dialog.component'
27 import { MailRuleEditDialogComponent } from './mail-rule-edit-dialog.component'
28
29 describe('MailRuleEditDialogComponent', () => {
30 let component: MailRuleEditDialogComponent
31 let settingsService: SettingsService
32 let fixture: ComponentFixture<MailRuleEditDialogComponent>
33
34 beforeEach(async () => {
35 TestBed.configureTestingModule({
36 declarations: [
37 MailRuleEditDialogComponent,
38 IfPermissionsDirective,
39 IfOwnerDirective,
40 SelectComponent,
41 TextComponent,
42 PermissionsFormComponent,
43 NumberComponent,
44 TagsComponent,
45 SafeHtmlPipe,
46 CheckComponent,
47 SwitchComponent,
48 ],
49 imports: [FormsModule, ReactiveFormsModule, NgSelectModule, NgbModule],
50 providers: [
51 NgbActiveModal,
52 {
53 provide: MailAccountService,
54 useValue: {
55 listAll: () => of([]),
56 },
57 },
58 {
59 provide: CorrespondentService,
60 useValue: {
61 listAll: () => of([]),
62 },
63 },
64 {
65 provide: DocumentTypeService,
66 useValue: {
67 listAll: () => of([]),
68 },
69 },
70 provideHttpClient(withInterceptorsFromDi()),
71 provideHttpClientTesting(),
72 ],
73 }).compileComponents()
74
75 fixture = TestBed.createComponent(MailRuleEditDialogComponent)
76 settingsService = TestBed.inject(SettingsService)
77 settingsService.currentUser = { id: 99, username: 'user99' }
78 component = fixture.componentInstance
79
80 fixture.detectChanges()
81 })
82
83 it('should support create and edit modes', () => {
84 component.dialogMode = EditDialogMode.CREATE
85 const createTitleSpy = jest.spyOn(component, 'getCreateTitle')
86 const editTitleSpy = jest.spyOn(component, 'getEditTitle')
87 fixture.detectChanges()
88 expect(createTitleSpy).toHaveBeenCalled()
89 expect(editTitleSpy).not.toHaveBeenCalled()
90 component.dialogMode = EditDialogMode.EDIT
91 fixture.detectChanges()
92 expect(editTitleSpy).toHaveBeenCalled()
93 })
94
95 it('should support optional fields', () => {
96 expect(component.showCorrespondentField).toBeFalsy()
97 component.objectForm
98 .get('assign_correspondent_from')
99 .setValue(MailMetadataCorrespondentOption.FromCustom)
100 expect(component.showCorrespondentField).toBeTruthy()
101
102 expect(component.showActionParamField).toBeFalsy()
103 component.objectForm.get('action').setValue(MailAction.Move)
104 expect(component.showActionParamField).toBeTruthy()
105 component.objectForm.get('action').setValue('')
106 expect(component.showActionParamField).toBeFalsy()
107 component.objectForm.get('action').setValue(MailAction.Tag)
108 expect(component.showActionParamField).toBeTruthy()
109
110 // coverage of optional chaining
111 component.objectForm = null
112 expect(component.showCorrespondentField).toBeFalsy()
113 expect(component.showActionParamField).toBeFalsy()
114 })
115 })