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'
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'
27 describe('StoragePathEditDialogComponent', () => {
28 let component: StoragePathEditDialogComponent
29 let settingsService: SettingsService
30 let documentService: DocumentService
31 let fixture: ComponentFixture<StoragePathEditDialogComponent>
33 beforeEach(async () => {
34 TestBed.configureTestingModule({
36 StoragePathEditDialogComponent,
37 IfPermissionsDirective,
42 PermissionsFormComponent,
45 imports: [FormsModule, ReactiveFormsModule, NgSelectModule, NgbModule],
48 provideHttpClient(withInterceptorsFromDi()),
49 provideHttpClientTesting(),
51 }).compileComponents()
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
59 fixture.detectChanges()
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()
74 it('should support test path', () => {
75 const testSpy = jest.spyOn(
76 component['service'] as StoragePathService,
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()
88 testSpy.mockReturnValueOnce(of(''))
89 component.testPath({ id: 1 })
90 expect(component.testResult).toBeNull()
91 expect(component.testFailed).toBeTruthy()
93 component.testPath(null)
94 expect(component.testResult).toBeNull()
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()
104 it('should use id as trackBy', () => {
105 expect(component.trackByFn({ id: 1 })).toBe(1)
108 it('should search on select text input', () => {
110 .query(By.directive(NgbAccordionButton))
111 .triggerEventHandler('click', null)
112 fixture.detectChanges()
114 { id: 1, title: 'foo' },
115 { id: 2, title: 'bar' },
117 const listSpy = jest.spyOn(documentService, 'listFiltered')
118 listSpy.mockReturnValueOnce(
121 results: documents[0],
125 component.documentsInput$.next('bar')
126 expect(listSpy).toHaveBeenCalledWith(
131 [{ rule_type: FILTER_TITLE, value: 'bar' }],
132 { truncate_content: true }
134 listSpy.mockReturnValueOnce(
137 results: [...documents],
141 component.documentsInput$.next('ba')
142 listSpy.mockReturnValueOnce(throwError(() => new Error()))
143 component.documentsInput$.next('foo')
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}}' },
153 fixture.detectChanges()
154 expect(testSpy).toHaveBeenCalled()