2016-08-21 18:45:25 +08:00
|
|
|
import test from 'ava';
|
2016-09-09 17:01:06 +08:00
|
|
|
import {
|
2017-09-08 19:36:15 +08:00
|
|
|
jsonToEson, esonToJson, pathExists, transform, traverse,
|
2016-11-12 21:10:10 +08:00
|
|
|
parseJSONPointer, compileJSONPointer,
|
2017-09-22 17:38:23 +08:00
|
|
|
expand, addErrors, search, applySearchResults, nextSearchResult, previousSearchResult,
|
|
|
|
applySelection
|
2017-09-08 19:36:15 +08:00
|
|
|
} from '../src/eson'
|
2016-08-21 18:45:25 +08:00
|
|
|
|
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
// TODO: move all JSON documents in separate json files to keep the test readable?
|
|
|
|
|
2016-08-21 18:45:25 +08:00
|
|
|
const JSON_EXAMPLE = {
|
|
|
|
obj: {
|
2016-11-28 04:16:17 +08:00
|
|
|
arr: [1,2, {first:3,last:4}]
|
2016-08-21 18:45:25 +08:00
|
|
|
},
|
|
|
|
str: 'hello world',
|
|
|
|
nill: null,
|
|
|
|
bool: false
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
const ESON = {
|
2016-09-17 21:40:17 +08:00
|
|
|
type: 'Object',
|
2016-08-21 18:45:25 +08:00
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'obj',
|
|
|
|
value: {
|
2016-09-17 21:40:17 +08:00
|
|
|
type: 'Object',
|
2016-08-21 18:45:25 +08:00
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'arr',
|
|
|
|
value: {
|
2016-09-17 21:40:17 +08:00
|
|
|
type: 'Array',
|
2016-08-21 18:45:25 +08:00
|
|
|
expanded: true,
|
|
|
|
items: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 1
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 2
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'first',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'last',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 4
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
}
|
2017-01-06 20:57:16 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'str',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 'hello world'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'nill',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'bool',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
// TODO: instead of all slightly different copies of ESON, built them up via setIn, updateIn based on ESON
|
|
|
|
|
|
|
|
const ESON_COLLAPSED_1 = {
|
2017-09-08 19:36:15 +08:00
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
2017-09-22 17:38:23 +08:00
|
|
|
name: 'obj',
|
2017-09-08 19:36:15 +08:00
|
|
|
value: {
|
2017-09-22 17:38:23 +08:00
|
|
|
type: 'Object',
|
2017-09-08 19:36:15 +08:00
|
|
|
expanded: true,
|
2017-09-22 17:38:23 +08:00
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'arr',
|
|
|
|
value: {
|
|
|
|
type: 'Array',
|
|
|
|
expanded: true,
|
|
|
|
items: [
|
|
|
|
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: false,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'first',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'last',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-09-08 19:36:15 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
2017-09-22 17:38:23 +08:00
|
|
|
name: 'str',
|
2017-09-08 19:36:15 +08:00
|
|
|
value: {
|
|
|
|
type: 'value',
|
2017-09-22 17:38:23 +08:00
|
|
|
value: 'hello world'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'nill',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'bool',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: false
|
2017-09-08 19:36:15 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
const ESON_COLLAPSED_2 = {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'obj',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: false,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'arr',
|
|
|
|
value: {
|
|
|
|
type: 'Array',
|
|
|
|
expanded: false,
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: false,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'first',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'last',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'str',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 'hello world'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'nill',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'bool',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
// after search for 'L' (case insensitive)
|
|
|
|
const ESON_SEARCH_L = {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'obj',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'arr',
|
|
|
|
value: {
|
|
|
|
type: 'Array',
|
|
|
|
expanded: true,
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'first',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'last',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 4
|
|
|
|
},
|
|
|
|
searchResult: 'active'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'str',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 'hello world',
|
|
|
|
searchResult: 'normal'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'nill',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: null,
|
|
|
|
searchResult: 'normal'
|
|
|
|
},
|
|
|
|
searchResult: 'normal'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'bool',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: false,
|
|
|
|
searchResult: 'normal'
|
|
|
|
},
|
|
|
|
searchResult: 'normal'
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2017-01-06 20:57:16 +08:00
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
const ESON_SELECTED_OBJECT = {
|
2016-09-17 21:40:17 +08:00
|
|
|
type: 'Object',
|
2016-08-21 18:45:25 +08:00
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'obj',
|
|
|
|
value: {
|
2016-09-17 21:40:17 +08:00
|
|
|
type: 'Object',
|
2016-08-21 18:45:25 +08:00
|
|
|
expanded: true,
|
2017-09-22 17:38:23 +08:00
|
|
|
selected: true,
|
2016-08-21 18:45:25 +08:00
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'arr',
|
|
|
|
value: {
|
2016-09-17 21:40:17 +08:00
|
|
|
type: 'Array',
|
2016-08-21 18:45:25 +08:00
|
|
|
expanded: true,
|
|
|
|
items: [
|
2017-09-22 17:38:23 +08:00
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 1
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 2
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'first',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'last',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'str',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 'hello world',
|
|
|
|
selected: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'nill',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: null,
|
|
|
|
selected: true
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'bool',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2017-01-06 20:57:16 +08:00
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
const ESON_SELECTED_ARRAY = {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'obj',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'arr',
|
|
|
|
value: {
|
|
|
|
type: 'Array',
|
|
|
|
expanded: true,
|
|
|
|
items: [
|
2016-08-21 18:45:25 +08:00
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
2017-09-22 17:38:23 +08:00
|
|
|
selected: true,
|
2017-01-06 20:57:16 +08:00
|
|
|
value: 1
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
2017-09-22 17:38:23 +08:00
|
|
|
selected: true,
|
2017-01-06 20:57:16 +08:00
|
|
|
value: 2
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
2017-09-22 17:38:23 +08:00
|
|
|
expanded: true,
|
2017-01-06 20:57:16 +08:00
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'first',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'last',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 4
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
}
|
2017-01-06 20:57:16 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'str',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 'hello world'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'nill',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'bool',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
const ESON_SELECTED_VALUE = {
|
2016-09-17 21:40:17 +08:00
|
|
|
type: 'Object',
|
2016-08-21 18:45:25 +08:00
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'obj',
|
|
|
|
value: {
|
2016-09-17 21:40:17 +08:00
|
|
|
type: 'Object',
|
2017-09-22 17:38:23 +08:00
|
|
|
expanded: true,
|
2016-08-21 18:45:25 +08:00
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'arr',
|
|
|
|
value: {
|
2016-09-17 21:40:17 +08:00
|
|
|
type: 'Array',
|
2017-09-22 17:38:23 +08:00
|
|
|
expanded: true,
|
2016-08-21 18:45:25 +08:00
|
|
|
items: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 1
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 2
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
2017-09-22 17:38:23 +08:00
|
|
|
expanded: true,
|
2017-01-06 20:57:16 +08:00
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'first',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
2017-09-22 17:38:23 +08:00
|
|
|
selected: true,
|
2017-01-06 20:57:16 +08:00
|
|
|
value: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'last',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 4
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
}
|
2017-01-06 20:57:16 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2016-08-21 18:45:25 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'str',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 'hello world'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'nill',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-08-21 18:45:25 +08:00
|
|
|
name: 'bool',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
const ESON_SELECTED_PARENT = {
|
2016-11-20 04:21:09 +08:00
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-20 04:21:09 +08:00
|
|
|
name: 'obj',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-20 04:21:09 +08:00
|
|
|
name: 'arr',
|
|
|
|
value: {
|
|
|
|
type: 'Array',
|
|
|
|
expanded: true,
|
|
|
|
items: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 1
|
|
|
|
}
|
2016-11-20 04:21:09 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 2
|
|
|
|
}
|
2016-11-20 04:21:09 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
2017-09-22 17:38:23 +08:00
|
|
|
selected: true,
|
2017-01-06 20:57:16 +08:00
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'first',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 3
|
|
|
|
}
|
2016-12-30 20:45:43 +08:00
|
|
|
},
|
2017-01-06 20:57:16 +08:00
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'last',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 4
|
2017-09-22 17:38:23 +08:00
|
|
|
}
|
2017-01-06 20:57:16 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2016-11-28 04:16:17 +08:00
|
|
|
}
|
2016-11-20 04:21:09 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-20 04:21:09 +08:00
|
|
|
name: 'str',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
2017-09-22 17:38:23 +08:00
|
|
|
value: 'hello world'
|
2016-11-20 04:21:09 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-20 04:21:09 +08:00
|
|
|
name: 'nill',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
2017-09-22 17:38:23 +08:00
|
|
|
value: null
|
|
|
|
}
|
2016-11-20 04:21:09 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-20 04:21:09 +08:00
|
|
|
name: 'bool',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
2017-09-22 17:38:23 +08:00
|
|
|
value: false
|
|
|
|
}
|
2016-11-20 04:21:09 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
const ESON_SMALL = {
|
2016-11-20 04:21:09 +08:00
|
|
|
type: 'Object',
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-20 04:21:09 +08:00
|
|
|
name: 'obj',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-20 04:21:09 +08:00
|
|
|
name: 'a',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-20 04:21:09 +08:00
|
|
|
name: 'arr',
|
|
|
|
value: {
|
|
|
|
type: 'Array',
|
|
|
|
items: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 3
|
|
|
|
}
|
2016-11-20 04:21:09 +08:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-12 21:10:10 +08:00
|
|
|
const JSON_SCHEMA_ERRORS = [
|
2017-09-19 03:05:26 +08:00
|
|
|
{dataPath: '/obj/arr/2/last', message: 'String expected'},
|
|
|
|
{dataPath: '/nill', message: 'Null expected'}
|
2016-11-12 21:10:10 +08:00
|
|
|
]
|
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
const ESON_ERRORS = {
|
2016-11-12 21:10:10 +08:00
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-12 21:10:10 +08:00
|
|
|
name: 'obj',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-12 21:10:10 +08:00
|
|
|
name: 'arr',
|
|
|
|
value: {
|
|
|
|
type: 'Array',
|
|
|
|
expanded: true,
|
|
|
|
items: [
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 1
|
|
|
|
}
|
2016-11-12 21:10:10 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 2
|
|
|
|
}
|
2016-11-12 21:10:10 +08:00
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
|
|
|
value: {
|
|
|
|
type: 'Object',
|
|
|
|
expanded: true,
|
|
|
|
props: [
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'first',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 3
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: '[ID]',
|
|
|
|
name: 'last',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 4,
|
|
|
|
error: JSON_SCHEMA_ERRORS[0]
|
|
|
|
}
|
2016-11-12 21:10:10 +08:00
|
|
|
}
|
2017-01-06 20:57:16 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2016-11-12 21:10:10 +08:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-12 21:10:10 +08:00
|
|
|
name: 'str',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: 'hello world'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-12 21:10:10 +08:00
|
|
|
name: 'nill',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: null,
|
|
|
|
error: JSON_SCHEMA_ERRORS[1]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-01-06 20:57:16 +08:00
|
|
|
id: '[ID]',
|
2016-11-12 21:10:10 +08:00
|
|
|
name: 'bool',
|
|
|
|
value: {
|
|
|
|
type: 'value',
|
|
|
|
value: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2017-09-08 19:36:15 +08:00
|
|
|
test('jsonToEson', t => {
|
2016-08-21 18:45:25 +08:00
|
|
|
function expand (path) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2017-09-08 19:36:15 +08:00
|
|
|
const ESON = jsonToEson(JSON_EXAMPLE, expand, [])
|
|
|
|
replaceIds(ESON)
|
2017-01-06 20:57:16 +08:00
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
t.deepEqual(ESON, ESON)
|
2016-08-21 18:45:25 +08:00
|
|
|
})
|
|
|
|
|
2017-09-08 19:36:15 +08:00
|
|
|
test('esonToJson', t => {
|
2017-09-22 17:38:23 +08:00
|
|
|
t.deepEqual(esonToJson(ESON), JSON_EXAMPLE)
|
2016-08-21 18:45:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('expand a single path', t => {
|
2017-09-22 17:38:23 +08:00
|
|
|
const collapsed = expand(ESON, ['obj', 'arr', 2], false)
|
2016-08-21 18:45:25 +08:00
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
t.deepEqual(collapsed, ESON_COLLAPSED_1)
|
2016-08-21 18:45:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('expand a callback', t => {
|
|
|
|
function callback (path) {
|
|
|
|
return path.length >= 1
|
|
|
|
}
|
|
|
|
const expanded = false
|
2017-09-22 17:38:23 +08:00
|
|
|
const collapsed = expand(ESON, callback, expanded)
|
2016-08-21 18:45:25 +08:00
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
t.deepEqual(collapsed, ESON_COLLAPSED_2)
|
2016-08-21 18:45:25 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('expand a callback should not change the object when nothing happens', t => {
|
|
|
|
function callback (path) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const expanded = false
|
2017-09-22 17:38:23 +08:00
|
|
|
const collapsed = expand(ESON, callback, expanded)
|
2016-08-21 18:45:25 +08:00
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
t.is(collapsed, ESON)
|
2016-08-21 18:45:25 +08:00
|
|
|
})
|
|
|
|
|
2016-09-09 17:01:06 +08:00
|
|
|
test('pathExists', t => {
|
2017-09-22 17:38:23 +08:00
|
|
|
t.is(pathExists(ESON, ['obj', 'arr', 2, 'first']), true)
|
|
|
|
t.is(pathExists(ESON, ['obj', 'foo']), false)
|
|
|
|
t.is(pathExists(ESON, ['obj', 'foo', 'bar']), false)
|
|
|
|
t.is(pathExists(ESON, []), true)
|
2016-09-09 17:01:06 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('parseJSONPointer', t => {
|
|
|
|
t.deepEqual(parseJSONPointer('/obj/a'), ['obj', 'a'])
|
|
|
|
t.deepEqual(parseJSONPointer('/arr/-'), ['arr', '-'])
|
|
|
|
t.deepEqual(parseJSONPointer('/foo/~1~0 ~0~1'), ['foo', '/~ ~/'])
|
2016-10-18 02:38:35 +08:00
|
|
|
t.deepEqual(parseJSONPointer('/obj'), ['obj'])
|
2016-10-28 17:04:06 +08:00
|
|
|
t.deepEqual(parseJSONPointer('/'), [''])
|
|
|
|
t.deepEqual(parseJSONPointer(''), [])
|
2016-09-09 17:01:06 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('compileJSONPointer', t => {
|
|
|
|
t.deepEqual(compileJSONPointer(['foo', 'bar']), '/foo/bar')
|
|
|
|
t.deepEqual(compileJSONPointer(['foo', '/~ ~/']), '/foo/~1~0 ~0~1')
|
2016-10-28 17:04:06 +08:00
|
|
|
t.deepEqual(compileJSONPointer(['']), '/')
|
|
|
|
t.deepEqual(compileJSONPointer([]), '')
|
2016-09-09 17:01:06 +08:00
|
|
|
})
|
|
|
|
|
2016-11-12 21:10:10 +08:00
|
|
|
test('add and remove errors', t => {
|
2017-09-22 17:38:23 +08:00
|
|
|
const dataWithErrors = addErrors(ESON, JSON_SCHEMA_ERRORS)
|
|
|
|
t.deepEqual(dataWithErrors, ESON_ERRORS)
|
2016-11-20 03:44:58 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('transform', t => {
|
|
|
|
// {obj: {a: 2}, arr: [3]}
|
2017-01-06 20:57:16 +08:00
|
|
|
|
2016-11-20 03:44:58 +08:00
|
|
|
let log = []
|
2017-09-22 17:38:23 +08:00
|
|
|
const transformed = transform(ESON_SMALL, function (value, path, root) {
|
|
|
|
t.is(root, ESON_SMALL)
|
2016-11-20 03:44:58 +08:00
|
|
|
|
|
|
|
log.push([value, path, root])
|
|
|
|
|
|
|
|
if (path.length === 2 && path[0] === 'obj' && path[1] === 'a') {
|
|
|
|
// change the value
|
|
|
|
return { type: 'value', value: 42 }
|
|
|
|
}
|
|
|
|
|
|
|
|
// leave the value unchanged
|
|
|
|
return value
|
|
|
|
})
|
|
|
|
|
|
|
|
// console.log('transformed', JSON.stringify(transformed, null, 2))
|
|
|
|
|
|
|
|
const EXPECTED_LOG = [
|
2017-09-22 17:38:23 +08:00
|
|
|
[ESON_SMALL, [], ESON_SMALL],
|
|
|
|
[ESON_SMALL.props[0].value, ['obj'], ESON_SMALL],
|
|
|
|
[ESON_SMALL.props[0].value.props[0].value, ['obj', 'a'], ESON_SMALL],
|
|
|
|
[ESON_SMALL.props[1].value, ['arr'], ESON_SMALL],
|
|
|
|
[ESON_SMALL.props[1].value.items[0].value, ['arr', '0'], ESON_SMALL],
|
2016-11-20 03:44:58 +08:00
|
|
|
]
|
|
|
|
|
2017-01-06 20:57:16 +08:00
|
|
|
log.forEach((row, index) => {
|
|
|
|
t.deepEqual(log[index], EXPECTED_LOG[index], 'should have equal log at index ' + index )
|
|
|
|
})
|
2016-11-20 03:44:58 +08:00
|
|
|
t.deepEqual(log, EXPECTED_LOG)
|
2017-09-22 17:38:23 +08:00
|
|
|
t.not(transformed, ESON_SMALL)
|
|
|
|
t.not(transformed.props[0].value, ESON_SMALL.props[0].value)
|
|
|
|
t.not(transformed.props[0].value.props[0].value, ESON_SMALL.props[0].value.props[0].value)
|
|
|
|
t.is(transformed.props[1].value, ESON_SMALL.props[1].value)
|
|
|
|
t.is(transformed.props[1].value.items[0].value, ESON_SMALL.props[1].value.items[0].value)
|
2016-11-20 04:21:09 +08:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-11-28 04:16:17 +08:00
|
|
|
test('traverse', t => {
|
|
|
|
// {obj: {a: 2}, arr: [3]}
|
|
|
|
|
|
|
|
let log = []
|
2017-09-22 17:38:23 +08:00
|
|
|
const returnValue = traverse(ESON_SMALL, function (value, path, root) {
|
|
|
|
t.is(root, ESON_SMALL)
|
2016-11-28 04:16:17 +08:00
|
|
|
|
|
|
|
log.push([value, path, root])
|
|
|
|
})
|
|
|
|
|
|
|
|
t.is(returnValue, undefined)
|
|
|
|
|
|
|
|
const EXPECTED_LOG = [
|
2017-09-22 17:38:23 +08:00
|
|
|
[ESON_SMALL, [], ESON_SMALL],
|
|
|
|
[ESON_SMALL.props[0].value, ['obj'], ESON_SMALL],
|
|
|
|
[ESON_SMALL.props[0].value.props[0].value, ['obj', 'a'], ESON_SMALL],
|
|
|
|
[ESON_SMALL.props[1].value, ['arr'], ESON_SMALL],
|
|
|
|
[ESON_SMALL.props[1].value.items[0].value, ['arr', '0'], ESON_SMALL],
|
2016-11-28 04:16:17 +08:00
|
|
|
]
|
|
|
|
|
2017-01-06 20:57:16 +08:00
|
|
|
log.forEach((row, index) => {
|
|
|
|
t.deepEqual(log[index], EXPECTED_LOG[index], 'should have equal log at index ' + index )
|
|
|
|
})
|
2016-11-28 04:16:17 +08:00
|
|
|
t.deepEqual(log, EXPECTED_LOG)
|
|
|
|
})
|
|
|
|
|
2016-11-20 04:21:09 +08:00
|
|
|
|
|
|
|
test('search', t => {
|
2017-09-22 17:38:23 +08:00
|
|
|
const searchResults = search(ESON, 'L')
|
2017-01-06 20:57:16 +08:00
|
|
|
// printJSON(searchResults)
|
2016-11-28 04:16:17 +08:00
|
|
|
|
|
|
|
t.deepEqual(searchResults, [
|
2017-09-22 17:38:23 +08:00
|
|
|
{path: ['obj', 'arr', '2', 'last'], field: 'property'},
|
|
|
|
{path: ['str'], field: 'value'},
|
|
|
|
{path: ['nill'], field: 'property'},
|
|
|
|
{path: ['nill'], field: 'value'},
|
|
|
|
{path: ['bool'], field: 'property'},
|
|
|
|
{path: ['bool'], field: 'value'}
|
2016-11-28 04:16:17 +08:00
|
|
|
])
|
|
|
|
|
2016-12-30 17:44:57 +08:00
|
|
|
const activeSearchResult = searchResults[0]
|
2017-09-22 17:38:23 +08:00
|
|
|
const updatedData = applySearchResults(ESON, searchResults, activeSearchResult)
|
2017-01-06 20:57:16 +08:00
|
|
|
// printJSON(updatedData)
|
2016-11-12 21:10:10 +08:00
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
t.deepEqual(updatedData, ESON_SEARCH_L)
|
2016-11-12 21:10:10 +08:00
|
|
|
})
|
2016-12-30 21:05:11 +08:00
|
|
|
|
|
|
|
test('nextSearchResult', t => {
|
|
|
|
const searchResults = [
|
2017-09-22 17:38:23 +08:00
|
|
|
{path: ['obj', 'arr', '2', 'last'], field: 'property'},
|
|
|
|
{path: ['str'], field: 'value'},
|
|
|
|
{path: ['nill'], field: 'property'},
|
|
|
|
{path: ['nill'], field: 'value'},
|
|
|
|
{path: ['bool'], field: 'property'},
|
|
|
|
{path: ['bool'], field: 'value'}
|
2016-12-30 21:05:11 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
t.deepEqual(nextSearchResult(searchResults,
|
2017-09-22 17:38:23 +08:00
|
|
|
{path: ['nill'], field: 'property'}),
|
|
|
|
{path: ['nill'], field: 'value'})
|
2016-12-30 21:05:11 +08:00
|
|
|
|
|
|
|
// wrap around
|
|
|
|
t.deepEqual(nextSearchResult(searchResults,
|
2017-09-22 17:38:23 +08:00
|
|
|
{path: ['bool'], field: 'value'}),
|
|
|
|
{path: ['obj', 'arr', '2', 'last'], field: 'property'})
|
2016-12-30 21:05:11 +08:00
|
|
|
|
|
|
|
// return first when current is not found
|
|
|
|
t.deepEqual(nextSearchResult(searchResults,
|
2017-09-22 17:38:23 +08:00
|
|
|
{path: ['non', 'existing'], field: 'value'}),
|
|
|
|
{path: ['obj', 'arr', '2', 'last'], field: 'property'})
|
2016-12-30 21:05:11 +08:00
|
|
|
|
|
|
|
// return null when searchResults are empty
|
2017-09-22 17:38:23 +08:00
|
|
|
t.deepEqual(nextSearchResult([], {path: ['non', 'existing'], field: 'value'}), null)
|
2016-12-30 21:05:11 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
test('previousSearchResult', t => {
|
|
|
|
const searchResults = [
|
2017-01-05 22:15:53 +08:00
|
|
|
{path: ['obj', 'arr', '2', 'last'], type: 'property'},
|
|
|
|
{path: ['str'], type: 'value'},
|
|
|
|
{path: ['nill'], type: 'property'},
|
|
|
|
{path: ['nill'], type: 'value'},
|
|
|
|
{path: ['bool'], type: 'property'},
|
|
|
|
{path: ['bool'], type: 'value'}
|
2016-12-30 21:05:11 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
t.deepEqual(previousSearchResult(searchResults,
|
2017-01-05 22:15:53 +08:00
|
|
|
{path: ['nill'], type: 'property'}),
|
|
|
|
{path: ['str'], type: 'value'})
|
2016-12-30 21:05:11 +08:00
|
|
|
|
|
|
|
// wrap around
|
|
|
|
t.deepEqual(previousSearchResult(searchResults,
|
2017-01-05 22:15:53 +08:00
|
|
|
{path: ['obj', 'arr', '2', 'last'], type: 'property'}),
|
|
|
|
{path: ['bool'], type: 'value'})
|
2016-12-30 21:05:11 +08:00
|
|
|
|
|
|
|
// return first when current is not found
|
|
|
|
t.deepEqual(previousSearchResult(searchResults,
|
2017-01-05 22:15:53 +08:00
|
|
|
{path: ['non', 'existing'], type: 'value'}),
|
|
|
|
{path: ['obj', 'arr', '2', 'last'], type: 'property'})
|
2016-12-30 21:05:11 +08:00
|
|
|
|
|
|
|
// return null when searchResults are empty
|
2017-01-05 22:15:53 +08:00
|
|
|
t.deepEqual(previousSearchResult([], {path: ['non', 'existing'], type: 'value'}), null)
|
2016-12-30 21:05:11 +08:00
|
|
|
})
|
2017-01-06 20:57:16 +08:00
|
|
|
|
2017-09-22 17:38:23 +08:00
|
|
|
test('selection (object)', t => {
|
|
|
|
const selection = {
|
|
|
|
start: {path: ['obj', 'arr', '2', 'last']},
|
|
|
|
end: {path: ['nill']}
|
|
|
|
}
|
|
|
|
const result = applySelection(ESON, selection)
|
|
|
|
|
|
|
|
t.deepEqual(result, ESON_SELECTED_OBJECT)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('selection (array)', t => {
|
|
|
|
const selection = {
|
|
|
|
start: {path: ['obj', 'arr', '1']},
|
|
|
|
end: {path: ['obj', 'arr', '0']} // note the "wrong" order of start and end
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = applySelection(ESON, selection)
|
|
|
|
|
|
|
|
t.deepEqual(result, ESON_SELECTED_ARRAY)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('selection (value)', t => {
|
|
|
|
const selection = {
|
|
|
|
start: {path: ['obj', 'arr', '2', 'first']},
|
|
|
|
end: {path: ['obj', 'arr', '2', 'first']}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = applySelection(ESON, selection)
|
|
|
|
|
|
|
|
t.deepEqual(result, ESON_SELECTED_VALUE)
|
|
|
|
})
|
|
|
|
|
|
|
|
test('selection (single parent)', t => {
|
|
|
|
const selection = {
|
|
|
|
start: {path: ['obj', 'arr', '2']},
|
|
|
|
end: {path: ['obj', 'arr', '2']}
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = applySelection(ESON, selection)
|
|
|
|
|
|
|
|
t.deepEqual(result, ESON_SELECTED_PARENT)
|
|
|
|
})
|
|
|
|
|
2017-01-06 20:57:16 +08:00
|
|
|
// helper function to replace all id properties with a constant value
|
|
|
|
function replaceIds (data, value = '[ID]') {
|
|
|
|
if (data.type === 'Object') {
|
|
|
|
data.props.forEach(prop => {
|
|
|
|
prop.id = value
|
|
|
|
replaceIds(prop.value, value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data.type === 'Array') {
|
|
|
|
data.items.forEach(item => {
|
|
|
|
item.id = value
|
|
|
|
replaceIds(item.value, value)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// helper function to print JSON in the console
|
|
|
|
function printJSON (json, message = null) {
|
|
|
|
if (message) {
|
|
|
|
console.log(message)
|
|
|
|
}
|
|
|
|
console.log(JSON.stringify(json, null, 2))
|
2017-09-08 17:14:41 +08:00
|
|
|
}
|