/
test.js
103 lines (90 loc) · 2.23 KB
/
test.js
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
97
98
99
100
101
102
103
const state = {
self: 'self',
one: 1,
two: 2,
numbers: {
one: 1,
two: 2,
},
literal: {
one: 'two',
two: 'one',
},
literalByNum: {
1: 'one',
2: 'two',
},
}
const store = {
getFromRoot: (key) => state[key],
}
const describe = (name, uselessfn) =>
uselessfn((description, fn) => {
try {
fn()
console.log(`[GOOD] it ${description}`)
} catch (err) {
console.log(`[ BAD] it ${description}`)
console.log(err.stack)
}
})
const expect = value => ({
toBe: otherval => {
if (value !== otherval) {
throw new Error(`Expected ${value} to be ${otherval}`)
}
},
toThrow: _ => {
try {
value()
} catch (err) {
return
}
throw new Error(`Expected ${value} to throw`)
},
})
const simpleQuery = require('./dist/simplequery')
// describe('simplequery - tokenize', it => {
// it('should return the right tokens', _ => {
// expect(simpleQuery('header')(store)).toHaveTokens
// })
// })
describe('simplequery - get from store', it => {
it('should find single depth value', _ => {
expect(simpleQuery('one')(store)).toBe(1)
expect(simpleQuery('numbers')(store)).toBe(state.numbers)
})
it('should find double depth key', _ => {
expect(simpleQuery('numbers.one')(store)).toBe(1)
expect(simpleQuery('literal.two')(store)).toBe('one')
})
it('should find by other value', _ => {
expect(simpleQuery('literalByNum[numbers.one]')(store)).toBe('one')
})
it('should support a lot of nesting', _ => {
expect(simpleQuery('literal[literal[literal[literal[literal.one]]]]')(store)).toBe('two')
})
it('should support a numeric key', _ => {
expect(simpleQuery('literalByNum.1')(store)).toBe('one')
})
it('should support derived key access on the root', _ => {
expect(simpleQuery('[literalByNum.1]')(store)).toBe(1)
})
it('should support derived key access on the root xtreme', _ => {
expect(simpleQuery('[[[[[self]]]]]')(store)).toBe('self')
})
const badQueries = [
'[]',
'',
'.test',
'[.test]',
'test[',
']',
'[good]bad',
]
badQueries.forEach(query => {
it(`should throw with query '${query}'`, _ => {
expect(() => simpleQuery(query)).toThrow()
})
})
})