Fixed unit tests

This commit is contained in:
jos 2018-09-05 11:33:18 +02:00
parent 41a7069398
commit e2ffd04e6e
3 changed files with 16 additions and 14 deletions

View File

@ -23,9 +23,6 @@ it('sort root Array (json)', () => {
const sorted2 = immutableJSONPatch(json, sort(json, [], 'asc')).json const sorted2 = immutableJSONPatch(json, sort(json, [], 'asc')).json
const sorted3 = immutableJSONPatch(json, sort(json, [], 'desc')).json const sorted3 = immutableJSONPatch(json, sort(json, [], 'desc')).json
console.log(json)
console.log(sorted1)
assertEqualEson(json, [1,3, 2]) // should be untouched assertEqualEson(json, [1,3, 2]) // should be untouched
assertEqualEson(sorted1, [1,2,3]) assertEqualEson(sorted1, [1,2,3])
assertEqualEson(sorted2, [1,2,3]) assertEqualEson(sorted2, [1,2,3])

View File

@ -428,13 +428,14 @@ export function findSelectionIndices (root, rootPath, selection) {
const end = (selection.after || selection.inside || selection.end)[rootPath.length] const end = (selection.after || selection.inside || selection.end)[rootPath.length]
// if no object we assume it's an Array // if no object we assume it's an Array
const props = Object.keys(root).sort(naturalSort) // TODO: create a util function getSortedProps // TODO: create a util function getSortedProps, cache results?
const startIndex = root[TYPE] === 'object' ? props.indexOf(start) : parseInt(start, 10) const rootIsObject = getType(root) === 'object'
const endIndex = root[TYPE] === 'object' ? props.indexOf(end) : parseInt(end, 10) const props = rootIsObject ? Object.keys(root).sort(naturalSort) : undefined
const startIndex = rootIsObject ? props.indexOf(start) : parseInt(start, 10)
const endIndex = rootIsObject ? props.indexOf(end) : parseInt(end, 10)
const minIndex = Math.min(startIndex, endIndex) const minIndex = Math.min(startIndex, endIndex)
const maxIndex = Math.max(startIndex, endIndex) + const maxIndex = Math.max(startIndex, endIndex) + ((selection.after || selection.inside) ? 0 : 1) // include max index itself
((selection.after || selection.inside) ? 0 : 1) // include max index itself
return { minIndex, maxIndex } return { minIndex, maxIndex }
} }
@ -519,8 +520,9 @@ export function pathsFromSelection (eson, selection) {
const { minIndex, maxIndex } = findSelectionIndices(root, rootPath, selection) const { minIndex, maxIndex } = findSelectionIndices(root, rootPath, selection)
if (root[TYPE] === 'object') { if (getType(root) === 'object') {
const props = Object.keys(root).sort(naturalSort) // TODO: create a util function getSortedProps const props = Object.keys(root).sort(naturalSort) // TODO: create a util function getSortedProps
return times(maxIndex - minIndex, i => rootPath.concat(props[i + minIndex])) return times(maxIndex - minIndex, i => rootPath.concat(props[i + minIndex]))
} }
else { // root[TYPE] === 'array' else { // root[TYPE] === 'array'
@ -550,6 +552,10 @@ export function getType (any) {
return 'undefined' return 'undefined'
} }
if (any && any[TYPE]) {
return any[TYPE]
}
if (Array.isArray(any)) { if (Array.isArray(any)) {
return 'array' return 'array'
} }

View File

@ -447,12 +447,12 @@ test('selection (node)', () => {
test('pathsFromSelection (object)', () => { test('pathsFromSelection (object)', () => {
const json = { const json = {
"bool": false,
"nill": null,
"obj": { "obj": {
"arr": [1,2, {"first":3,"last":4}] "arr": [1,2, {"first":3,"last":4}]
}, },
"str": "hello world", "str": "hello world",
"nill": null,
"bool": false
} }
const selection = { const selection = {
start: ['obj', 'arr', '2', 'last'], start: ['obj', 'arr', '2', 'last'],
@ -460,9 +460,8 @@ test('pathsFromSelection (object)', () => {
} }
expect(pathsFromSelection(json, selection)).toEqual([ expect(pathsFromSelection(json, selection)).toEqual([
['obj'], ['nill'],
['str'], ['obj']
['nill']
]) ])
}) })