-
-
Notifications
You must be signed in to change notification settings - Fork 200
Expand file tree
/
Copy pathtest.api.js
More file actions
96 lines (92 loc) · 3.29 KB
/
test.api.js
File metadata and controls
96 lines (92 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
describe('JSONPath - API', function () {
// tests based on examples at http://goessner.net/articles/jsonpath/
const json = {
"store": {
"book": [{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}],
"bicycle": {
"color": "red",
"price": 19.95
}
}
};
it('should test non-object argument of constructor', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
let result = jsonpath('$.store.book[*].author', json);
assert.deepEqual(result, expected);
result = jsonpath({json, path: 'store.book[*].author'});
assert.deepEqual(result, expected);
});
it('should test array path of constructor', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
let result = jsonpath({path: ['$', 'store', 'book', '*', 'author'], json});
assert.deepEqual(result, expected);
result = jsonpath({json, path: 'store.book[*].author'});
assert.deepEqual(result, expected);
});
it('should test defaults on manual `evaluate` with `autostart: false`', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
let jp = jsonpath({
path: '$.store.book[*].author',
json,
autostart: false
});
let result = jp.evaluate();
assert.deepEqual(result, expected);
jp = jsonpath({
json,
path: 'store.book[*].author',
autostart: false
});
result = jp.evaluate();
assert.deepEqual(result, expected);
});
it('should test defaults with `evaluate` object and `autostart: false`', () => {
const books = json.store.book;
const expected = [books[0].author, books[1].author, books[2].author, books[3].author];
const jp = jsonpath({
autostart: false
});
const result = jp.evaluate({
json,
path: '$.store.book[*].author',
sandbox: {category: 'reference'},
eval: false,
flatten: true,
wrap: false,
resultType: 'value',
callback () { /* */ },
parent: null,
parentProperty: null,
otherTypeCallback () { /* */ }
});
assert.deepEqual(result, expected);
});
});