-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.test.js
98 lines (77 loc) · 1.99 KB
/
index.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 {transform} from 'babel-core'
import removeDecoratorPlugin, {decoratableTypes} from './index.js'
const classDecorator = `
const TestDecorator = (target) => {
target.decoratedProperty = true
return target
}
@TestDecorator
class TestClass {}
return TestClass
`
test('has class decorators', () => {
const {code} = transform(classDecorator, {
plugins: ['transform-decorators-legacy'],
parserOpts: {
allowReturnOutsideFunction: true,
}
})
const fn = new Function(code)
expect(new fn().decoratedProperty).toBe(true)
})
test('strips class decorators', () => {
const {code} = transform(classDecorator, {
plugins: [removeDecoratorPlugin, 'transform-decorators-legacy'],
parserOpts: {
allowReturnOutsideFunction: true,
}
})
const fn = new Function(code)
expect(new fn().decoratedProperty).toBe(undefined)
})
const classFunctionDecorator = `
const TestDecorator = (target, prop) => {
target.decorated = true
return target
}
class TestClass {
@TestDecorator
a() {}
}
return new TestClass()
`
test('has class function decorator', () => {
const {code} = transform(classFunctionDecorator, {
plugins: ['transform-decorators-legacy'],
parserOpts: {
allowReturnOutsideFunction: true,
}
})
const fn = new Function(code)
expect(new fn().decorated).toBe(true)
})
test('strips class function decorator', () => {
const {code} = transform(classFunctionDecorator, {
plugins: [removeDecoratorPlugin, 'transform-decorators-legacy'],
parserOpts: {
allowReturnOutsideFunction: true,
}
})
const fn = new Function(code)
expect(fn().a.decorated).toBe(undefined)
})
test('decoratableTypes are as expected', () => {
expect(decoratableTypes).toEqual([
"ArrayPattern",
"AssignmentPattern",
"ClassExpression",
"ClassDeclaration",
"ClassMethod",
"ClassProperty",
"Identifier",
"ObjectMethod",
"ObjectPattern",
"ObjectProperty",
"RestElement"
])
})