-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
161 lines (121 loc) · 3 KB
/
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
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
import tap from 'tap';
import Vue from 'vue';
import VueSuper from './vue-super';
Vue.use(VueSuper);
const Parent = Vue.extend({
name: 'parent',
data() {
return {parent: 0};
},
methods: {
increment() {
this.parent += 1;
},
},
});
const Child = Parent.extend({
name: 'child',
data() {
return {child: 0};
},
methods: {
increment() {
this.$super(Child, this).increment();
this.child += 1;
},
},
});
const Final = Child.extend({
name: 'final',
data() {
return {'final': 0};
},
methods: {
increment() {
this.$super.increment();
this.final += 1;
},
},
});
const Skip = Child.extend({
data() {
return {skip: 0};
},
methods: {
increment() {
// skip child - call parent instead
this.$super(Child, this).increment();
this.skip += 1;
},
},
});
tap.test('regular method should execute', t => {
t.plan(2);
const vue = new Parent();
// base case - parent data should increment
t.equal(vue.parent, 0);
vue.increment();
t.equal(vue.parent, 1);
});
tap.test('inherited method should call parent', t => {
t.plan(4);
const vue = new Child();
// child should also call parent
t.equal(vue.parent, 0);
t.equal(vue.child, 0);
vue.increment();
t.equal(vue.parent, 1);
t.equal(vue.child, 1);
});
tap.test('final class should have direct method access', t => {
t.plan(6);
const vue = new Final();
// test entire call chain
t.equal(vue.parent, 0);
t.equal(vue.child, 0);
t.equal(vue.final, 0);
vue.increment();
t.equal(vue.parent, 1);
t.equal(vue.child, 1);
t.equal(vue.final, 1);
});
tap.test('use provided class as base', t => {
t.plan(6);
const vue = new Skip();
// Child implementation skipped
t.equal(vue.parent, 0);
t.equal(vue.child, 0);
t.equal(vue.skip, 0);
vue.increment();
t.equal(vue.parent, 1);
t.equal(vue.child, 0);
t.equal(vue.skip, 1);
});
tap.test('only bind against inherited types', t => {
t.plan(1);
const Err = Parent.extend({
name: 'err',
methods: {
increment() {
this.$super(Final, this).increment();
this.child += 1;
},
},
});
const vue = new Err();
t.throws(vue.increment, new TypeError('<Err> not an instance of <Final>'));
});
tap.test('throw appropriate error when parent does not have `methods` attribute', t => {
t.plan(1);
const Err = Parent.extend({
methods: {
increment() {
// Parent's super class is just Vue - method does not exist
this.$super(Parent, this).increment();
this.child += 1;
},
},
});
const vue = new Err();
t.throws(vue.increment, new TypeError('this.$super(...).increment is not a function'));
});