This repository has been archived by the owner on Dec 12, 2020. It is now read-only.
/
class.spec.ts
78 lines (69 loc) · 2.53 KB
/
class.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
import { decorateClass, decorateMethod, reflect } from "../src"
describe("Class Introspection", () => {
it("Should inspect class properly", () => {
class DummyClass {
dummyMethod() { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})
it("Should inspect class with constructor parameters", () => {
class DummyClass {
constructor(id: number, name: string) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})
it("Should inspect class with method parameters", () => {
class DummyClass {
dummyMethod(dummy: string, other: any) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})
it("Should inspect constructor parameters type with decorator", () => {
@decorateClass({})
class DummyClass {
constructor(id: number, name: string) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})
it("Should inspect parameters type with method decorator", () => {
class DummyClass {
@decorateMethod({})
dummyMethod(dummy: string, other: any): number { return 1 }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})
it("Should inspect array on constructor parameters", () => {
class EmptyClass { }
@decorateClass({})
class DummyClass {
constructor(@reflect.array(EmptyClass) empty: EmptyClass[]) { }
}
const meta = reflect(DummyClass)
expect(meta).toMatchSnapshot()
})
it("Should inspect array on method parameter", () => {
class EmptyClass { }
class DummyClass {
@decorateMethod({})
dummyMethod(@reflect.array(EmptyClass) empty: EmptyClass[]) { }
}
const meta = reflect(DummyClass)
expect(meta.methods[0].parameters[0].type).toEqual([EmptyClass])
expect(meta).toMatchSnapshot()
})
it("Should able to override type with other type", () => {
class OtherDummyClass { }
class DummyClass {
method(@reflect.type(OtherDummyClass, "Readonly") dummy: Readonly<OtherDummyClass>) { }
}
const meta = reflect(DummyClass)
expect(meta.methods[0].parameters[0].decorators[0].type).toEqual(OtherDummyClass)
expect(meta.methods[0].parameters[0].decorators[0].info).toEqual("Readonly")
expect(meta).toMatchSnapshot()
})
})