mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-28 18:24:38 -05:00
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
This commit is contained in:
48
src-ui/src/app/guards/dirty-doc.guard.spec.ts
Normal file
48
src-ui/src/app/guards/dirty-doc.guard.spec.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { TestBed } from '@angular/core/testing'
|
||||
import { NgbModal, NgbModule } from '@ng-bootstrap/ng-bootstrap'
|
||||
import { RouterTestingModule } from '@angular/router/testing'
|
||||
import { routes } from '../app-routing.module'
|
||||
import { Component } from '@angular/core'
|
||||
import { ComponentCanDeactivate, DirtyDocGuard } from './dirty-doc.guard'
|
||||
|
||||
@Component({})
|
||||
class GenericDirtyDocComponent implements ComponentCanDeactivate {
|
||||
canDeactivate: () => boolean
|
||||
}
|
||||
|
||||
describe('DirtyDocGuard', () => {
|
||||
let guard: DirtyDocGuard
|
||||
let component: ComponentCanDeactivate
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [DirtyDocGuard, NgbModal, GenericDirtyDocComponent],
|
||||
imports: [RouterTestingModule.withRoutes(routes), NgbModule],
|
||||
declarations: [GenericDirtyDocComponent],
|
||||
}).compileComponents()
|
||||
|
||||
guard = TestBed.inject(DirtyDocGuard)
|
||||
const fixture = TestBed.createComponent(GenericDirtyDocComponent)
|
||||
component = fixture.componentInstance
|
||||
window.confirm = jest.fn().mockImplementation(() => true)
|
||||
|
||||
fixture.detectChanges()
|
||||
})
|
||||
|
||||
it('should deactivate if component is not dirty', () => {
|
||||
component.canDeactivate = () => true
|
||||
const confirmSpy = jest.spyOn(window, 'confirm')
|
||||
const canDeactivate = guard.canDeactivate(component)
|
||||
|
||||
expect(canDeactivate).toBeTruthy()
|
||||
expect(confirmSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should not deactivate if component is dirty', () => {
|
||||
component.canDeactivate = () => false
|
||||
const confirmSpy = jest.spyOn(window, 'confirm')
|
||||
const canDeactivate = guard.canDeactivate(component)
|
||||
|
||||
expect(confirmSpy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
65
src-ui/src/app/guards/dirty-form.guard.spec.ts
Normal file
65
src-ui/src/app/guards/dirty-form.guard.spec.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { TestBed } from '@angular/core/testing'
|
||||
import { NgbModal, NgbModule } from '@ng-bootstrap/ng-bootstrap'
|
||||
import { RouterTestingModule } from '@angular/router/testing'
|
||||
import { routes } from '../app-routing.module'
|
||||
import { ConfirmDialogComponent } from '../components/common/confirm-dialog/confirm-dialog.component'
|
||||
import { DirtyFormGuard } from './dirty-form.guard'
|
||||
import { DirtyComponent } from '@ngneat/dirty-check-forms'
|
||||
import { ActivatedRoute } from '@angular/router'
|
||||
import { Component } from '@angular/core'
|
||||
|
||||
@Component({})
|
||||
class GenericDirtyComponent implements DirtyComponent {
|
||||
isDirty$: boolean
|
||||
}
|
||||
|
||||
describe('DirtyFormGuard', () => {
|
||||
let guard: DirtyFormGuard
|
||||
let component: DirtyComponent
|
||||
let route: ActivatedRoute
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
DirtyFormGuard,
|
||||
NgbModal,
|
||||
{
|
||||
provide: ActivatedRoute,
|
||||
useValue: {
|
||||
snapshot: {},
|
||||
},
|
||||
},
|
||||
GenericDirtyComponent,
|
||||
],
|
||||
imports: [RouterTestingModule.withRoutes(routes), NgbModule],
|
||||
declarations: [ConfirmDialogComponent, GenericDirtyComponent],
|
||||
}).compileComponents()
|
||||
|
||||
guard = TestBed.inject(DirtyFormGuard)
|
||||
route = TestBed.inject(ActivatedRoute)
|
||||
const fixture = TestBed.createComponent(GenericDirtyComponent)
|
||||
component = fixture.componentInstance
|
||||
|
||||
fixture.detectChanges()
|
||||
})
|
||||
|
||||
it('should deactivate if component is not dirty', () => {
|
||||
component.isDirty$ = false
|
||||
const confirmSpy = jest.spyOn(guard, 'confirmChanges')
|
||||
const canDeactivate = guard.canDeactivate(component, route.snapshot)
|
||||
canDeactivate.subscribe()
|
||||
|
||||
expect(canDeactivate).toBeTruthy()
|
||||
expect(confirmSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should offer confirm before deactivate if component is dirty', () => {
|
||||
component.isDirty$ = true
|
||||
const confirmSpy = jest.spyOn(guard, 'confirmChanges')
|
||||
const canDeactivate = guard.canDeactivate(component, route.snapshot)
|
||||
canDeactivate.subscribe()
|
||||
|
||||
expect(canDeactivate).toHaveProperty('source') // Observable
|
||||
expect(confirmSpy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
131
src-ui/src/app/guards/dirty-saved-view.guard.spec.ts
Normal file
131
src-ui/src/app/guards/dirty-saved-view.guard.spec.ts
Normal file
@@ -0,0 +1,131 @@
|
||||
import { TestBed } from '@angular/core/testing'
|
||||
import { DirtySavedViewGuard } from './dirty-saved-view.guard'
|
||||
import { NgbModal, NgbModalRef } from '@ng-bootstrap/ng-bootstrap'
|
||||
import { SettingsService } from '../services/settings.service'
|
||||
import { DocumentListComponent } from '../components/document-list/document-list.component'
|
||||
import { RouterTestingModule } from '@angular/router/testing'
|
||||
import { routes } from '../app-routing.module'
|
||||
import { HttpClientTestingModule } from '@angular/common/http/testing'
|
||||
import { ConfirmDialogComponent } from '../components/common/confirm-dialog/confirm-dialog.component'
|
||||
|
||||
describe('DirtySavedViewGuard', () => {
|
||||
let guard: DirtySavedViewGuard
|
||||
let settingsService: SettingsService
|
||||
let modalService: NgbModal
|
||||
let component: DocumentListComponent
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
DirtySavedViewGuard,
|
||||
SettingsService,
|
||||
NgbModal,
|
||||
DocumentListComponent,
|
||||
],
|
||||
imports: [
|
||||
RouterTestingModule.withRoutes(routes),
|
||||
HttpClientTestingModule,
|
||||
],
|
||||
declarations: [ConfirmDialogComponent],
|
||||
})
|
||||
|
||||
settingsService = TestBed.inject(SettingsService)
|
||||
modalService = TestBed.inject(NgbModal)
|
||||
guard = TestBed.inject(DirtySavedViewGuard)
|
||||
const fixture = TestBed.createComponent(DocumentListComponent)
|
||||
component = fixture.componentInstance
|
||||
})
|
||||
|
||||
it('should deactivate if component is not dirty', () => {
|
||||
jest
|
||||
.spyOn(DocumentListComponent.prototype, 'savedViewIsModified', 'get')
|
||||
.mockImplementation(() => {
|
||||
return false
|
||||
})
|
||||
const canDeactivate = guard.canDeactivate(component)
|
||||
|
||||
expect(canDeactivate).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should not warn on deactivate if component is dirty & setting disabled', () => {
|
||||
jest
|
||||
.spyOn(DocumentListComponent.prototype, 'savedViewIsModified', 'get')
|
||||
.mockImplementation(() => {
|
||||
return true
|
||||
})
|
||||
|
||||
jest.spyOn(settingsService, 'get').mockImplementation(() => {
|
||||
return false
|
||||
})
|
||||
|
||||
const modalSpy = jest.spyOn(modalService, 'open')
|
||||
|
||||
const canDeactivate = guard.canDeactivate(component)
|
||||
|
||||
expect(canDeactivate).toBeTruthy()
|
||||
expect(modalSpy).not.toHaveBeenCalled()
|
||||
|
||||
const saveSpy = jest.spyOn(component, 'saveViewConfig')
|
||||
expect(saveSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should warn on deactivate if component is dirty & setting enabled', () => {
|
||||
jest
|
||||
.spyOn(DocumentListComponent.prototype, 'savedViewIsModified', 'get')
|
||||
.mockImplementation(() => {
|
||||
return true
|
||||
})
|
||||
|
||||
jest.spyOn(settingsService, 'get').mockImplementation(() => {
|
||||
return true
|
||||
})
|
||||
|
||||
const modalSpy = jest.spyOn(modalService, 'open')
|
||||
|
||||
let modal: NgbModalRef
|
||||
|
||||
modalService.activeInstances.subscribe((ngbmodalRef) => {
|
||||
modal = ngbmodalRef[0]
|
||||
})
|
||||
|
||||
const canDeactivate = guard.canDeactivate(component)
|
||||
|
||||
expect(canDeactivate).toHaveProperty('closed') // returns confirm dialog subject
|
||||
expect(modalSpy).toHaveBeenCalled()
|
||||
expect(modal).not.toBeNull()
|
||||
|
||||
const saveSpy = jest.spyOn(component, 'saveViewConfig')
|
||||
modal.componentInstance.alternativeClicked.emit()
|
||||
expect(saveSpy).toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('should not save if user proceeds on warn', () => {
|
||||
jest
|
||||
.spyOn(DocumentListComponent.prototype, 'savedViewIsModified', 'get')
|
||||
.mockImplementation(() => {
|
||||
return true
|
||||
})
|
||||
|
||||
jest.spyOn(settingsService, 'get').mockImplementation(() => {
|
||||
return true
|
||||
})
|
||||
|
||||
const modalSpy = jest.spyOn(modalService, 'open')
|
||||
|
||||
let modal: NgbModalRef
|
||||
|
||||
modalService.activeInstances.subscribe((ngbmodalRef) => {
|
||||
modal = ngbmodalRef[0]
|
||||
})
|
||||
|
||||
const canDeactivate = guard.canDeactivate(component)
|
||||
|
||||
expect(canDeactivate).toHaveProperty('closed') // returns confirm dialog subject
|
||||
expect(modalSpy).toHaveBeenCalled()
|
||||
expect(modal).not.toBeNull()
|
||||
|
||||
const saveSpy = jest.spyOn(component, 'saveViewConfig')
|
||||
modal.componentInstance.confirmClicked.emit()
|
||||
expect(saveSpy).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
100
src-ui/src/app/guards/permissions.guard.spec.ts
Normal file
100
src-ui/src/app/guards/permissions.guard.spec.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
import { TestBed } from '@angular/core/testing'
|
||||
import { PermissionsGuard } from './permissions.guard'
|
||||
import {
|
||||
PermissionAction,
|
||||
PermissionType,
|
||||
PermissionsService,
|
||||
} from '../services/permissions.service'
|
||||
import { ActivatedRoute } from '@angular/router'
|
||||
import { RouterStateSnapshot } from '@angular/router'
|
||||
import { TourService } from 'ngx-ui-tour-ng-bootstrap'
|
||||
import { ToastService } from '../services/toast.service'
|
||||
import { RouterState } from '@angular/router'
|
||||
|
||||
describe('PermissionsGuard', () => {
|
||||
let guard: PermissionsGuard
|
||||
let permissionsService: PermissionsService
|
||||
let route: ActivatedRoute
|
||||
let routerState: RouterState
|
||||
let tourService: TourService
|
||||
let toastService: ToastService
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
providers: [
|
||||
PermissionsGuard,
|
||||
PermissionsService,
|
||||
{
|
||||
provide: ActivatedRoute,
|
||||
useValue: {
|
||||
snapshot: {
|
||||
data: {
|
||||
requiredPermission: {
|
||||
action: PermissionAction.View,
|
||||
type: PermissionType.Document,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
provide: RouterState,
|
||||
useValue: {
|
||||
snapshot: {
|
||||
url: '/documents',
|
||||
},
|
||||
},
|
||||
},
|
||||
TourService,
|
||||
ToastService,
|
||||
],
|
||||
})
|
||||
|
||||
permissionsService = TestBed.inject(PermissionsService)
|
||||
tourService = TestBed.inject(TourService)
|
||||
toastService = TestBed.inject(ToastService)
|
||||
guard = TestBed.inject(PermissionsGuard)
|
||||
route = TestBed.inject(ActivatedRoute)
|
||||
routerState = TestBed.inject(RouterState)
|
||||
})
|
||||
|
||||
it('should activate if user has permissions', () => {
|
||||
jest
|
||||
.spyOn(permissionsService, 'currentUserCan')
|
||||
.mockImplementation((action, type) => {
|
||||
return true
|
||||
})
|
||||
|
||||
const canActivate = guard.canActivate(route.snapshot, routerState.snapshot)
|
||||
|
||||
expect(canActivate).toBeTruthy()
|
||||
})
|
||||
|
||||
it('should not activate if user does not have permissions', () => {
|
||||
jest
|
||||
.spyOn(permissionsService, 'currentUserCan')
|
||||
.mockImplementation((action, type) => {
|
||||
return false
|
||||
})
|
||||
|
||||
const canActivate = guard.canActivate(route.snapshot, routerState.snapshot)
|
||||
|
||||
expect(canActivate).toHaveProperty('root') // returns UrlTree
|
||||
})
|
||||
|
||||
it('should not activate if user does not have permissions and tour is running', () => {
|
||||
jest
|
||||
.spyOn(permissionsService, 'currentUserCan')
|
||||
.mockImplementation((action, type) => {
|
||||
return false
|
||||
})
|
||||
jest.spyOn(tourService, 'getStatus').mockImplementation(() => 2)
|
||||
|
||||
const toastSpy = jest.spyOn(toastService, 'showError')
|
||||
|
||||
const canActivate = guard.canActivate(route.snapshot, routerState.snapshot)
|
||||
|
||||
expect(canActivate).toHaveProperty('root') // returns UrlTree
|
||||
expect(toastSpy).toHaveBeenCalled()
|
||||
})
|
||||
})
|
Reference in New Issue
Block a user