mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-07-20 17:44:56 -05:00
137 lines
3.4 KiB
TypeScript
137 lines
3.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('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)
|
|
})
|
|
})
|