-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathVueAutonumeric.spec.js
287 lines (255 loc) · 12.4 KB
/
VueAutonumeric.spec.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import { mount } from '@vue/test-utils';
import VueAutonumeric from '@/components/VueAutonumeric';
describe('VueAutonumeric.vue', () => {
it('should generate an input by default', () => {
const wrapper = mount(VueAutonumeric);
expect(wrapper.is('input')).toBe(true);
expect(wrapper.attributes().type).toEqual('text');
expect(wrapper.attributes().value).toEqual('');
expect(wrapper.isEmpty()).toBe(true);
});
it('should correctly be formatted', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
options: 'euro',
value : 1234567.78,
},
});
expect(wrapper.is('input')).toBe(true);
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567,78 €');
});
it('should correctly be formatted after its value is modified by a user interaction', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
options: 'euro',
value : 1234567.78,
},
});
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567,78 €');
const input = wrapper.find('input');
expect(input.element.value).toEqual('1.234.567,78 €');
// Test inputting a number
input.trigger('click'); //FIXME Is this really needed? Isn't the focus already on the input since it's the generated DOM element?
input.trigger('keydown.home');
input.trigger('keydown', { //FIXME Fails (see https://github.com/vuejs/vue-test-utils/issues/484)
key: '1',
keyCode: 49,
which: 49,
});
expect(input.element.value).toEqual('11.234.567,78 €');
expect(wrapper.vm.anElement.rawValue).toEqual('11234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('11.234.567,78 €');
});
it('should correctly be formatted after its value is modified without a user interaction, via setting the input `value`', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
options: 'euro',
value : 1234567.78,
},
});
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567,78 €');
// Modify the input value programmatically
const input = wrapper.find('input');
input.element.value = 221100;
expect(wrapper.element.value).toEqual('221.100,00 €'); //FIXME Fails : received 221100; this is not formatted and the `rawValue` is not updated
expect(wrapper.vm.value).toEqual('221100'); //FIXME Fails
expect(wrapper.vm.anElement.rawValue).toEqual('221100'); //FIXME Fails
expect(wrapper.vm.anElement.getFormatted()).toEqual('221.100,00 €');
});
it('should correctly be formatted after its value is modified without a user interaction, via props', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
options: 'euro',
value : 1234567.78,
},
});
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567,78 €');
wrapper.setProps({ value: '221100' });
expect(wrapper.vm.value).toEqual('221100');
expect(wrapper.element.value).toEqual('221.100,00 €'); //FIXME Fails
expect(wrapper.vm.anElement.rawValue).toEqual('221100'); //FIXME Fails
expect(wrapper.vm.anElement.getFormatted()).toEqual('221.100,00 €');
});
it('should correctly be formatted after multiple option updates', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
options: 'euro',
value : 1234567.78,
},
});
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567,78 €');
wrapper.setProps({ options: 'dollar' }); //FIXME Fails
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('$1,234,567.78');
wrapper.setProps({ options: 'euro' }); //FIXME Fails
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567,78 €');
wrapper.setProps({ options: ['euro', { currencySymbol: '#' }] }); //FIXME Fails
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567,78 #');
});
it('should correctly set the decimal place precision when updating the options, then the value', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
options: 'euro',
value : 1234567.7812,
},
});
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567,78 €');
wrapper.setProps({ options: 'percentageEU3dec' });
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567.780,000 %'); //FIXME Fails
wrapper.setProps({ value: 0.00123456 }); //FIXME Fails
expect(wrapper.vm.anElement.rawValue).toEqual('0.001234');
expect(wrapper.vm.anElement.getFormatted()).toEqual('0,123 %');
});
it('should save the maximal `decimalPlaces` option between options updates in order to keep the correct precision', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
options: 'euro',
value : 221456.72,
},
});
expect(wrapper.vm.anElement.rawValue).toEqual('221456.72');
expect(wrapper.vm.anElement.getFormatted()).toEqual('221.456,72 €');
wrapper.setProps({ options: ['dollar', { decimalPlaces: 5 }] });
expect(wrapper.vm.anElement.rawValue).toEqual('221456.72');
expect(wrapper.vm.anElement.getFormatted()).toEqual('$221,456.72000');
// Then modify the decimal places
const input = wrapper.find('input');
input.trigger('click'); //FIXME Is this really needed? Isn't the focus already on the input since it's the generated DOM element?
input.trigger('keydown.end');
input.trigger('keydown.left');
input.trigger('keydown.left');
input.trigger('keydown.left'); // Position ourselves here: '$221,456.72|000'
input.trigger('keydown', { //FIXME Fails (see https://github.com/vuejs/vue-test-utils/issues/484)
key: '8',
keyCode: 56,
which: 56,
});
input.trigger('keydown', {
key: '2',
keyCode: 50,
which: 50,
});
input.trigger('keydown', {
key: '2',
keyCode: 50,
which: 50,
});
expect(wrapper.vm.anElement.rawValue).toEqual('221456.72882');
expect(wrapper.vm.anElement.getFormatted()).toEqual('$221,456.72882');
// Switch back to option that have a lesser number of decimal places
wrapper.setProps({ options: ['euro'] });
expect(wrapper.vm.anElement.rawValue).toEqual('221456.72882');
expect(wrapper.vm.anElement.getFormatted()).toEqual('221.456,72 €');
// ...then finally switch back to the option with 5 decimal places; that data should not have been 'deleted' when switching to the previous options
wrapper.setProps({ options: ['dollar', { decimalPlaces: 5 }] });
expect(wrapper.vm.anElement.rawValue).toEqual('221456.72882');
expect(wrapper.vm.anElement.getFormatted()).toEqual('$221,456.72882');
});
it('should correctly update the options and value when using a single object to change those two at the same time', () => {
// It should correctly set the decimal place precision during a combined option and value update
let obj = {
val : 123456.78,
options: 'euro',
};
const wrapper = mount(VueAutonumeric, {
propsData: {
options: obj.options,
value : obj.val,
},
});
expect(wrapper.vm.anElement.rawValue).toEqual('123456.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('123.456,78 €');
// Update the options and value at the same time:
obj = {
options : 'percentageEU3dec',
val : 0.00123456,
};
expect(wrapper.vm.anElement.rawValue).toEqual('0.001234'); //FIXME Fails
expect(wrapper.vm.anElement.getFormatted()).toEqual('1,234 %');
// And back the original options/values
obj.val = 123456.78;
obj.options = 'euro';
expect(wrapper.vm.anElement.rawValue).toEqual('123456.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('123.456,78 €');
});
it('should be generated with the `id` and `name` passed as attributes', () => {
const id = 'theIdToUse';
const name = 'theInputName';
const wrapper = mount(VueAutonumeric, {
attrs: {
id,
name,
},
});
expect(wrapper.is('input')).toBe(true);
expect(wrapper.attributes().id).toEqual(id); //FIXME Fails (see https://github.com/vuejs/vue-test-utils/issues/483)
expect(wrapper.attributes().name).toEqual(name); //FIXME Fails
});
it('should be generated with the `placeholder` attribute passed as a prop', () => {
const placeholderText = 'This is my placeholder';
const wrapper = mount(VueAutonumeric, {
propsData: {
placeholder: placeholderText,
},
});
expect(wrapper.is('input')).toBe(true);
expect(wrapper.attributes().placeholder).toEqual(placeholderText);
});
it('should correctly generate a `div` element when using the `tag` prop, and set the `contenteditable` attribute', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
tag: 'div',
},
});
expect(wrapper.is('input')).toBe(false);
expect(wrapper.is('div')).toBe(true);
expect(wrapper.attributes().contenteditable).toBeTruthy();
});
it('should correctly generate a `p` element when using the `tag` prop, and set the `contenteditable` attribute', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
tag: 'p',
},
});
expect(wrapper.is('input')).toBe(false);
expect(wrapper.is('p')).toBe(true);
expect(wrapper.attributes().contenteditable).toBeTruthy();
});
it('should bubble up the AutoNumeric events to the parent', () => {
const wrapper = mount(VueAutonumeric, {
propsData: {
options: 'euro',
value : 1234567.78,
},
});
expect(wrapper.vm.anElement.rawValue).toEqual('1234567.78');
expect(wrapper.vm.anElement.getFormatted()).toEqual('1.234.567,78 €');
const input = wrapper.find('input');
expect(input.element.value).toEqual('1.234.567,78 €');
// Test inputting a number
input.trigger('click'); //FIXME Is this really needed? Isn't the focus already on the input since it's the generated DOM element?
input.trigger('keydown.home');
input.trigger('keydown', { //FIXME Fails (see https://github.com/vuejs/vue-test-utils/issues/484)
key: '1',
keyCode: 49,
which: 49,
});
//FIXME Finish this: How to test that the native AutoNumeric events are correctly bubbling up?
expect(wrapper.emittedByOrder()[2].args[0]).toEqual('11234567.78'); //FIXME Fails
expect(wrapper.vm.anElement.getFormatted()).toEqual('11.234.567,78 €');
});
xit('should correctly format the input value when a shared v-model is modified by another instance of VueAutonumeric', () => { //FIXME Finish this
const wrapper = mount(VueAutonumeric);
expect(wrapper.is('input')).toBe(true);
});
//FIXME Finish this: test the `resetOnOptions` and `tag` props
});