forked from kazupon/vue-i18n
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath.test.js
98 lines (83 loc) · 2.51 KB
/
path.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
import I18nPath from '../../src/path'
describe('path', () => {
const path = new I18nPath()
describe('primitive', () => {
it('should get path value', () => {
assert.strictEqual(path.getPathValue({ a: { b: 1 } }, 'a.b'), 1)
})
})
describe('whitespace', () => {
it('should get value if it contains space 0x20', () => {
const val = path.getPathValue({ 'a c': 1 }, 'a c')
assert.strictEqual(val, 1)
})
it('should return null if it contains whitespace chars except space 0x20', () => {
const val = path.getPathValue({ 'a\tc': 1 }, 'a\tc')
assert.strictEqual(val, null)
})
})
describe('object', () => {
it('should get path value', () => {
const val = path.getPathValue({ a: { b: 1 } }, 'a')
assert.strictEqual(val.b, 1)
})
it('should accept space 0x20 as keypath', () => {
const val = path.getPathValue({ a: { 'b c d': 1 } }, 'a.b c d')
assert.strictEqual(val, 1)
})
})
describe('number key in object', () => {
it('should get path value', () => {
assert.strictEqual(
path.getPathValue({ errors: { '1': 'error number 1' } }, 'errors[1]'),
'error number 1'
)
})
})
describe('array index path', () => {
it('should get value', () => {
assert.strictEqual(
path.getPathValue({ errors: ['error number 0'] }, 'errors[0]'),
'error number 0'
)
})
})
describe('array path', () => {
it('should get path value', () => {
assert.strictEqual(
path.getPathValue({ errors: ['error number 0'] }, 'errors')[0],
'error number 0'
)
})
})
describe('not found', () => {
it('should not get null', () => {
assert.strictEqual(path.getPathValue({}, 'a.b'), null)
})
})
describe('obj: primitive', () => {
it('should not get null', () => {
assert.strictEqual(path.getPathValue(10, 'a.b'), null)
})
})
describe('obj: null', () => {
it('should not get null', () => {
assert.strictEqual(path.getPathValue(null, 'a.b'), null)
})
})
describe('Blanket: term', () => {
it('should not get null', () => {
assert.strictEqual(path.getPathValue({}, 'a.b.c[]'), null)
})
})
describe('Blanket: middle', () => {
it('should not get null', () => {
assert.strictEqual(path.getPathValue({}, 'a.b.c[]d'), null)
})
})
describe('obj: null child', () => {
it('should return null if parent is null', () => {
assert.strictEqual(path.getPathValue({ a: null }, 'a.b'), null)
})
})
})