2022-08-11 00:19:45 -05:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
2023-08-01 02:34:06 -05:00
|
|
|
import axios from 'axios';
|
2023-10-23 07:13:17 -05:00
|
|
|
import pgAdmin from 'sources/pgadmin';
|
|
|
|
import BgProcessManager from '../../../pgadmin/misc/bgprocess/static/js/BgProcessManager';
|
|
|
|
import { BgProcessManagerProcessState } from '../../../pgadmin/misc/bgprocess/static/js/BgProcessConstants';
|
2022-08-11 00:19:45 -05:00
|
|
|
import * as BgProcessNotify from '../../../pgadmin/misc/bgprocess/static/js/BgProcessNotify';
|
|
|
|
|
|
|
|
|
|
|
|
describe('BgProcessManager', ()=>{
|
|
|
|
let obj;
|
|
|
|
let networkMock;
|
2023-10-23 07:13:17 -05:00
|
|
|
const pgBrowser = pgAdmin.Browser;
|
2022-08-11 00:19:45 -05:00
|
|
|
|
|
|
|
beforeAll(()=>{
|
|
|
|
networkMock = new MockAdapter(axios);
|
|
|
|
networkMock.onGet('/misc/bgprocess/').reply(200, [{}]);
|
|
|
|
networkMock.onPut('/misc/bgprocess/stop/12345').reply(200,{});
|
|
|
|
networkMock.onPut('/misc/bgprocess/12345').reply(200,{});
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
networkMock.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(()=>{
|
|
|
|
obj = new BgProcessManager(pgBrowser);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('init', ()=>{
|
2023-10-23 07:13:17 -05:00
|
|
|
jest.spyOn(obj, 'startWorker').mockImplementation(() => {});
|
2022-08-11 00:19:45 -05:00
|
|
|
obj.init();
|
|
|
|
expect(obj.startWorker).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('procList', ()=>{
|
|
|
|
obj._procList = [{a: '1'}];
|
|
|
|
expect(obj.procList).toEqual([{a: '1'}]);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('startWorker', (done)=>{
|
2023-10-23 07:13:17 -05:00
|
|
|
jest.spyOn(obj, 'syncProcesses').mockImplementation(() => {});
|
2022-08-11 00:19:45 -05:00
|
|
|
obj._pendingJobId = ['123123123123'];
|
|
|
|
obj.startWorker();
|
|
|
|
|
|
|
|
setTimeout(()=>{
|
|
|
|
expect(obj.syncProcesses).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
}, 2000);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('startProcess', ()=>{
|
2023-10-23 07:13:17 -05:00
|
|
|
let nSpy = jest.spyOn(BgProcessNotify, 'processStarted');
|
2022-08-11 00:19:45 -05:00
|
|
|
obj.startProcess('12345', 'process desc');
|
|
|
|
expect(obj._pendingJobId).toEqual(['12345']);
|
2023-10-23 07:13:17 -05:00
|
|
|
expect(nSpy.mock.calls[nSpy.mock.calls.length - 1][0]).toBe('process desc');
|
2022-08-11 00:19:45 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('stopProcess', (done)=>{
|
|
|
|
obj._procList = [{
|
|
|
|
id: '12345',
|
|
|
|
process_state: BgProcessManagerProcessState.PROCESS_STARTED,
|
|
|
|
}];
|
|
|
|
|
|
|
|
obj.stopProcess('12345');
|
|
|
|
setTimeout(()=>{
|
|
|
|
expect(obj._procList[0].process_state).toBe(BgProcessManagerProcessState.PROCESS_TERMINATED);
|
|
|
|
done();
|
|
|
|
}, 500);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('acknowledge', (done)=>{
|
|
|
|
obj._procList = [{
|
|
|
|
id: '12345',
|
|
|
|
process_state: BgProcessManagerProcessState.PROCESS_FINISHED,
|
|
|
|
}];
|
|
|
|
|
|
|
|
obj.acknowledge(['12345']);
|
|
|
|
setTimeout(()=>{
|
|
|
|
expect(obj._procList.length).toBe(0);
|
|
|
|
done();
|
|
|
|
}, 500);
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('checkPending', ()=>{
|
|
|
|
obj._procList = [{
|
|
|
|
id: '12345',
|
|
|
|
process_state: BgProcessManagerProcessState.PROCESS_FINISHED,
|
2023-10-23 07:13:17 -05:00
|
|
|
desc: 'Some desc',
|
2022-08-11 00:19:45 -05:00
|
|
|
}];
|
|
|
|
obj._pendingJobId = ['12345'];
|
2023-10-23 07:13:17 -05:00
|
|
|
let nSpy = jest.spyOn(BgProcessNotify, 'processCompleted');
|
2022-08-11 00:19:45 -05:00
|
|
|
obj.checkPending();
|
|
|
|
expect(nSpy).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('openProcessesPanel', ()=>{
|
2023-10-23 07:13:17 -05:00
|
|
|
const panel = {};
|
|
|
|
jest.spyOn(pgBrowser.docker, 'openTab').mockReturnValue(panel);
|
2022-08-11 00:19:45 -05:00
|
|
|
|
|
|
|
/* panel open */
|
2023-10-23 07:13:17 -05:00
|
|
|
jest.spyOn(pgBrowser.docker, 'find').mockReturnValue(panel);
|
|
|
|
jest.spyOn(pgBrowser.docker, 'focus');
|
2022-08-11 00:19:45 -05:00
|
|
|
obj.openProcessesPanel();
|
2023-10-23 07:13:17 -05:00
|
|
|
expect(pgBrowser.docker.focus).toHaveBeenCalled();
|
|
|
|
expect(pgBrowser.docker.openTab).not.toHaveBeenCalled();
|
2022-08-11 00:19:45 -05:00
|
|
|
|
|
|
|
/* panel closed */
|
2023-10-23 07:13:17 -05:00
|
|
|
jest.spyOn(pgBrowser.docker, 'find').mockReturnValue(null);
|
2022-08-11 00:19:45 -05:00
|
|
|
obj.openProcessesPanel();
|
2023-10-23 07:13:17 -05:00
|
|
|
expect(pgBrowser.docker.openTab).toHaveBeenCalled();
|
2022-08-11 00:19:45 -05:00
|
|
|
});
|
|
|
|
});
|