Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for handling multiple commands from browser.post_connect_cmd #548

Merged
merged 2 commits into from
May 23, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/shared/modules/commands/commandsDuck.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { getInterpreter, isNamedInterpreter, cleanCommand } from 'services/commandUtils'
import { getInterpreter, isNamedInterpreter, cleanCommand, extractPostConnectCommandsFromServerConfig } from 'services/commandUtils'
import { hydrate } from 'services/duckUtils'
import helper from 'services/commandInterpreterHelper'
import { addHistory } from '../history/historyDuck'
Expand Down Expand Up @@ -134,7 +134,12 @@ export const postConnectCmdEpic = (some$, store) =>
const serverSettings = getAvailableSettings(store.getState())
if (serverSettings && serverSettings['browser.post_connect_cmd']) {
const cmdchar = getCmdChar(store.getState())
store.dispatch(executeSystemCommand(`${cmdchar}${serverSettings['browser.post_connect_cmd']}`))
const cmds = extractPostConnectCommandsFromServerConfig(serverSettings['browser.post_connect_cmd'])
if (cmds !== undefined) {
cmds.forEach((cmd) => {
store.dispatch(executeSystemCommand(`${cmdchar}${cmd}`))
})
}
}
})
.mapTo({ type: 'NOOP' })
57 changes: 46 additions & 11 deletions src/shared/modules/commands/postConnectCmdEpic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/* global describe, afterEach, test, expect */
/* global describe, test, expect */
import configureMockStore from 'redux-mock-store'
import { createEpicMiddleware } from 'redux-observable'
import { createBus, createReduxMiddleware } from 'suber'
Expand All @@ -27,18 +27,14 @@ import { UPDATE_SETTINGS } from 'shared/modules/dbMeta/dbMetaDuck'
import * as commands from './commandsDuck'
import { CONNECTION_SUCCESS } from 'shared/modules/connections/connectionsDuck'

const bus = createBus()
const epicMiddleware = createEpicMiddleware(commands.postConnectCmdEpic)
const mockStore = configureMockStore([epicMiddleware, createReduxMiddleware(bus)])

describe('postConnectCmdEpic', () => {
afterEach(() => {
bus.reset()
})
test('creates a SYSTEM_COMMAND_QUEUED if found', (done) => {
// Given
const bus = createBus()
const epicMiddlewareLocal = createEpicMiddleware(commands.postConnectCmdEpic)
const mockStoreLocal = configureMockStore([epicMiddlewareLocal, createReduxMiddleware(bus)])
const command = 'play hello'
const store = mockStore({
const store = mockStoreLocal({
settings: {
cmdchar: ':'
},
Expand All @@ -65,9 +61,48 @@ describe('postConnectCmdEpic', () => {
store.dispatch(action)
store.dispatch(action2)
})
test.skip('does nothing if settings not found', (done) => { // Ignore for now. Some bug in mockStore that breaks this test
test('supports multiple commands', (done) => {
// Given
const command1 = 'play hello'
const command2 = 'play intro'
const command = command1 + '; ' + command2
const bus = createBus()
const epicMiddlewareLocal = createEpicMiddleware(commands.postConnectCmdEpic)
const mockStoreLocal = configureMockStore([epicMiddlewareLocal, createReduxMiddleware(bus)])
const store = mockStoreLocal({
settings: {
cmdchar: ':'
},
meta: {
settings: {
'browser.post_connect_cmd': command
}
}
})
const action = { type: CONNECTION_SUCCESS }
const action2 = { type: UPDATE_SETTINGS }
bus.take('NOOP', (currentAction) => {
// Then
expect(store.getActions()).toEqual([
action,
action2,
commands.executeSystemCommand(':' + command1),
commands.executeSystemCommand(':' + command2),
{ type: 'NOOP' }
])
done()
})

// When
store.dispatch(action)
store.dispatch(action2)
})
test('does nothing if settings not found', (done) => {
// Given
const store = mockStore({
const bus = createBus()
const epicMiddlewareLocal = createEpicMiddleware(commands.postConnectCmdEpic)
const mockStoreLocal = configureMockStore([epicMiddlewareLocal, createReduxMiddleware(bus)])
const store = mockStoreLocal({
settings: {
cmdchar: ':'
},
Expand Down
8 changes: 8 additions & 0 deletions src/shared/services/commandUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,11 @@ export const getInterpreter = (interpret, cmd, cmdchar) => {
}

export const isNamedInterpreter = (interpreter) => interpreter && interpreter.name !== 'catch-all'

export const extractPostConnectCommandsFromServerConfig = (str) => {
const splitted = str
.split(';')
.map((item) => item.trim())
.filter((item) => item && item.length)
return splitted && splitted.length ? splitted : undefined
}
19 changes: 19 additions & 0 deletions src/shared/services/commandUtils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,23 @@ describe('commandutils', () => {
expect(obj.str + ': ' + utils.isCypherCommand(obj.str, ':')).toEqual(obj.str + ': ' + obj.expect)
})
})
test('extractPostConnectCommandsFromServerConfig should split and return an array of commands', () => {
// Given
const testStrs = [
{ str: '', expect: undefined },
{ str: ';;;;;;;;', expect: undefined },
{ str: ':play cypher', expect: [':play cypher'] },
{ str: ' :play cypher ', expect: [':play cypher'] },
{ str: ':play cypher;', expect: [':play cypher'] },
{ str: ':play cypher ;', expect: [':play cypher'] },
{ str: ';:play cypher;', expect: [':play cypher'] },
{ str: ':play cypher; :param x: 1', expect: [':play cypher', ':param x: 1'] },
{ str: 'RETURN 1; RETURN 3; :play start', expect: ['RETURN 1', 'RETURN 3', ':play start'] }
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Splitting on ; means that if you have a cypher query containing ; then that will also split.
Test case: { str: 'MATCH (n: {foo: "bar;"}) RETURN n', expect: ['MATCH (n: {foo: "bar;"}) RETURN n'] }


// When & Then
testStrs.forEach((item) => {
expect(utils.extractPostConnectCommandsFromServerConfig(item.str)).toEqual(item.expect)
})
})
})