2021-06-29 04:03:36 -05:00
|
|
|
/////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// pgAdmin 4 - PostgreSQL Tools
|
|
|
|
//
|
2022-01-04 02:24:25 -06:00
|
|
|
// Copyright (C) 2013 - 2022, The pgAdmin Development Team
|
2021-06-29 04:03:36 -05:00
|
|
|
// This software is released under the PostgreSQL Licence
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
import jasmineEnzyme from 'jasmine-enzyme';
|
|
|
|
import React from 'react';
|
|
|
|
import '../helper/enzyme.helper';
|
|
|
|
import {default as OrigCodeMirror} from 'bundled_codemirror';
|
2022-02-10 23:06:24 -06:00
|
|
|
import { withTheme } from '../fake_theme';
|
2021-06-29 04:03:36 -05:00
|
|
|
|
2022-04-11 06:55:19 -05:00
|
|
|
import pgWindow from 'sources/window';
|
2021-06-29 04:03:36 -05:00
|
|
|
import CodeMirror from 'sources/components/CodeMirror';
|
|
|
|
import { mount } from 'enzyme';
|
2022-02-10 23:06:24 -06:00
|
|
|
import { FindDialog } from '../../../pgadmin/static/js/components/CodeMirror';
|
2022-04-11 06:55:19 -05:00
|
|
|
import fakePgAdmin from '../fake_pgadmin';
|
2021-06-29 04:03:36 -05:00
|
|
|
|
|
|
|
describe('CodeMirror', ()=>{
|
|
|
|
let cmInstance, options={
|
|
|
|
lineNumbers: true,
|
|
|
|
mode: 'text/x-pgsql',
|
2021-09-27 23:54:25 -05:00
|
|
|
},
|
|
|
|
cmObj = jasmine.createSpyObj('cmObj', {
|
|
|
|
'getValue':()=>'',
|
2022-01-12 06:02:20 -06:00
|
|
|
'setValue': ()=>{/*This is intentional (SonarQube)*/},
|
|
|
|
'refresh': ()=>{/*This is intentional (SonarQube)*/},
|
|
|
|
'setOption': ()=>{/*This is intentional (SonarQube)*/},
|
|
|
|
'removeKeyMap': ()=>{/*This is intentional (SonarQube)*/},
|
|
|
|
'addKeyMap': ()=>{/*This is intentional (SonarQube)*/},
|
2022-02-10 23:06:24 -06:00
|
|
|
'getSearchCursor': {
|
|
|
|
_from: 3,
|
|
|
|
_to: 14,
|
|
|
|
find: function(_rev) {
|
|
|
|
if(_rev){
|
|
|
|
this._from = 1;
|
|
|
|
this._to = 10;
|
|
|
|
} else {
|
|
|
|
this._from = 3;
|
|
|
|
this._to = 14;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
from: function() {return this._from;},
|
|
|
|
to: function() {return this._to;},
|
|
|
|
replace: jasmine.createSpy('replace'),
|
|
|
|
},
|
|
|
|
'getCursor': ()=>{/*This is intentional (SonarQube)*/},
|
|
|
|
'removeOverlay': ()=>{/*This is intentional (SonarQube)*/},
|
|
|
|
'addOverlay': ()=>{/*This is intentional (SonarQube)*/},
|
|
|
|
'setSelection': ()=>{/*This is intentional (SonarQube)*/},
|
|
|
|
'scrollIntoView': ()=>{/*This is intentional (SonarQube)*/},
|
2021-09-27 23:54:25 -05:00
|
|
|
'getWrapperElement': document.createElement('div'),
|
2022-04-07 07:06:56 -05:00
|
|
|
'on': ()=>{/*This is intentional (SonarQube)*/},
|
2021-09-27 23:54:25 -05:00
|
|
|
});
|
2021-06-29 04:03:36 -05:00
|
|
|
beforeEach(()=>{
|
|
|
|
jasmineEnzyme();
|
2022-04-11 06:55:19 -05:00
|
|
|
pgWindow.pgAdmin = fakePgAdmin;
|
2021-06-29 04:03:36 -05:00
|
|
|
spyOn(OrigCodeMirror, 'fromTextArea').and.returnValue(cmObj);
|
2022-02-10 23:06:24 -06:00
|
|
|
const ThemedCM = withTheme(CodeMirror);
|
2021-06-29 04:03:36 -05:00
|
|
|
cmInstance = mount(
|
2022-02-10 23:06:24 -06:00
|
|
|
<ThemedCM
|
2021-06-29 04:03:36 -05:00
|
|
|
value={'Init text'}
|
|
|
|
options={options}
|
|
|
|
className="testClass"
|
|
|
|
/>);
|
|
|
|
});
|
|
|
|
|
2022-04-07 07:06:56 -05:00
|
|
|
afterEach(()=>{
|
2022-04-11 06:55:19 -05:00
|
|
|
pgWindow.pgAdmin = undefined;
|
2022-04-07 07:06:56 -05:00
|
|
|
});
|
|
|
|
|
2021-06-29 04:03:36 -05:00
|
|
|
it('init', ()=>{
|
|
|
|
/* textarea ref passed to fromTextArea */
|
2022-02-10 23:06:24 -06:00
|
|
|
expect(OrigCodeMirror.fromTextArea).toHaveBeenCalledWith(cmInstance.find('textarea').getDOMNode(), jasmine.objectContaining(options));
|
2021-06-29 04:03:36 -05:00
|
|
|
expect(cmObj.setValue).toHaveBeenCalledWith('Init text');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('change value', ()=>{
|
|
|
|
cmInstance.setProps({value: 'the new text'});
|
|
|
|
expect(cmObj.setValue).toHaveBeenCalledWith('the new text');
|
2022-02-10 23:06:24 -06:00
|
|
|
|
|
|
|
cmInstance.setProps({value: null});
|
|
|
|
expect(cmObj.setValue).toHaveBeenCalledWith('');
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
describe('FindDialog', ()=>{
|
|
|
|
let ctrl;
|
|
|
|
const onClose = jasmine.createSpy('onClose');
|
|
|
|
const ThemedFindDialog = withTheme(FindDialog);
|
|
|
|
const ctrlMount = (props, callback)=>{
|
|
|
|
ctrl?.unmount();
|
|
|
|
ctrl = mount(
|
|
|
|
<ThemedFindDialog
|
|
|
|
editor={cmObj}
|
|
|
|
show={true}
|
|
|
|
onClose={onClose}
|
|
|
|
{...props}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
setTimeout(()=>{
|
|
|
|
ctrl.update();
|
|
|
|
callback();
|
|
|
|
}, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
it('init', (done)=>{
|
|
|
|
ctrlMount({}, ()=>{
|
|
|
|
cmObj.removeOverlay.calls.reset();
|
|
|
|
cmObj.addOverlay.calls.reset();
|
|
|
|
ctrl.find('InputText').find('input').simulate('change', {
|
|
|
|
target: {value: '\n\r\t\A'},
|
|
|
|
});
|
|
|
|
setTimeout(()=>{
|
|
|
|
expect(cmObj.removeOverlay).toHaveBeenCalled();
|
|
|
|
expect(cmObj.addOverlay).toHaveBeenCalled();
|
|
|
|
expect(cmObj.setSelection).toHaveBeenCalledWith(3, 14);
|
|
|
|
expect(cmObj.scrollIntoView).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('reverse forward', (done)=>{
|
|
|
|
ctrlMount({}, ()=>{
|
|
|
|
ctrl.find('InputText').find('input').simulate('change', {
|
|
|
|
target: {value: 'A'},
|
|
|
|
});
|
|
|
|
cmObj.setSelection.calls.reset();
|
|
|
|
cmObj.addOverlay.calls.reset();
|
|
|
|
ctrl.find('InputText').find('input').simulate('keypress', {
|
|
|
|
key: 'Enter', shiftKey: true,
|
|
|
|
});
|
|
|
|
ctrl.find('InputText').find('input').simulate('keypress', {
|
|
|
|
key: 'Enter', shiftKey: false,
|
|
|
|
});
|
|
|
|
setTimeout(()=>{
|
|
|
|
expect(cmObj.setSelection).toHaveBeenCalledWith(1, 10);
|
|
|
|
expect(cmObj.setSelection).toHaveBeenCalledWith(3, 14);
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('escape', (done)=>{
|
|
|
|
ctrlMount({}, ()=>{
|
|
|
|
cmObj.removeOverlay.calls.reset();
|
|
|
|
ctrl.find('InputText').find('input').simulate('keydown', {
|
|
|
|
key: 'Escape',
|
|
|
|
});
|
|
|
|
setTimeout(()=>{
|
|
|
|
expect(cmObj.removeOverlay).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggle match case', (done)=>{
|
|
|
|
ctrlMount({}, ()=>{
|
|
|
|
expect(ctrl.find('PgIconButton[data-test="case"]').props()).toEqual(jasmine.objectContaining({
|
|
|
|
color: 'default'
|
|
|
|
}));
|
|
|
|
ctrl.find('PgIconButton[data-test="case"]').find('button').simulate('click');
|
|
|
|
setTimeout(()=>{
|
|
|
|
expect(ctrl.find('PgIconButton[data-test="case"]').props()).toEqual(jasmine.objectContaining({
|
|
|
|
color: 'primary'
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('toggle regex', (done)=>{
|
|
|
|
ctrlMount({}, ()=>{
|
|
|
|
ctrl.find('InputText').find('input').simulate('change', {
|
|
|
|
target: {value: 'A'},
|
|
|
|
});
|
|
|
|
expect(ctrl.find('PgIconButton[data-test="regex"]').props()).toEqual(jasmine.objectContaining({
|
|
|
|
color: 'default'
|
|
|
|
}));
|
|
|
|
ctrl.find('PgIconButton[data-test="regex"]').find('button').simulate('click');
|
|
|
|
setTimeout(()=>{
|
|
|
|
expect(ctrl.find('PgIconButton[data-test="regex"]').props()).toEqual(jasmine.objectContaining({
|
|
|
|
color: 'primary'
|
|
|
|
}));
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('replace', (done)=>{
|
|
|
|
ctrlMount({replace: true}, ()=>{
|
|
|
|
cmObj.getSearchCursor().replace.calls.reset();
|
|
|
|
ctrl.find('InputText').at(0).find('input').simulate('change', {
|
|
|
|
target: {value: 'A'},
|
|
|
|
});
|
|
|
|
ctrl.find('InputText').at(1).find('input').simulate('change', {
|
|
|
|
target: {value: 'B'},
|
|
|
|
});
|
|
|
|
ctrl.find('InputText').at(1).find('input').simulate('keypress', {
|
|
|
|
key: 'Enter', shiftKey: true,
|
|
|
|
});
|
|
|
|
setTimeout(()=>{
|
|
|
|
expect(cmObj.getSearchCursor().replace).toHaveBeenCalled();
|
|
|
|
done();
|
|
|
|
}, 0);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-06-29 04:03:36 -05:00
|
|
|
});
|
|
|
|
});
|