paperless-ngx/src-ui/src/app/components/document-notes/document-notes.component.spec.ts
shamoon 181673c9a3 frontend unit tests
toasts component testing

conditional import of angular setup-jest for vscode-jest support

Update jest.config.js

Create open-documents.service.spec.ts

Add unit tests for all REST services

settings service test

Remove component from settings service test

Create permissions.service.spec.ts

upload documents service tests

Update package.json

Create toast.service.spec.ts

Tasks service test

Statistics widget component tests

Update permissions.service.ts

Create app.component.spec.ts

settings component testing

tasks component unit testing

Management list component generic tests

Some management component tests

document notes component unit tests

Create document-list.component.spec.ts

Create save-view-config-dialog.component.spec.ts

Create filter-editor.component.spec.ts

small and large document cards unit testing

Create bulk-editor.component.spec.ts

document detail unit tests

saving work on documentdetail component spec

Create document-asn.component.spec.ts

dashboard & widgets unit testing

Fix ResizeObserver mock

common component unit tests

fix some merge errors

Update app-frame.component.spec.ts

Create page-header.component.spec.ts

input component unit tests

FilterableDropdownComponent unit testing

and found minor errors

update taskservice unit tests

Edit dialogs unit tests

Create date-dropdown.component.spec.ts

Remove selectors from guard tests

confirm dialog component tests

app frame component test

Miscellaneous component tests

Update document-list-view.service.spec.ts

directives unit tests

Remove unused resizeobserver mock

guard unit tests

Update query-params.spec.ts

try to fix flaky playwright

filter rules utils & testing

Interceptor unit tests

Pipes unit testing

Utils unit tests

Update upload-documents.service.spec.ts

consumer status service tests

Update setup-jest.ts

Create document-list-view.service.spec.ts

Update app-routing.module.ts
2023-06-15 23:53:04 -07:00

188 lines
6.2 KiB
TypeScript

