mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2026-11-05 14:29:22 +00:00
162 lines
4.4 KiB
TypeScript
162 lines
4.4 KiB
TypeScript
import {
|
|
HttpEventType,
|
|
provideHttpClient,
|
|
withInterceptorsFromDi,
|
|
} from '@angular/common/http'
|
|
import {
|
|
HttpTestingController,
|
|
provideHttpClientTesting,
|
|
} from '@angular/common/http/testing'
|
|
import { TestBed } from '@angular/core/testing'
|
|
import { environment } from 'src/environments/environment'
|
|
import { UploadDocumentsService } from './upload-documents.service'
|
|
import {
|
|
FileStatusPhase,
|
|
WebsocketStatusService,
|
|
} from './websocket-status.service'
|
|
|
|
describe('UploadDocumentsService', () => {
|
|
let httpTestingController: HttpTestingController
|
|
let uploadDocumentsService: UploadDocumentsService
|
|
let websocketStatusService: WebsocketStatusService
|
|
|
|
beforeEach(() => {
|
|
TestBed.configureTestingModule({
|
|
imports: [],
|
|
providers: [
|
|
UploadDocumentsService,
|
|
WebsocketStatusService,
|
|
provideHttpClient(withInterceptorsFromDi()),
|
|
provideHttpClientTesting(),
|
|
],
|
|
})
|
|
|
|
httpTestingController = TestBed.inject(HttpTestingController)
|
|
uploadDocumentsService = TestBed.inject(UploadDocumentsService)
|
|
websocketStatusService = TestBed.inject(WebsocketStatusService)
|
|
})
|
|
|
|
afterEach(() => {
|
|
httpTestingController.verify()
|
|
})
|
|
|
|
it('calls post_document api endpoint on upload', () => {
|
|
const file = new File(
|
|
[new Blob(['testing'], { type: 'application/pdf' })],
|
|
'file.pdf'
|
|
)
|
|
uploadDocumentsService.uploadFile(file)
|
|
const req = httpTestingController.match(
|
|
`${environment.apiBaseUrl}documents/post_document/`
|
|
)
|
|
expect(req[0].request.method).toEqual('POST')
|
|
|
|
req[0].flush('123-456')
|
|
})
|
|
|
|
it('uploads each file when given multiple files', () => {
|
|
const uploadSpy = jest.spyOn(uploadDocumentsService, 'uploadFile')
|
|
const file1 = new File(['one'], 'one.pdf')
|
|
const file2 = new File(['two'], 'two.pdf')
|
|
uploadDocumentsService.uploadFiles([file1, file2])
|
|
expect(uploadSpy).toHaveBeenCalledWith(file1)
|
|
expect(uploadSpy).toHaveBeenCalledWith(file2)
|
|
httpTestingController
|
|
.match(`${environment.apiBaseUrl}documents/post_document/`)
|
|
.forEach((req) => req.flush('123-456'))
|
|
})
|
|
|
|
it('emits when uploads start', () => {
|
|
const startedSpy = jest.fn()
|
|
uploadDocumentsService.onUploadsStarted().subscribe(startedSpy)
|
|
uploadDocumentsService.uploadFiles([])
|
|
expect(startedSpy).not.toHaveBeenCalled()
|
|
const file = new File(['one'], 'one.pdf')
|
|
uploadDocumentsService.uploadFiles([file])
|
|
expect(startedSpy).toHaveBeenCalledWith([file])
|
|
httpTestingController
|
|
.match(`${environment.apiBaseUrl}documents/post_document/`)
|
|
.forEach((req) => req.flush('123-456'))
|
|
})
|
|
|
|
it('updates progress during upload and failure', () => {
|
|
const file = new File(
|
|
[new Blob(['testing'], { type: 'application/pdf' })],
|
|
'file.pdf'
|
|
)
|
|
const file2 = new File(
|
|
[new Blob(['testing'], { type: 'application/pdf' })],
|
|
'file2.pdf'
|
|
)
|
|
uploadDocumentsService.uploadFile(file)
|
|
uploadDocumentsService.uploadFile(file2)
|
|
|
|
expect(websocketStatusService.getConsumerStatusNotCompleted()).toHaveLength(
|
|
2
|
|
)
|
|
expect(
|
|
websocketStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
|
).toHaveLength(0)
|
|
|
|
const req = httpTestingController.match(
|
|
`${environment.apiBaseUrl}documents/post_document/`
|
|
)
|
|
|
|
req[0].event({
|
|
type: HttpEventType.UploadProgress,
|
|
loaded: 100,
|
|
total: 300,
|
|
})
|
|
|
|
expect(
|
|
websocketStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
|
|
).toHaveLength(1)
|
|
})
|
|
|
|
it('updates progress on failure', () => {
|
|
const file = new File(
|
|
[new Blob(['testing'], { type: 'application/pdf' })],
|
|
'file.pdf'
|
|
)
|
|
uploadDocumentsService.uploadFile(file)
|
|
|
|
let req = httpTestingController.match(
|
|
`${environment.apiBaseUrl}documents/post_document/`
|
|
)
|
|
|
|
expect(
|
|
websocketStatusService.getConsumerStatus(FileStatusPhase.FAILED)
|
|
).toHaveLength(0)
|
|
|
|
req[0].flush(
|
|
{},
|
|
{
|
|
status: 400,
|
|
statusText: 'failed',
|
|
}
|
|
)
|
|
|
|
expect(
|
|
websocketStatusService.getConsumerStatus(FileStatusPhase.FAILED)
|
|
).toHaveLength(1)
|
|
|
|
uploadDocumentsService.uploadFile(file)
|
|
|
|
req = httpTestingController.match(
|
|
`${environment.apiBaseUrl}documents/post_document/`
|
|
)
|
|
|
|
req[0].flush(
|
|
{},
|
|
{
|
|
status: 500,
|
|
statusText: 'failed',
|
|
}
|
|
)
|
|
|
|
expect(
|
|
websocketStatusService.getConsumerStatus(FileStatusPhase.FAILED)
|
|
).toHaveLength(2)
|
|
})
|
|
})
|