Skip to content

Commit 26f8ad5

Browse files
committed
#RI-4840 - [Regression] Profiler logs download failure
1 parent d97d6ab commit 26f8ad5

File tree

2 files changed

+26
-16
lines changed

2 files changed

+26
-16
lines changed

redisinsight/ui/src/components/monitor/MonitorLog/MonitorLog.spec.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { cloneDeep } from 'lodash'
22
import React from 'react'
3+
import { saveAs } from 'file-saver'
34
import { monitorSelector, resetProfiler, stopMonitor } from 'uiSrc/slices/cli/monitor'
4-
import { cleanup, fireEvent, mockedStore, render, screen } from 'uiSrc/utils/test-utils'
5+
import { act, cleanup, fireEvent, mockedStore, render, screen } from 'uiSrc/utils/test-utils'
56
import MonitorLog from './MonitorLog'
67

78
let store: typeof mockedStore
89
let URLMock: jest.SpyInstance<object>
910
const mockURLrevokeObjectURL = 123123
1011

12+
jest.mock('file-saver', () => ({
13+
...jest.requireActual('file-saver'),
14+
saveAs: jest.fn(),
15+
}))
16+
1117
jest.mock('uiSrc/slices/cli/monitor', () => ({
1218
...jest.requireActual('uiSrc/slices/cli/monitor'),
1319
monitorSelector: jest.fn().mockReturnValue({
@@ -22,6 +28,7 @@ jest.mock('uiSrc/slices/cli/monitor', () => ({
2228
}),
2329
}))
2430

31+
global.Blob = function (content, options) { return ({ content, options }) }
2532
global.fetch = jest.fn(() =>
2633
Promise.resolve({
2734
text: () => Promise.resolve('123'),
@@ -54,7 +61,8 @@ describe('MonitorLog', () => {
5461
expect(store.getActions()).toEqual(expectedActions)
5562
})
5663

57-
it.skip('should call download a file', () => {
64+
it('should call download a file', async () => {
65+
const saveAsMock = jest.fn()
5866
const monitorSelectorMock = jest.fn().mockReturnValue({
5967
isSaveToFile: true,
6068
logFileId: 'logFileId',
@@ -65,11 +73,20 @@ describe('MonitorLog', () => {
6573
duration: 123,
6674
}
6775
});
68-
(monitorSelector as jest.Mock).mockImplementation(monitorSelectorMock)
76+
77+
(monitorSelector as jest.Mock).mockImplementation(monitorSelectorMock);
78+
(saveAs as jest.Mock).mockImplementation(() => saveAsMock)
6979

7080
render(<MonitorLog />)
71-
fireEvent.click(screen.getByTestId('download-log-btn'))
7281

73-
expect(URLMock).toBeCalled()
82+
await act(() => {
83+
fireEvent.click(screen.getByTestId('download-log-btn'))
84+
})
85+
86+
expect(saveAs).toBeCalledWith(
87+
{ content: ['123'], options: { type: 'text/plain;charset=utf-8' } },
88+
'filename.txt',
89+
)
90+
saveAs.mockRestore()
7491
})
7592
})

redisinsight/ui/src/components/monitor/MonitorLog/MonitorLog.tsx

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { format, formatDuration, intervalToDuration } from 'date-fns'
33
import React from 'react'
44
import { useDispatch, useSelector } from 'react-redux'
55
import AutoSizer from 'react-virtualized-auto-sizer'
6+
import { saveAs } from 'file-saver'
7+
68
import { ApiEndpoints } from 'uiSrc/constants'
79
import { monitorSelector, resetProfiler, stopMonitor } from 'uiSrc/slices/cli/monitor'
810
import { cutDurationText, getBaseApiUrl } from 'uiSrc/utils'
@@ -54,18 +56,9 @@ const MonitorLog = () => {
5456
)
5557

5658
const contentDisposition = response.headers.get('Content-Disposition') || ''
59+
const file = new Blob([await response.text()], { type: 'text/plain;charset=utf-8' })
5760

58-
downloadFile(await response.text(), contentDisposition.split('"')?.[1])
59-
}
60-
61-
const downloadFile = (content: string, fileName: string) => {
62-
const link = document.createElement('a')
63-
const file = new Blob([content], { type: 'text/plain' })
64-
link.href = URL.createObjectURL(file)
65-
link.download = fileName
66-
link.click()
67-
68-
URL.revokeObjectURL(link.href)
61+
saveAs(file, contentDisposition.split('"')?.[1])
6962
}
7063

7164
return (

0 commit comments

Comments
 (0)