@@ -2,23 +2,107 @@ import 'reflect-metadata'
2
2
import { h } from 'vue'
3
3
import { Options , Vue } from 'vue-class-component'
4
4
import { mount } from '../../src'
5
+ import ClassComponent from '../components/ClassComponent.vue'
5
6
6
- test ( 'data: $props should be available' , ( ) => {
7
- @Options ( {
8
- props : [ 'foo' ]
7
+ describe ( 'class component' , ( ) => {
8
+ it ( 'minimal class component' , ( ) => {
9
+ class Foo extends Vue {
10
+ render ( ) {
11
+ return h ( 'span' )
12
+ }
13
+ }
14
+ const wrapper = mount ( Foo )
15
+ expect ( wrapper . find ( 'span' ) . exists ( ) ) . toBe ( true )
9
16
} )
10
- class MyComp extends Vue {
11
- message = 'answer is ' + ( this . $props as any ) . foo
12
- render ( ) {
13
- return h ( 'div' , this . message )
17
+
18
+ it ( 'data: $props should be available' , ( ) => {
19
+ @Options ( {
20
+ props : [ 'foo' ]
21
+ } )
22
+ class MyComp extends Vue {
23
+ message = 'answer is ' + ( this . $props as any ) . foo
24
+ render ( ) {
25
+ return h ( 'div' , this . message )
26
+ }
14
27
}
15
- }
16
28
17
- const wrapper = mount ( MyComp , {
18
- props : {
19
- foo : 42
29
+ const wrapper = mount ( MyComp , {
30
+ props : {
31
+ foo : 42
32
+ }
33
+ } )
34
+
35
+ expect ( wrapper . html ( ) ) . toBe ( '<div>answer is 42</div>' )
36
+ } )
37
+
38
+ it ( 'hooks' , ( ) => {
39
+ let created = false
40
+ class MyComp extends Vue {
41
+ created ( ) {
42
+ created = true
43
+ }
44
+ render ( ) {
45
+ return h ( 'div' )
46
+ }
20
47
}
48
+ mount ( MyComp )
49
+ expect ( created ) . toBe ( true )
21
50
} )
22
51
23
- expect ( wrapper . html ( ) ) . toBe ( '<div>answer is 42</div>' )
52
+ it ( 'methods' , ( ) => {
53
+ let msg : string = ''
54
+
55
+ class MyComp extends Vue {
56
+ hello ( ) {
57
+ msg = 'hi'
58
+ }
59
+ content ( ) {
60
+ return 'content'
61
+ }
62
+ render ( ) {
63
+ return h ( 'div' , this . content ( ) )
64
+ }
65
+ }
66
+
67
+ const wrapper = mount ( MyComp )
68
+ wrapper . vm . hello ( )
69
+ expect ( wrapper . html ( ) ) . toBe ( '<div>content</div>' )
70
+ expect ( msg ) . toBe ( 'hi' )
71
+ } )
72
+
73
+ it ( 'computed' , ( ) => {
74
+ class MyComp extends Vue {
75
+ a ! : number
76
+ data ( ) {
77
+ return {
78
+ a : 1
79
+ }
80
+ }
81
+ get b ( ) {
82
+ return this . a + 1
83
+ }
84
+ render ( ) {
85
+ return h ( 'div' )
86
+ }
87
+ }
88
+
89
+ const wrapper = mount ( MyComp )
90
+ expect ( wrapper . vm . a ) . toBe ( 1 )
91
+ expect ( wrapper . vm . b ) . toBe ( 2 )
92
+ wrapper . vm . a = 2
93
+ expect ( wrapper . vm . b ) . toBe ( 3 )
94
+ } )
95
+
96
+ it ( 'works with shallow mount and SFC' , async ( ) => {
97
+ const wrapper = mount ( ClassComponent , {
98
+ props : {
99
+ msg : 'Props Message'
100
+ } ,
101
+ shallow : true
102
+ } )
103
+ expect ( wrapper . get ( '[data-computed]' ) . text ( ) ) . toBe ( 'Message: Props Message' )
104
+ expect ( wrapper . get ( '[data-props]' ) . text ( ) ) . toBe ( 'Props Message' )
105
+ await wrapper . get ( 'button' ) . trigger ( 'click' )
106
+ expect ( wrapper . get ( '[data-methods]' ) . text ( ) ) . toBe ( 'Updated' )
107
+ } )
24
108
} )
0 commit comments