From bf890a6d9d793b581a44ee267616ed589f429bef Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 11 Sep 2017 21:07:02 +0900 Subject: command as action/reducer --- src/actions/command.js | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 src/actions/command.js (limited to 'src/actions/command.js') diff --git a/src/actions/command.js b/src/actions/command.js new file mode 100644 index 0000000..982255c --- /dev/null +++ b/src/actions/command.js @@ -0,0 +1,35 @@ +import actions from '../actions'; + +const normalizeUrl = (string) => { + try { + return new URL(string).href + } catch (e) { + return 'http://' + string; + } +} + +export function exec(line) { + let name = line.split(' ')[0]; + let remaining = line.replace(name + ' ', ''); + + switch (name) { + case 'open': + // TODO use search engined and pass keywords to them + return { + type: actions.COMMAND_OPEN_URL, + url: normalizeUrl(remaining) + }; + case 'tabopen': + return { + type: actions.COMMAND_TABOPEN_URL, + url: remaining + }; + case 'b': + case 'buffer': + return { + type: actions.COMMAND_BUFFER, + keywords: remaining + }; + } + throw new Error(name + ' command is not defined'); +} -- cgit v1.2.3 From 2190a525b40a102851121c40654dcde2bb5ac6b3 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Mon, 11 Sep 2017 21:37:32 +0900 Subject: add command actions test --- src/actions/command.js | 2 +- test/actions/command.test.js | 51 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 test/actions/command.test.js (limited to 'src/actions/command.js') diff --git a/src/actions/command.js b/src/actions/command.js index 982255c..c983278 100644 --- a/src/actions/command.js +++ b/src/actions/command.js @@ -22,7 +22,7 @@ export function exec(line) { case 'tabopen': return { type: actions.COMMAND_TABOPEN_URL, - url: remaining + url: normalizeUrl(remaining) }; case 'b': case 'buffer': diff --git a/test/actions/command.test.js b/test/actions/command.test.js new file mode 100644 index 0000000..01a67f2 --- /dev/null +++ b/test/actions/command.test.js @@ -0,0 +1,51 @@ +import { expect } from "chai"; +import actions from '../../src/actions'; +import * as commandActions from '../../src/actions/command'; + +describe("command actions", () => { + describe("exec", () => { + context("open command", () => { + it('create COMMAND_OPEN_URL acion with a full url', () => { + let action = commandActions.exec("open https://github.com/") + expect(action.type).to.equal(actions.COMMAND_OPEN_URL); + expect(action.url).to.equal('https://github.com/'); + }); + + it('create COMMAND_OPEN_URL acion with a domain name', () => { + let action = commandActions.exec("open github.com") + expect(action.type).to.equal(actions.COMMAND_OPEN_URL); + expect(action.url).to.equal('http://github.com'); + }); + }); + + context("tabopen command", () => { + it('create COMMAND_TABOPEN_URL acion with a full url', () => { + let action = commandActions.exec("tabopen https://github.com/") + expect(action.type).to.equal(actions.COMMAND_TABOPEN_URL); + expect(action.url).to.equal('https://github.com/'); + }); + + it('create COMMAND_TABOPEN_URL acion with a domain name', () => { + let action = commandActions.exec("tabopen github.com") + expect(action.type).to.equal(actions.COMMAND_TABOPEN_URL); + expect(action.url).to.equal('http://github.com'); + }); + }); + + context("buffer command", () => { + it('create COMMAND_BUFFER acion with a keywords', () => { + let action = commandActions.exec("buffer foo bar") + expect(action.type).to.equal(actions.COMMAND_BUFFER); + expect(action.keywords).to.equal('foo bar'); + }); + }); + + context("b command", () => { + it('create COMMAND_BUFFER acion with a keywords', () => { + let action = commandActions.exec("b foo bar") + expect(action.type).to.equal(actions.COMMAND_BUFFER); + expect(action.keywords).to.equal('foo bar'); + }); + }); + }); +}); -- cgit v1.2.3