paperless-ngx/src-ui/src/app/services/open-documents.service.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

225 lines
6.9 KiB
TypeScript

import { TestBed } from '@angular/core/testing'
import { OpenDocumentsService } from './open-documents.service'
import {
HttpClientTestingModule,
HttpTestingController,
} from '@angular/common/http/testing'
import { environment } from 'src/environments/environment'
import { Subscription } from 'rxjs'
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { ConfirmDialogComponent } from '../components/common/confirm-dialog/confirm-dialog.component'
import { OPEN_DOCUMENT_SERVICE } from '../data/storage-keys'
const documents = [
{
id: 1,
title: 'Doc 1',
content: 'some content',
tags: [1, 2, 3],
correspondent: 11,
document_type: 3,
storage_path: 8,
},
{
id: 2,
title: 'Doc 2',
content: 'some content',
},
{
id: 3,
title: 'Doc 3',
content: 'some content',
},
{
id: 4,
title: 'Doc 4',
content: 'some content',
},
{
id: 5,
title: 'Doc 5',
content: 'some content',
},
{
id: 6,
title: 'Doc 6',
content: 'some content',
},
]
describe('OpenDocumentsService', () => {
let httpTestingController: HttpTestingController
let openDocumentsService: OpenDocumentsService
let modalService: NgbModal
let subscriptions: Subscription[] = []
beforeEach(() => {
TestBed.configureTestingModule({
providers: [OpenDocumentsService, NgbModal],
imports: [HttpClientTestingModule],
declarations: [ConfirmDialogComponent],
})
sessionStorage.clear()
httpTestingController = TestBed.inject(HttpTestingController)
openDocumentsService = TestBed.inject(OpenDocumentsService)
modalService = TestBed.inject(NgbModal)
})
afterEach(() => {
httpTestingController.verify()
})
afterAll(() => {
subscriptions?.forEach((subscription) => {
subscription.unsubscribe()
})
})
it('should open documents', () => {
subscriptions.push(
openDocumentsService.openDocument(documents[0]).subscribe()
)
expect(openDocumentsService.getOpenDocuments()).toHaveLength(1)
const doc = openDocumentsService.getOpenDocument(documents[0].id)
expect(doc.id).toEqual(documents[0].id)
})
it('should limit number of open documents', () => {
subscriptions.push(
openDocumentsService.openDocument(documents[0]).subscribe()
)
subscriptions.push(
openDocumentsService.openDocument(documents[1]).subscribe()
)
subscriptions.push(
openDocumentsService.openDocument(documents[2]).subscribe()
)
subscriptions.push(
openDocumentsService.openDocument(documents[3]).subscribe()
)
subscriptions.push(
openDocumentsService.openDocument(documents[4]).subscribe()
)
subscriptions.push(
openDocumentsService.openDocument(documents[5]).subscribe()
)
expect(openDocumentsService.getOpenDocuments()).toHaveLength(5)
})
it('should close documents', () => {
subscriptions.push(
openDocumentsService.openDocument(documents[0]).subscribe()
)
expect(openDocumentsService.getOpenDocuments()).toHaveLength(1)
openDocumentsService.closeDocument(documents[0])
expect(openDocumentsService.getOpenDocuments()).toHaveLength(0)
subscriptions.push(
openDocumentsService.openDocument(documents[0]).subscribe()
)
subscriptions.push(
openDocumentsService.openDocument(documents[1]).subscribe()
)
expect(openDocumentsService.getOpenDocuments()).toHaveLength(2)
subscriptions.push(openDocumentsService.closeAll().subscribe())
})
it('should allow set dirty status, warn on close', () => {
subscriptions.push(
openDocumentsService.openDocument(documents[0]).subscribe()
)
openDocumentsService.setDirty(documents[0], false)
expect(openDocumentsService.hasDirty()).toBeFalsy()
openDocumentsService.setDirty(documents[0], true)
expect(openDocumentsService.hasDirty()).toBeTruthy()
const modalSpy = jest.spyOn(modalService, 'open')
subscriptions.push(
openDocumentsService.closeDocument(documents[0]).subscribe()
)
expect(modalSpy).toHaveBeenCalled()
})
it('should allow set dirty status, warn on closeAll', () => {
subscriptions.push(
openDocumentsService.openDocument(documents[0]).subscribe()
)
subscriptions.push(
openDocumentsService.openDocument(documents[1]).subscribe()
)
openDocumentsService.setDirty(documents[0], true)
expect(openDocumentsService.hasDirty()).toBeTruthy()
const modalSpy = jest.spyOn(modalService, 'open')
subscriptions.push(openDocumentsService.closeAll().subscribe())
expect(modalSpy).toHaveBeenCalled()
})
it('should load open documents from localStorage', () => {
sessionStorage.setItem(
OPEN_DOCUMENT_SERVICE.DOCUMENTS,
JSON.stringify(documents)
)
const testOpenDocumentsService = new OpenDocumentsService(
null,
modalService
)
expect(testOpenDocumentsService.getOpenDocuments()).toHaveLength(
documents.length
)
})
it('should remove open documents from localStorage on error', () => {
sessionStorage.setItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS, 'hello world')
const testOpenDocumentsService = new OpenDocumentsService(
null,
modalService
)
expect(testOpenDocumentsService.getOpenDocuments()).toHaveLength(0)
expect(sessionStorage.getItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS)).toBeNull()
})
it('should save open documents to localStorage', () => {
subscriptions.push(
openDocumentsService.openDocument(documents[0]).subscribe()
)
subscriptions.push(
openDocumentsService.openDocument(documents[1]).subscribe()
)
subscriptions.push(
openDocumentsService.openDocument(documents[2]).subscribe()
)
openDocumentsService.save()
const localStorageDocs = JSON.parse(
sessionStorage.getItem(OPEN_DOCUMENT_SERVICE.DOCUMENTS)
)
expect(localStorageDocs).toContainEqual(documents[0])
expect(localStorageDocs).toContainEqual(documents[1])
expect(localStorageDocs).toContainEqual(documents[2])
})
it('should refresh documents', () => {
subscriptions.push(
openDocumentsService.openDocument(documents[1]).subscribe()
)
openDocumentsService.refreshDocument(documents[1].id)
const req = httpTestingController.expectOne(
`${environment.apiBaseUrl}documents/${documents[1].id}/?full_perms=true`
)
expect(req.request.method).toEqual('GET')
req.flush(documents[1])
expect(openDocumentsService.getOpenDocuments()).toHaveLength(1)
})
it('should handle error on refresh documents', () => {
subscriptions.push(
openDocumentsService.openDocument(documents[1]).subscribe()
)
openDocumentsService.refreshDocument(documents[1].id)
const req = httpTestingController.expectOne(
`${environment.apiBaseUrl}documents/${documents[1].id}/?full_perms=true`
)
expect(req.request.method).toEqual('GET')
req.error(new ErrorEvent('timeout'))
expect(openDocumentsService.getOpenDocuments()).toHaveLength(0)
})
})