feature(vim) add find support with: "/", "n" and "N"

This commit is contained in:
coderaiser 2017-09-11 17:12:24 +03:00
parent 3693f6f799
commit 2edf7f8321
7 changed files with 269 additions and 29 deletions

View file

@ -0,0 +1,79 @@
'use strict';
const test = require('tape');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const dir = '../../../../client/key/vim/';
const {
getDOM,
getCloudCmd,
} = require('./globals');
global.DOM = global.DOM || getDOM();
global.CloudCmd = global.CloudCmd || getCloudCmd();
const DOM = global.DOM
const CloudCmd = global.CloudCmd;
const {
find,
findNext,
findPrevious,
_next,
_previous,
} = require(dir + 'find');
test('cloudcmd: client: vim: find', (t) => {
const setCurrentByName = sinon.stub();
DOM.setCurrentByName = setCurrentByName;
DOM.Dialog.prompt = Promise.resolve.bind(Promise);
find('');
t.ok(setCurrentByName.calledWith(), 'should call setCurrentByName');
t.end();
});
test('cloudcmd: client: vim: findNext', (t) => {
const setCurrentByName = sinon.stub();
DOM.setCurrentByName = setCurrentByName;
findNext();
t.ok(setCurrentByName.calledWith(), 'should call setCurrentByName');
t.end();
});
test('cloudcmd: client: vim: findPrevious', (t) => {
const setCurrentByName = sinon.stub();
DOM.setCurrentByName = setCurrentByName;
findPrevious();
t.ok(setCurrentByName.calledWith(), 'should call setCurrentByName');
t.end();
});
test('cloudcmd: client: vim: _next', (t) => {
const result = _next(1, 2)
t.notOk(result, 'should return 0');
t.end();
});
test('cloudcmd: client: vim: _previous', (t) => {
const result = _previous(0, 2)
t.equal(result, 1, 'should return 1');
t.end();
});
function clean(path) {
delete require.cache[require.resolve(path)];
}
function stub(name, fn) {
require.cache[require.resolve(name)].exports = fn;
}

View file

@ -0,0 +1,43 @@
'use strict';
module.exports.getDOM = () => {
const resolve = Promise.resolve.bind(Promise);
const CurrentInfo = {
element: {},
files: [],
};
const noop = () => {};
const Buffer = {
copy: noop,
paste: noop,
};
const Dialog = {
prompt: resolve
};
return {
Buffer,
CurrentInfo,
Dialog,
selectFile: noop,
unselectFile: noop,
unselectFiles: noop,
setCurrentFile: noop,
getCurrentName: noop,
setCurrentByName: noop,
toggleSelectedFile: noop,
};
};
module.exports.getCloudCmd = () => {
const show = () => {};
return {
Operation: {
show
}
};
};

View file

@ -3,10 +3,16 @@
const test = require('tape');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const dir = '../../../client/key/';
const dir = '../../../../client/key/';
const KEY = require(dir + 'key');
initGlobals();
const {
getDOM,
getCloudCmd,
} = require('./globals');
global.DOM = global.DOM || getDOM();
global.CloudCmd = global.CloudCmd || getCloudCmd();
const DOM = global.DOM;
const Buffer = DOM.Buffer;
@ -349,8 +355,8 @@ test('cloudcmd: client: key: Enter', (t) => {
const setCurrentFile = sinon.stub();
global.DOM.CurrentInfo.element = element;
global.DOM.setCurrentFile = setCurrentFile;
DOM.CurrentInfo.element = element;
DOM.setCurrentFile = setCurrentFile;
vim('', {
keyCode: KEY.ENTER
@ -363,32 +369,60 @@ test('cloudcmd: client: key: Enter', (t) => {
t.end();
});
function initGlobals() {
const CurrentInfo = {
element: {},
};
test('cloudcmd: client: key: /', (t) => {
const preventDefault = sinon.stub();
const element = {};
const noop = () => {};
const Buffer = {
copy: noop,
};
DOM.CurrentInfo.element = element;
DOM.getCurrentName = () => '';
global.DOM = {
Buffer,
CurrentInfo,
selectFile: noop,
unselectFile: noop,
unselectFiles: noop,
setCurrentFile: noop,
toggleSelectedFile: noop,
};
vim('/', {
preventDefault
});
const show = () => {};
t.ok(preventDefault.calledWith(), 'should call preventDefault');
t.end();
});
test('cloudcmd: client: key: n', (t) => {
const findNext = sinon.stub();
global.CloudCmd = {
Operation: {
show
}
};
clean(dir + 'vim');
stub(dir + 'vim/find', {
findNext
});
const vim = require(dir + 'vim');
const event = {};
vim('n', event);
t.ok(findNext.calledWith(), 'should call findNext');
t.end();
});
test('cloudcmd: client: key: N', (t) => {
const findPrevious = sinon.stub();
clean(dir + 'vim');
stub(dir + 'vim/find', {
findPrevious,
});
const vim = require(dir + 'vim');
const event = {};
vim('N', event);
t.ok(findPrevious.calledWith(), 'should call findPrevious');
t.end();
});
function clean(path) {
delete require.cache[require.resolve(path)];
}
function stub(name, fn) {
require.cache[require.resolve(name)].exports = fn;
}