-
Notifications
You must be signed in to change notification settings - Fork 434
/
Copy pathtest.ts
178 lines (154 loc) · 3.43 KB
/
test.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import Component, { createDecorator } from '../lib/index'
import { expect } from 'chai'
import * as Vue from 'vue'
describe('vue-class-component', () => {
it('hooks', () => {
let created = false
let destroyed = false
@Component
class MyComp extends Vue {
created () {
created = true
}
destroyed () {
destroyed = true
}
}
const c = new MyComp()
expect(created).to.be.true
expect(destroyed).to.be.false
c.$destroy()
expect(destroyed).to.be.true
})
it('data: should collect from class properties', () => {
@Component({
props: ['foo']
})
class MyComp extends Vue {
foo: number
a: string = 'hello'
b: number = this.foo + 1
}
const c = new MyComp({
propsData: {
foo: 1
}
})
expect(c.a).to.equal('hello')
expect(c.b).to.equal(2)
})
it('data: should collect custom property defined on beforeCreate', () => {
@Component
class MyComp extends Vue {
$store: any
foo: string = 'Hello, ' + this.$store.state.msg
beforeCreate () {
this.$store = {
state: {
msg: 'world'
}
}
}
}
const c = new MyComp()
expect(c.foo).to.equal('Hello, world')
})
it('methods', () => {
let msg: string = ''
@Component
class MyComp extends Vue {
hello () {
msg = 'hi'
}
}
const c = new MyComp()
c.hello()
expect(msg).to.equal('hi')
})
it('computed', () => {
@Component
class MyComp extends Vue {
a: number
data () {
return {
a: 1
}
}
get b () {
return this.a + 1
}
}
const c = new MyComp()
expect(c.a).to.equal(1)
expect(c.b).to.equal(2)
c.a = 2
expect(c.b).to.equal(3)
})
it('other options', (done) => {
let v: number
@Component<MyComp>({
watch: {
a: val => v = val
}
})
class MyComp extends Vue {
a: number
data () {
return { a: 1 }
}
}
const c = new MyComp()
c.a = 2
Vue.nextTick(() => {
expect(v).to.equal(2)
done()
})
})
it('extending', function () {
@Component
class Base extends Vue {
a: number
data (): any {
return { a: 1 }
}
}
@Component
class A extends Base {
b: number
data (): any {
return { b: 2 }
}
}
const a = new A()
expect(a.a).to.equal(1)
expect(a.b).to.equal(2)
})
it('createDecorator', function () {
const Prop = createDecorator((options, key) => {
// component options should be passed to the callback
// and update for the options affect the component
(options.props || (options.props = {}))[key] = true
})
const NoCache = createDecorator((options, key) => {
// options should have computed and methods etc.
// that specified by class property accessors and methods
const computedOption: Vue.ComputedOptions<Vue> = options.computed![key]
computedOption.cache = false
})
@Component
class MyComp extends Vue {
@Prop foo: string
@NoCache get bar (): string {
return 'world'
}
}
const c = new MyComp({
propsData: {
foo: 'hello'
}
})
expect(c.foo).to.equal('hello')
expect(c.bar).to.equal('world')
expect((MyComp as any).options.computed.bar.cache).to.be.false
})
})