import { ComponentFixture, TestBed } from '@angular/core/testing'
import { environment } from 'src/environments/environment'
import { DocumentNotesComponent } from './document-notes.component'
import { UserService } from 'src/app/services/rest/user.service'
import { of, throwError } from 'rxjs'
import { DocumentNotesService } from 'src/app/services/rest/document-notes.service'
import { ToastService } from 'src/app/services/toast.service'
import { PaperlessDocumentNote } from 'src/app/data/paperless-document-note'
import { HttpClientTestingModule } from '@angular/common/http/testing'
import { CustomDatePipe } from 'src/app/pipes/custom-date.pipe'
import { IfPermissionsDirective } from 'src/app/directives/if-permissions.directive'
import { DatePipe } from '@angular/common'
import { By } from '@angular/platform-browser'
import { PermissionsService } from 'src/app/services/permissions.service'
import { FormsModule, ReactiveFormsModule } from '@angular/forms'
const notes: PaperlessDocumentNote[] = [
{
id: 23,
note: 'Note 23',
user: 1,
},
{
id: 24,
note: 'Note 24',
user: 1,
},
{
id: 25,
note: 'Note 25',
user: 2,
},
{
id: 30,
note: 'Note 30',
user: 3,
},
]
describe('DocumentNotesComponent', () => {
let component: DocumentNotesComponent
let fixture: ComponentFixture<DocumentNotesComponent>
let notesService: DocumentNotesService
let toastService: ToastService
beforeEach(async () => {
TestBed.configureTestingModule({
declarations: [
DocumentNotesComponent,
CustomDatePipe,
IfPermissionsDirective,
],
providers: [
{
provide: UserService,
useValue: {
listAll: () =>
of({
results: [
{
id: 1,
username: 'user1',
first_name: 'User1',
last_name: 'Lastname1',
},
{
id: 2,
username: 'user2',
},
{
id: 3,
username: 'user3',
},
],
}),
},
},
{
provide: PermissionsService,
useValue: {
currentUserCan: () => true,
},
},
CustomDatePipe,
DatePipe,
],
imports: [HttpClientTestingModule, FormsModule, ReactiveFormsModule],
}).compileComponents()
notesService = TestBed.inject(DocumentNotesService)
toastService = TestBed.inject(ToastService)
fixture = TestBed.createComponent(DocumentNotesComponent)
component = fixture.componentInstance
fixture.detectChanges()
})
it('should display notes with user name / username', () => {
component.notes = notes
fixture.detectChanges()
expect(fixture.debugElement.nativeElement.textContent).toContain(
notes[0].note
)
expect(fixture.debugElement.nativeElement.textContent).toContain(
notes[1].note
)
expect(fixture.debugElement.nativeElement.textContent).toContain(
notes[2].note
)
expect(fixture.debugElement.nativeElement.textContent).toContain(
notes[3].note
)
expect(fixture.debugElement.nativeElement.textContent).toContain(
'User1 Lastname1'
)
expect(fixture.debugElement.nativeElement.textContent).toContain('user2')
expect(fixture.debugElement.nativeElement.textContent).toContain('user3')
})
it('should handle note user display in all situations', () => {
expect(component.displayName({ id: 1, user: 1 })).toEqual(
'User1 Lastname1 (user1)'
)
expect(component.displayName({ id: 1, user: 2 })).toEqual('user2')
expect(component.displayName({ id: 1, user: 4 })).toEqual('')
expect(component.displayName({ id: 1 })).toEqual('')
})
it('should support note entry, show error if fails', () => {
component.documentId = 12
const note = 'This is the new note.'
const noteTextArea = fixture.debugElement.query(By.css('textarea'))
noteTextArea.nativeElement.value = note
noteTextArea.nativeElement.dispatchEvent(new Event('input'))
fixture.detectChanges()
const addSpy = jest.spyOn(notesService, 'addNote')
addSpy.mockReturnValueOnce(throwError(() => new Error('error saving note')))
const toastsSpy = jest.spyOn(toastService, 'showError')
const addButton = fixture.debugElement.query(By.css('button'))
addButton.triggerEventHandler('click')
expect(addSpy).toHaveBeenCalledWith(12, note)
expect(toastsSpy).toHaveBeenCalled()
addSpy.mockReturnValueOnce(of([...notes, { id: 31, note, user: 1 }]))
addButton.triggerEventHandler('click')
fixture.detectChanges()
expect(fixture.debugElement.nativeElement.textContent).toContain(note)
})
it('should support note save on ctrl+Enter', () => {
component.documentId = 12
const note = 'This is the new note.'
const noteTextArea = fixture.debugElement.query(By.css('textarea'))
noteTextArea.nativeElement.value = note
const addSpy = jest.spyOn(component, 'addNote')
noteTextArea.nativeElement.dispatchEvent(
new KeyboardEvent('keydown', { key: 'Enter', ctrlKey: true })
)
expect(addSpy).toHaveBeenCalled()
})
it('should support delete note, show error if fails', () => {
component.documentId = 12
component.notes = notes
fixture.detectChanges()
const deleteButton = fixture.debugElement.queryAll(By.css('button'))[1] // 0 is add button
const deleteSpy = jest.spyOn(notesService, 'deleteNote')
const toastsSpy = jest.spyOn(toastService, 'showError')
deleteSpy.mockReturnValueOnce(
throwError(() => new Error('error deleting note'))
)
deleteButton.triggerEventHandler('click')
expect(deleteSpy).toHaveBeenCalledWith(12, notes[0].id)
expect(toastsSpy).toHaveBeenCalled()
fixture.detectChanges()
expect(fixture.debugElement.nativeElement.textContent).toContain(
notes[0].note
)
deleteSpy.mockReturnValueOnce(of(notes.slice(1, 2)))
deleteButton.triggerEventHandler('click')
expect(deleteSpy).toHaveBeenCalledWith(12, notes[0].id)
fixture.detectChanges()
expect(fixture.debugElement.nativeElement.textContent).not.toContain(
notes[0].note
)
})
})