-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.spec.ts
85 lines (69 loc) · 2.98 KB
/
index.spec.ts
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
import {expect} from 'chai';
import {typedPath as tp, typedPath} from './index';
const sym = Symbol("SomeSymbol");
type TestType = {
a: {
testFunc: () => { result: string },
b: {
arrayOfArrays: string[][]
c: number;
f: { test: string, blah: { path?: string }, arr: string[] }[];
}
[sym]: {
g: string,
}
}
};
interface OptionalThing {
foo: string;
bar?: string;
}
const testAdditionalHandlers = {
$abs: (path: string[]) => typedPath<TestType, typeof testAdditionalHandlers>(testAdditionalHandlers, ['', ...path]),
$url: (path: string[]) => path.join('/')
}
describe('Typed path', () => {
it('should get field path', () => {
expect(typedPath<TestType>().a.b.c.$path).to.equal('a.b.c');
expect(typedPath<TestType>().a.b.c.$raw).to.deep.equal(['a', 'b', 'c']);
});
it('should get index path', () => {
expect(tp<TestType>().a.b.f[3].$path).to.equal('a.b.f[3]');
expect(tp<TestType>().a.b.f[3].$raw).to.deep.equal(['a', 'b', 'f', "3"]);
});
it('should get index of index for array of array ', () => {
expect(tp<TestType>().a.b.arrayOfArrays[3][3].$path).to.equal('a.b.arrayOfArrays[3][3]');
expect(tp<TestType>().a.b.arrayOfArrays[3][3].$raw).to.deep.equal(['a', 'b', 'arrayOfArrays', "3", "3"]);
});
it('should get array node', () => {
expect(tp<TestType>().a.b.f.$path).to.equal('a.b.f');
expect(tp<TestType>().a.b.f.$raw).to.deep.equal(['a', 'b', 'f']);
});
it('should get function return type path', () => {
expect(tp<TestType>().a.testFunc.result.$path).to.equal('a.testFunc.result');
expect(tp<TestType>().a.testFunc.result.$raw).to.deep.equal(['a', 'testFunc', 'result']);
});
it('should get function path', () => {
expect(tp<TestType>().a.testFunc.$path).to.equal('a.testFunc');
expect(tp<TestType>().a.testFunc.$raw).to.deep.equal(['a', 'testFunc']);
});
it('should get path with optional fields', () => {
expect(tp<OptionalThing>().bar.$path).to.equal('bar');
});
it('should get path with symbol', () => {
expect(tp<TestType>().a[sym].g.$path).to.equal('a.Symbol(SomeSymbol).g');
expect(tp<TestType>().a[sym].g.$raw).to.deep.equal(['a', 'Symbol(SomeSymbol)', 'g']);
});
it('should get path for toString()', () => {
expect(tp<TestType>().a.b.f[3].blah.path.toString()).to.equal('a.b.f[3].blah.path');
});
it('should get path for valueOf()', () => {
expect(tp<TestType>().a.b.f[3].blah.path.valueOf()).to.equal('a.b.f[3].blah.path');
});
it('should work with extended handlers', () => {
expect(tp<TestType, typeof testAdditionalHandlers>(testAdditionalHandlers).a.b.c.$url).to.equal('a/b/c');
})
it('should work with chained extended handlers', () => {
expect(tp<TestType, typeof testAdditionalHandlers>(testAdditionalHandlers).a.b.c.$abs.$url).to.equal('/a/b/c');
})
});