diff --git a/src/js/components/VariableMeta.js b/src/js/components/VariableMeta.js
index 143d0828..06153b39 100644
--- a/src/js/components/VariableMeta.js
+++ b/src/js/components/VariableMeta.js
@@ -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
@@ -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
}
diff --git a/src/js/index.js b/src/js/index.js
index b2ed4031..9ba64985 100644
--- a/src/js/index.js
+++ b/src/js/index.js
@@ -247,6 +247,7 @@ class ReactJsonView extends React.PureComponent {
src: updatedSrc
})
} else {
+ ObjectAttributes.set(this.rjvId, 'global', 'src', src)
this.setState({
validationFailure: true
})
diff --git a/src/js/stores/ObjectAttributes.js b/src/js/stores/ObjectAttributes.js
index ff538208..add9bbb3 100644
--- a/src/js/stores/ObjectAttributes.js
+++ b/src/js/stores/ObjectAttributes.js
@@ -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
diff --git a/test/tests/js/Index-test.js b/test/tests/js/Index-test.js
index ee3fdec0..deb009f1 100644
--- a/test/tests/js/Index-test.js
+++ b/test/tests/js/Index-test.js
@@ -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('', 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(
+ 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()
+ 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(
+
+ )
+ 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(