Skip to content
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
4 changes: 2 additions & 2 deletions src/js/components/VariableMeta.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export default class extends React.PureComponent {
onClick={() => {
const request = {
name: depth > 0 ? name : null,
namespace: namespace.splice(0, namespace.length - 1),
namespace: namespace.slice(0, namespace.length - 1),
existing_value: src,
variable_removed: false,
key_name: null
Expand Down Expand Up @@ -89,7 +89,7 @@ export default class extends React.PureComponent {
rjvId,
data: {
name,
namespace: namespace.splice(0, namespace.length - 1),
namespace: namespace.slice(0, namespace.length - 1),
existing_value: src,
variable_removed: true
}
Expand Down
1 change: 1 addition & 0 deletions src/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class ReactJsonView extends React.PureComponent {
src: updatedSrc
})
} else {
ObjectAttributes.set(this.rjvId, 'global', 'src', src)
this.setState({
validationFailure: true
})
Expand Down
4 changes: 2 additions & 2 deletions src/js/stores/ObjectAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ class ObjectAttributes extends EventEmitter {
}

updateSrc = (rjvId, request) => {
// Keep the editor's namespace intact when an update is rejected.
request.namespace = request.namespace.slice(1)
const { name, namespace } = request
const newValue = request[NEW_VALUE_KEY]
const variableRemoved = request[VARIABLE_REMOVED_KEY]

namespace.shift()

// deepy copy src
const src = this.get(rjvId, 'global', 'src')
// deep copy of src variable
Expand Down
92 changes: 92 additions & 0 deletions test/tests/js/Index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,104 @@ import { expect } from 'chai'
import { JSDOM } from 'jsdom'

import Index from './../../../src/js/index'
import dispatcher from './../../../src/js/helpers/dispatcher'
import VariableMeta from './../../../src/js/components/VariableMeta'

const { window } = new JSDOM()
global.window = window
global.document = window.document

describe('<Index />', function () {
for (const action of ['VARIABLE_UPDATED', 'VARIABLE_ADDED', 'VARIABLE_REMOVED']) {
it(`does not include a rejected ${action} in a later accepted edit`, function () {
const src = { locked: 1, editable: 2 }
const onEdit = sinon.spy(change => change.name === 'editable')
const wrapper = mount(
<Index src={src} onEdit={onEdit} onAdd={() => false} onDelete={() => false} />
)
try {
const rjvId = wrapper.instance().rjvId
dispatcher.dispatch({
name: action,
rjvId,
data: {
name: action === 'VARIABLE_ADDED' ? 'added' : 'locked',
namespace: ['root'],
new_value: 3,
existing_value: action === 'VARIABLE_ADDED' ? undefined : 1,
variable_removed: action === 'VARIABLE_REMOVED'
}
})
expect(wrapper.state('src')).to.deep.equal(src)
dispatcher.dispatch({
name: 'VARIABLE_UPDATED',
rjvId,
data: {
name: 'editable',
namespace: ['root'],
new_value: 4,
existing_value: 2
}
})
expect(onEdit.lastCall.args[0].updated_src).to.deep.equal({ locked: 1, editable: 4 })
expect(wrapper.state('src')).to.deep.equal({ locked: 1, editable: 4 })
expect(src).to.deep.equal({ locked: 1, editable: 2 })
} finally {
wrapper.unmount()
}
})
}

it('keeps the nested path when a type-changing edit is rejected twice', function () {
const src = { branch: { locked: 1 } }
const onEdit = sinon.spy(() => false)
const wrapper = mount(<Index src={src} onEdit={onEdit} />)
try {
for (let attempt = 0; attempt < 2; attempt++) {
wrapper.find('.click-to-edit-icon').hostNodes().simulate('click')
wrapper.find('textarea').simulate('change', { target: { value: 'undefined' } })
wrapper.find('.edit-check.detected').hostNodes().simulate('click')
expect(onEdit.lastCall.args[0].namespace).to.deep.equal(['branch'])
expect(onEdit.lastCall.args[0].updated_src).to.deep.equal({ branch: { locked: undefined } })
expect(wrapper.state('src')).to.deep.equal(src)
}
expect(onEdit.callCount).to.equal(2)
expect(src).to.deep.equal({ branch: { locked: 1 } })
} finally {
wrapper.unmount()
}
})

for (const operation of ['add', 'delete']) {
it(`keeps child edits nested after a rejected object ${operation}`, function () {
const src = { branch: { editable: 1 } }
const reject = sinon.spy(() => false)
const onEdit = sinon.spy(() => true)
const wrapper = mount(
<Index src={src} onEdit={onEdit} onAdd={reject} onDelete={reject} />
)
try {
const meta = wrapper.find(VariableMeta).filterWhere(node => node.prop('name') === 'branch')
const icon = operation === 'add' ? 'add' : 'remove'
meta.find(`.click-to-${icon}-icon`).hostNodes().simulate('click')
if (operation === 'add') {
wrapper.find('.key-modal-input').simulate('change', { target: { value: 'newKey' } })
wrapper.find('.key-modal-submit').hostNodes().simulate('click')
}
expect(reject.calledOnce).to.equal(true)
expect(wrapper.state('src')).to.deep.equal(src)
wrapper.find('.click-to-edit-icon').hostNodes().simulate('click')
wrapper.find('textarea').simulate('change', { target: { value: '2' } })
wrapper.find('.edit-check.detected').hostNodes().simulate('click')
expect(onEdit.lastCall.args[0].namespace).to.deep.equal(['branch'])
expect(wrapper.state('src')).to.deep.equal({ branch: { editable: 2 } })
expect(src).to.deep.equal({ branch: { editable: 1 } })
} finally {
wrapper.unmount()
}
})
}

it('check data type labels from index', function () {
const wrapper = render(
<Index
Expand Down
Loading