frontend tests

This commit is contained in:
shamoon
2026-02-10 20:08:17 -08:00
parent d36a64d3fe
commit ddbf9982a5
2 changed files with 126 additions and 0 deletions

View File

@@ -354,6 +354,88 @@ describe('DocumentDetailComponent', () => {
expect(component.document).toEqual(doc)
})
it('should redirect to root when opening a version document id', () => {
const navigateSpy = jest.spyOn(router, 'navigate')
jest
.spyOn(activatedRoute, 'paramMap', 'get')
.mockReturnValue(of(convertToParamMap({ id: 10, section: 'details' })))
jest
.spyOn(documentService, 'get')
.mockReturnValueOnce(throwError(() => ({ status: 404 }) as any))
const getRootSpy = jest
.spyOn(documentService, 'getRootId')
.mockReturnValue(of({ root_id: 3 }))
jest.spyOn(openDocumentsService, 'getOpenDocument').mockReturnValue(null)
jest
.spyOn(openDocumentsService, 'openDocument')
.mockReturnValueOnce(of(true))
jest.spyOn(customFieldsService, 'listAll').mockReturnValue(
of({
count: customFields.length,
all: customFields.map((f) => f.id),
results: customFields,
})
)
fixture.detectChanges()
httpTestingController.expectOne(component.previewUrl).flush('preview')
expect(getRootSpy).toHaveBeenCalledWith(10)
expect(navigateSpy).toHaveBeenCalledWith(['documents', 3, 'details'], {
replaceUrl: true,
})
})
it('should not render a delete button for the root/original version', () => {
const docWithVersions = {
...doc,
versions: [
{
id: doc.id,
added: new Date('2024-01-01T00:00:00Z'),
label: 'Original',
checksum: 'aaaa',
is_root: true,
},
{
id: 10,
added: new Date('2024-01-02T00:00:00Z'),
label: 'Edited',
checksum: 'bbbb',
is_root: false,
},
],
} as Document
jest
.spyOn(activatedRoute, 'paramMap', 'get')
.mockReturnValue(of(convertToParamMap({ id: 3, section: 'details' })))
jest.spyOn(documentService, 'get').mockReturnValueOnce(of(docWithVersions))
jest
.spyOn(documentService, 'getMetadata')
.mockReturnValue(of({ has_archive_version: true } as any))
jest.spyOn(openDocumentsService, 'getOpenDocument').mockReturnValue(null)
jest
.spyOn(openDocumentsService, 'openDocument')
.mockReturnValueOnce(of(true))
jest.spyOn(customFieldsService, 'listAll').mockReturnValue(
of({
count: customFields.length,
all: customFields.map((f) => f.id),
results: customFields,
})
)
fixture.detectChanges()
httpTestingController.expectOne(component.previewUrl).flush('preview')
fixture.detectChanges()
const deleteButtons = fixture.debugElement.queryAll(
By.css('pngx-confirm-button')
)
expect(deleteButtons.length).toEqual(1)
})
it('should fall back to details tab when duplicates tab is active but no duplicates', () => {
initNormally()
component.activeNavID = component.DocumentDetailNavIDs.Duplicates

View File

@@ -233,6 +233,13 @@ describe(`DocumentService`, () => {
)
})
it('should return the correct preview URL for a specific version', () => {
const url = service.getPreviewUrl(documents[0].id, false, 123)
expect(url).toEqual(
`${environment.apiBaseUrl}${endpoint}/${documents[0].id}/preview/?version=123`
)
})
it('should return the correct thumb URL for a single document', () => {
let url = service.getThumbUrl(documents[0].id)
expect(url).toEqual(
@@ -289,6 +296,43 @@ describe(`DocumentService`, () => {
`${environment.apiBaseUrl}${endpoint}/${documents[0].id}/history/`
)
})
it('should call appropriate api endpoint for getting root document id', () => {
subscription = service.getRootId(documents[0].id).subscribe()
const req = httpTestingController.expectOne(
`${environment.apiBaseUrl}${endpoint}/${documents[0].id}/root/`
)
expect(req.request.method).toEqual('GET')
req.flush({ root_id: documents[0].id })
})
it('should call appropriate api endpoint for deleting a document version', () => {
subscription = service.deleteVersion(documents[0].id, 10).subscribe()
const req = httpTestingController.expectOne(
`${environment.apiBaseUrl}${endpoint}/${documents[0].id}/versions/10/`
)
expect(req.request.method).toEqual('DELETE')
req.flush({ result: 'OK', current_version_id: documents[0].id })
})
it('should call appropriate api endpoint for uploading a new version', () => {
const file = new File(['hello'], 'test.pdf', { type: 'application/pdf' })
subscription = service
.uploadVersion(documents[0].id, file, 'Label')
.subscribe()
const req = httpTestingController.expectOne(
`${environment.apiBaseUrl}${endpoint}/${documents[0].id}/update_version/`
)
expect(req.request.method).toEqual('POST')
expect(req.request.body).toBeInstanceOf(FormData)
const body = req.request.body as FormData
expect(body.get('label')).toEqual('Label')
expect(body.get('document')).toBeInstanceOf(File)
req.flush('task-id')
})
})
it('should construct sort fields respecting permissions', () => {