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 URL param support for populating editor with Cypher and parameters #956

Merged
merged 3 commits into from
Jan 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 30 additions & 4 deletions src/shared/modules/editor/editorDuck.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ export const SET_CONTENT = NAME + '/SET_CONTENT'
export const EDIT_CONTENT = NAME + '/EDIT_CONTENT'
export const FOCUS = `${NAME}/FOCUS`
export const EXPAND = `${NAME}/EXPAND`
export const NOT_SUPPORTED_URL_PARAM_COMMAND = `${NAME}/NOT_SUPPORTED_URL_PARAM_COMMAND`

// Supported commands
const validCommandTypes = {
play: (cmdchar, args) => `${cmdchar}play ${args.join(' ')}`,
edit: (_, args) => args.join('\n'),
param: (cmdchar, args) => `${cmdchar}param ${args.join(' ')}`,
params: (cmdchar, args) => `${cmdchar}params ${args.join(' ')}`
Copy link
Contributor

Choose a reason for hiding this comment

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

do we need to worry about interoperability here? can :play accept params? Also what happens if provided params are malformed/invalid?

}

export const setContent = newContent => ({
type: SET_CONTENT,
Expand All @@ -45,13 +54,30 @@ export const populateEditorFromUrlEpic = (some$, store) => {
.merge(some$.ofType(URL_ARGUMENTS_CHANGE))
.delay(1) // Timing issue. Needs to be detached like this
.mergeMap(action => {
if (!action.url) return Rx.Observable.never()
if (!action.url) {
return Rx.Observable.never()
}
const cmdParam = getUrlParamValue('cmd', action.url)
if (!cmdParam || cmdParam[0] !== 'play') return Rx.Observable.never()
const cmdCommand = getSettings(store.getState()).cmdchar + cmdParam[0]

// No URL command param found
if (!cmdParam || !cmdParam[0]) {
return Rx.Observable.never()
}

// Not supported URL param command
if (!Object.keys(validCommandTypes).includes(cmdParam[0])) {
return Rx.Observable.of({
type: NOT_SUPPORTED_URL_PARAM_COMMAND,
command: cmdParam[0]
})
}

const commandType = cmdParam[0]
const cmdchar = getSettings(store.getState()).cmdchar
const cmdArgs =
getUrlParamValue('arg', decodeURIComponent(action.url)) || []
const fullCommand = `${cmdCommand} ${cmdArgs.join(' ')}`
const fullCommand = validCommandTypes[commandType](cmdchar, cmdArgs)

return Rx.Observable.of({ type: SET_CONTENT, ...setContent(fullCommand) })
})
}
91 changes: 90 additions & 1 deletion src/shared/modules/editor/editorDuck.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
import configureMockStore from 'redux-mock-store'
import { createEpicMiddleware } from 'redux-observable'
import { createBus, createReduxMiddleware } from 'suber'
import { populateEditorFromUrlEpic, SET_CONTENT } from './editorDuck'
import {
populateEditorFromUrlEpic,
SET_CONTENT,
NOT_SUPPORTED_URL_PARAM_COMMAND
} from './editorDuck'
import { APP_START, URL_ARGUMENTS_CHANGE } from '../app/appDuck'

describe('editorDuck Epics', () => {
Expand Down Expand Up @@ -80,6 +84,91 @@ describe('editorDuck Epics', () => {
done()
})

// When
store.dispatch(action)
})
test('Handles the param command', done => {
const cmd = 'param'
const arg = 'x => 1'
const action = {
type: APP_START,
url: `?cmd=${cmd}&arg=${encodeURIComponent(arg)}`
}

bus.take(SET_CONTENT, currentAction => {
// Then
expect(store.getActions()).toEqual([
action,
{ type: SET_CONTENT, message: `:${cmd} ${arg}` }
])
done()
})

// When
store.dispatch(action)
})
test('Handles the params command', done => {
const cmd = 'params'
const arg = '{x: 1, y: "hello"}'
const action = {
type: APP_START,
url: `?cmd=${cmd}&arg=${encodeURIComponent(arg)}`
}

bus.take(SET_CONTENT, currentAction => {
// Then
expect(store.getActions()).toEqual([
action,
{ type: SET_CONTENT, message: `:${cmd} ${arg}` }
])
done()
})

// When
store.dispatch(action)
})
test('Accepts one or more Cypher queries from URL params and populates the editor', done => {
const cmd = 'edit'
const args = ['RETURN 1;', 'RETURN rand();']
const action = {
type: APP_START,
url:
`http://url.com?cmd=${cmd}` +
args.map(arg => `&arg=${encodeURIComponent(arg)}`).join('')
}

bus.take(SET_CONTENT, () => {
// Then
expect(store.getActions()).toEqual([
action,
{
type: SET_CONTENT,
message: args.join('\n')
}
])
done()
})

// When
store.dispatch(action)
})
test('Does not accept arbitrary URL params and populate the editor', done => {
const cmd = 'not-supported'
const arg = 'evil'
const action = {
type: APP_START,
url: `http://url.com?cmd=${cmd}&arg=${arg}`
}

bus.take(NOT_SUPPORTED_URL_PARAM_COMMAND, () => {
// Then
expect(store.getActions()).toEqual([
action,
{ type: NOT_SUPPORTED_URL_PARAM_COMMAND, command: cmd }
])
done()
})

// When
store.dispatch(action)
})
Expand Down