mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-11-14 10:33:03 -05:00
Sure sonar, consolidate
This commit is contained in:
parent
8f969ecab5
commit
9f0a4ac19d
@ -1514,94 +1514,51 @@ describe('DocumentDetailComponent', () => {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should show error toast if printing throws inside iframe', fakeAsync(() => {
|
const iframePrintErrorCases: Array<{
|
||||||
initNormally()
|
description: string
|
||||||
|
thrownError: Error
|
||||||
const appendChildSpy = jest
|
expectToast: boolean
|
||||||
.spyOn(document.body, 'appendChild')
|
}> = [
|
||||||
.mockImplementation((node: Node) => node)
|
{
|
||||||
const removeChildSpy = jest
|
description: 'should show error toast if printing throws inside iframe',
|
||||||
.spyOn(document.body, 'removeChild')
|
thrownError: new Error('focus failed'),
|
||||||
.mockImplementation((node: Node) => node)
|
expectToast: true,
|
||||||
const createObjectURLSpy = jest
|
},
|
||||||
.spyOn(URL, 'createObjectURL')
|
{
|
||||||
.mockReturnValue('blob:mock-url')
|
description:
|
||||||
const revokeObjectURLSpy = jest
|
'should suppress toast if cross-origin afterprint error occurs',
|
||||||
.spyOn(URL, 'revokeObjectURL')
|
thrownError: new DOMException(
|
||||||
.mockImplementation(() => {})
|
|
||||||
|
|
||||||
const toastSpy = jest.spyOn(toastService, 'showError')
|
|
||||||
|
|
||||||
const mockContentWindow = {
|
|
||||||
focus: jest.fn().mockImplementation(() => {
|
|
||||||
throw new Error('focus failed')
|
|
||||||
}),
|
|
||||||
print: jest.fn(),
|
|
||||||
onafterprint: null,
|
|
||||||
}
|
|
||||||
|
|
||||||
const mockIframe: any = {
|
|
||||||
style: {},
|
|
||||||
src: '',
|
|
||||||
onload: null,
|
|
||||||
contentWindow: mockContentWindow,
|
|
||||||
}
|
|
||||||
|
|
||||||
const createElementSpy = jest
|
|
||||||
.spyOn(document, 'createElement')
|
|
||||||
.mockReturnValue(mockIframe as any)
|
|
||||||
|
|
||||||
const blob = new Blob(['test'], { type: 'application/pdf' })
|
|
||||||
component.printDocument()
|
|
||||||
|
|
||||||
const req = httpTestingController.expectOne(
|
|
||||||
`${environment.apiBaseUrl}documents/${doc.id}/download/`
|
|
||||||
)
|
|
||||||
req.flush(blob)
|
|
||||||
|
|
||||||
tick()
|
|
||||||
|
|
||||||
if (mockIframe.onload) {
|
|
||||||
mockIframe.onload(new Event('load'))
|
|
||||||
}
|
|
||||||
|
|
||||||
tick(500)
|
|
||||||
|
|
||||||
expect(toastSpy).toHaveBeenCalled()
|
|
||||||
expect(removeChildSpy).toHaveBeenCalledWith(mockIframe)
|
|
||||||
expect(revokeObjectURLSpy).toHaveBeenCalledWith('blob:mock-url')
|
|
||||||
|
|
||||||
createElementSpy.mockRestore()
|
|
||||||
appendChildSpy.mockRestore()
|
|
||||||
removeChildSpy.mockRestore()
|
|
||||||
createObjectURLSpy.mockRestore()
|
|
||||||
revokeObjectURLSpy.mockRestore()
|
|
||||||
}))
|
|
||||||
|
|
||||||
it('should suppress toast if cross-origin afterprint error occurs', fakeAsync(() => {
|
|
||||||
initNormally()
|
|
||||||
|
|
||||||
const appendChildSpy = jest
|
|
||||||
.spyOn(document.body, 'appendChild')
|
|
||||||
.mockImplementation((node: Node) => node)
|
|
||||||
const removeChildSpy = jest
|
|
||||||
.spyOn(document.body, 'removeChild')
|
|
||||||
.mockImplementation((node: Node) => node)
|
|
||||||
const createObjectURLSpy = jest
|
|
||||||
.spyOn(URL, 'createObjectURL')
|
|
||||||
.mockReturnValue('blob:mock-url')
|
|
||||||
const revokeObjectURLSpy = jest
|
|
||||||
.spyOn(URL, 'revokeObjectURL')
|
|
||||||
.mockImplementation(() => {})
|
|
||||||
|
|
||||||
const toastSpy = jest.spyOn(toastService, 'showError')
|
|
||||||
|
|
||||||
const mockContentWindow = {
|
|
||||||
focus: jest.fn().mockImplementation(() => {
|
|
||||||
throw new DOMException(
|
|
||||||
'Accessing onafterprint triggered a cross-origin violation',
|
'Accessing onafterprint triggered a cross-origin violation',
|
||||||
'SecurityError'
|
'SecurityError'
|
||||||
)
|
),
|
||||||
|
expectToast: false,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
iframePrintErrorCases.forEach(({ description, thrownError, expectToast }) => {
|
||||||
|
it(
|
||||||
|
description,
|
||||||
|
fakeAsync(() => {
|
||||||
|
initNormally()
|
||||||
|
|
||||||
|
const appendChildSpy = jest
|
||||||
|
.spyOn(document.body, 'appendChild')
|
||||||
|
.mockImplementation((node: Node) => node)
|
||||||
|
const removeChildSpy = jest
|
||||||
|
.spyOn(document.body, 'removeChild')
|
||||||
|
.mockImplementation((node: Node) => node)
|
||||||
|
const createObjectURLSpy = jest
|
||||||
|
.spyOn(URL, 'createObjectURL')
|
||||||
|
.mockReturnValue('blob:mock-url')
|
||||||
|
const revokeObjectURLSpy = jest
|
||||||
|
.spyOn(URL, 'revokeObjectURL')
|
||||||
|
.mockImplementation(() => {})
|
||||||
|
|
||||||
|
const toastSpy = jest.spyOn(toastService, 'showError')
|
||||||
|
|
||||||
|
const mockContentWindow = {
|
||||||
|
focus: jest.fn().mockImplementation(() => {
|
||||||
|
throw thrownError
|
||||||
}),
|
}),
|
||||||
print: jest.fn(),
|
print: jest.fn(),
|
||||||
onafterprint: null,
|
onafterprint: null,
|
||||||
@ -1634,7 +1591,11 @@ describe('DocumentDetailComponent', () => {
|
|||||||
|
|
||||||
tick(200)
|
tick(200)
|
||||||
|
|
||||||
|
if (expectToast) {
|
||||||
|
expect(toastSpy).toHaveBeenCalled()
|
||||||
|
} else {
|
||||||
expect(toastSpy).not.toHaveBeenCalled()
|
expect(toastSpy).not.toHaveBeenCalled()
|
||||||
|
}
|
||||||
expect(removeChildSpy).toHaveBeenCalledWith(mockIframe)
|
expect(removeChildSpy).toHaveBeenCalledWith(mockIframe)
|
||||||
expect(revokeObjectURLSpy).toHaveBeenCalledWith('blob:mock-url')
|
expect(revokeObjectURLSpy).toHaveBeenCalledWith('blob:mock-url')
|
||||||
|
|
||||||
@ -1643,5 +1604,7 @@ describe('DocumentDetailComponent', () => {
|
|||||||
removeChildSpy.mockRestore()
|
removeChildSpy.mockRestore()
|
||||||
createObjectURLSpy.mockRestore()
|
createObjectURLSpy.mockRestore()
|
||||||
revokeObjectURLSpy.mockRestore()
|
revokeObjectURLSpy.mockRestore()
|
||||||
}))
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user