Sure sonar, consolidate

This commit is contained in:
shamoon 2025-10-30 18:00:19 -07:00
parent 8f969ecab5
commit 9f0a4ac19d
No known key found for this signature in database

View File

@ -1514,134 +1514,97 @@ describe('DocumentDetailComponent', () => {
) )
}) })
it('should show error toast if printing throws inside iframe', fakeAsync(() => { const iframePrintErrorCases: Array<{
initNormally() description: string
thrownError: Error
expectToast: boolean
}> = [
{
description: 'should show error toast if printing throws inside iframe',
thrownError: new Error('focus failed'),
expectToast: true,
},
{
description:
'should suppress toast if cross-origin afterprint error occurs',
thrownError: new DOMException(
'Accessing onafterprint triggered a cross-origin violation',
'SecurityError'
),
expectToast: false,
},
]
const appendChildSpy = jest iframePrintErrorCases.forEach(({ description, thrownError, expectToast }) => {
.spyOn(document.body, 'appendChild') it(
.mockImplementation((node: Node) => node) description,
const removeChildSpy = jest fakeAsync(() => {
.spyOn(document.body, 'removeChild') initNormally()
.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 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 mockContentWindow = { const toastSpy = jest.spyOn(toastService, 'showError')
focus: jest.fn().mockImplementation(() => {
throw new Error('focus failed')
}),
print: jest.fn(),
onafterprint: null,
}
const mockIframe: any = { const mockContentWindow = {
style: {}, focus: jest.fn().mockImplementation(() => {
src: '', throw thrownError
onload: null, }),
contentWindow: mockContentWindow, print: jest.fn(),
} onafterprint: null,
}
const createElementSpy = jest const mockIframe: any = {
.spyOn(document, 'createElement') style: {},
.mockReturnValue(mockIframe as any) src: '',
onload: null,
contentWindow: mockContentWindow,
}
const blob = new Blob(['test'], { type: 'application/pdf' }) const createElementSpy = jest
component.printDocument() .spyOn(document, 'createElement')
.mockReturnValue(mockIframe as any)
const req = httpTestingController.expectOne( const blob = new Blob(['test'], { type: 'application/pdf' })
`${environment.apiBaseUrl}documents/${doc.id}/download/` component.printDocument()
)
req.flush(blob)
tick() const req = httpTestingController.expectOne(
`${environment.apiBaseUrl}documents/${doc.id}/download/`
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',
'SecurityError'
) )
}), req.flush(blob)
print: jest.fn(),
onafterprint: null,
}
const mockIframe: any = { tick()
style: {},
src: '',
onload: null,
contentWindow: mockContentWindow,
}
const createElementSpy = jest if (mockIframe.onload) {
.spyOn(document, 'createElement') mockIframe.onload(new Event('load'))
.mockReturnValue(mockIframe as any) }
const blob = new Blob(['test'], { type: 'application/pdf' }) tick(200)
component.printDocument()
const req = httpTestingController.expectOne( if (expectToast) {
`${environment.apiBaseUrl}documents/${doc.id}/download/` expect(toastSpy).toHaveBeenCalled()
} else {
expect(toastSpy).not.toHaveBeenCalled()
}
expect(removeChildSpy).toHaveBeenCalledWith(mockIframe)
expect(revokeObjectURLSpy).toHaveBeenCalledWith('blob:mock-url')
createElementSpy.mockRestore()
appendChildSpy.mockRestore()
removeChildSpy.mockRestore()
createObjectURLSpy.mockRestore()
revokeObjectURLSpy.mockRestore()
})
) )
req.flush(blob) })
tick()
if (mockIframe.onload) {
mockIframe.onload(new Event('load'))
}
tick(200)
expect(toastSpy).not.toHaveBeenCalled()
expect(removeChildSpy).toHaveBeenCalledWith(mockIframe)
expect(revokeObjectURLSpy).toHaveBeenCalledWith('blob:mock-url')
createElementSpy.mockRestore()
appendChildSpy.mockRestore()
removeChildSpy.mockRestore()
createObjectURLSpy.mockRestore()
revokeObjectURLSpy.mockRestore()
}))
}) })