-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(json-patch): refactor createJsonPathOperations, add tests
- Loading branch information
Showing
3 changed files
with
172 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/pages/dataElements/edit/createJsonPatchOperations.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { | ||
sanitizeDirtyValueKeys, | ||
createJsonPatchOperations, | ||
} from './createJsonPatchOperations' | ||
|
||
describe('createJsonPatchOperations', () => { | ||
describe('sanitizeDirtyValueKeys', () => { | ||
it('should return the dirty values array as is', () => { | ||
const actual = sanitizeDirtyValueKeys(['foo', 'bar']) | ||
const expected = ['foo', 'bar'] | ||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
it('should remove all attribute values changes and add a single "attributeValues"', () => { | ||
const actual = sanitizeDirtyValueKeys([ | ||
'foo', | ||
'bar', | ||
'attributeValues[0].value', | ||
'attributeValues[1].value', | ||
]) | ||
const expected = ['foo', 'bar', 'attributeValues'] | ||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
it('should remove style.icon and style.color changes and add a single "style"', () => { | ||
const actual = sanitizeDirtyValueKeys([ | ||
'foo', | ||
'bar', | ||
'style.color', | ||
'style.icon', | ||
]) | ||
const expected = ['foo', 'bar', 'style'] | ||
expect(actual).toEqual(expected) | ||
}) | ||
}) | ||
|
||
describe('createJsonPatchOperations', () => { | ||
it('should return an empty array if no dirty fields', () => { | ||
const actual = createJsonPatchOperations({ | ||
dirtyFields: {}, | ||
originalValue: { | ||
id: 'foo', | ||
attributeValues: [], | ||
}, | ||
values: { | ||
attributeValues: [], | ||
}, | ||
}) | ||
expect(actual).toEqual([]) | ||
}) | ||
|
||
it('should return a json-patch payload for a single field', () => { | ||
const actual = createJsonPatchOperations({ | ||
dirtyFields: { | ||
name: true, | ||
}, | ||
originalValue: { | ||
id: 'foo', | ||
name: 'bar', | ||
attributeValues: [], | ||
}, | ||
values: { | ||
name: 'baz', | ||
attributeValues: [], | ||
}, | ||
}) | ||
const expected = [ | ||
{ | ||
op: 'replace', | ||
path: '/name', | ||
value: 'baz', | ||
}, | ||
] | ||
expect(actual).toEqual(expected) | ||
}) | ||
it('should return a json-patch payload with add if value does not exist in originalValue', () => { | ||
const actual = createJsonPatchOperations({ | ||
dirtyFields: { | ||
name: true, | ||
}, | ||
originalValue: { | ||
id: 'foo', | ||
attributeValues: [], | ||
}, | ||
values: { | ||
name: 'baz', | ||
attributeValues: [], | ||
}, | ||
}) | ||
const expected = [ | ||
{ | ||
op: 'add', | ||
path: '/name', | ||
value: 'baz', | ||
}, | ||
] | ||
expect(actual).toEqual(expected) | ||
}) | ||
|
||
it('should handle attributeValues', () => { | ||
const actual = createJsonPatchOperations({ | ||
dirtyFields: { | ||
attributeValues: true, | ||
}, | ||
originalValue: { | ||
id: 'foo', | ||
name: 'bar', | ||
attributeValues: [], | ||
}, | ||
values: { | ||
name: 'baz', | ||
attributeValues: [ | ||
{ value: 'INPUT', attribute: { id: 'foo' } }, | ||
], | ||
}, | ||
}) | ||
const expected = [ | ||
{ | ||
op: 'replace', | ||
path: '/attributeValues', | ||
value: [{ value: 'INPUT', attribute: { id: 'foo' } }], | ||
}, | ||
] | ||
expect(actual).toEqual(expected) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters