|
| 1 | +import Vue from 'vue'; |
| 2 | +Vue.config.productionTip = false; |
| 3 | +import VueNumeric from './../src/vue-numeric.vue'; |
| 4 | + |
| 5 | +function getInput(Component, propsData) { |
| 6 | + const Ctor = Vue.extend(Component) |
| 7 | + const vm = new Ctor({ propsData: propsData }).$mount() |
| 8 | + return vm.$el; |
| 9 | +} |
| 10 | + |
| 11 | +describe("vue-numeric", function() { |
| 12 | + |
| 13 | + let el; |
| 14 | + |
| 15 | + beforeEach(() => { |
| 16 | + el = document.createElement('div'); |
| 17 | + |
| 18 | + document.body.appendChild(el); |
| 19 | + }); |
| 20 | + |
| 21 | + it("Uses the default decimal separator ,", done => { |
| 22 | + |
| 23 | + var input = getInput(VueNumeric, { value: '2000' }); |
| 24 | + Vue.nextTick(() => { |
| 25 | + expect(input.value).toEqual(' 2,000') |
| 26 | + done() |
| 27 | + }); |
| 28 | + |
| 29 | + }); |
| 30 | + |
| 31 | + it('updates value with format if without focus', done => { |
| 32 | + const vm = new Vue({ |
| 33 | + el, |
| 34 | + data: function() { |
| 35 | + return { |
| 36 | + total: 0 |
| 37 | + } |
| 38 | + }, |
| 39 | + template: '<div><vue-numeric v-model="total"></vue-numeric></div>', |
| 40 | + components: { VueNumeric } |
| 41 | + }).$mount() |
| 42 | + |
| 43 | + vm.total = 3000; |
| 44 | + |
| 45 | + Vue.nextTick(() => { |
| 46 | + expect(vm.$el.firstChild.value.trim()).toEqual('3,000') |
| 47 | + done() |
| 48 | + }); |
| 49 | + |
| 50 | + }); |
| 51 | + |
| 52 | + it('updates values decimals', done => { |
| 53 | + const vm = new Vue({ |
| 54 | + el, |
| 55 | + data: function() { |
| 56 | + return { |
| 57 | + total: 0 |
| 58 | + } |
| 59 | + }, |
| 60 | + template: `<div> |
| 61 | + <vue-numeric v-model="total" separator="." precision="2"></vue-numeric> |
| 62 | + <span>{{total}}</span> |
| 63 | + </div>`, |
| 64 | + components: { VueNumeric } |
| 65 | + }).$mount() |
| 66 | + |
| 67 | + vm.total = 3000; |
| 68 | + |
| 69 | + Vue.nextTick(() => { |
| 70 | + const span = vm.$el.getElementsByTagName('span')[0]; |
| 71 | + expect(span.textContent.trim()).toEqual('3000'); |
| 72 | + expect(vm.$el.firstChild.value.trim()).toEqual('3.000,00') |
| 73 | + done() |
| 74 | + }); |
| 75 | + |
| 76 | + }); |
| 77 | + |
| 78 | + |
| 79 | + it('accepts decimal values', done => { |
| 80 | + const vm = new Vue({ |
| 81 | + el, |
| 82 | + data: function() { |
| 83 | + return { |
| 84 | + total: 200.22, |
| 85 | + subtotal: "110.98", |
| 86 | + large: "10.000,1" |
| 87 | + }; |
| 88 | + }, |
| 89 | + template: `<div> |
| 90 | + <vue-numeric v-model="large" separator="." precision="2"></vue-numeric> |
| 91 | + <vue-numeric v-model="total" separator="." precision="2"></vue-numeric> |
| 92 | + <vue-numeric v-model="subtotal" precision="2"></vue-numeric> |
| 93 | + </div>`, |
| 94 | + components: { VueNumeric } |
| 95 | + }).$mount() |
| 96 | + |
| 97 | + Vue.nextTick(() => { |
| 98 | + expect(vm.$el.children[0].value.trim()).toEqual('10.000,10') |
| 99 | + expect(vm.$el.children[1].value.trim()).toEqual('200,22') |
| 100 | + expect(vm.$el.children[2].value.trim()).toEqual('110.98') |
| 101 | + done() |
| 102 | + }); |
| 103 | + |
| 104 | + }); |
| 105 | + |
| 106 | + |
| 107 | + it('updates values with correct separator', done => { |
| 108 | + const vm = new Vue({ |
| 109 | + el, |
| 110 | + data: function() { |
| 111 | + return { |
| 112 | + total: 0 |
| 113 | + } |
| 114 | + }, |
| 115 | + template: `<div> |
| 116 | + <vue-numeric v-model="total" separator="."></vue-numeric> |
| 117 | + <span>{{total}}</span> |
| 118 | + </div>`, |
| 119 | + components: { VueNumeric } |
| 120 | + }).$mount() |
| 121 | + |
| 122 | + vm.total = 3000; |
| 123 | + |
| 124 | + Vue.nextTick(() => { |
| 125 | + const span = vm.$el.getElementsByTagName('span')[0]; |
| 126 | + expect(span.textContent.trim()).toEqual('3000'); |
| 127 | + expect(vm.$el.firstChild.value.trim()).toEqual('3.000') |
| 128 | + done() |
| 129 | + }); |
| 130 | + |
| 131 | + }); |
| 132 | + |
| 133 | + |
| 134 | + it('updates value without format', done => { |
| 135 | + const vm = new Vue({ |
| 136 | + el, |
| 137 | + data: function() { |
| 138 | + return { |
| 139 | + total: 0 |
| 140 | + } |
| 141 | + }, |
| 142 | + template: '<div><vue-numeric v-model="total"></vue-numeric></div>', |
| 143 | + components: { VueNumeric } |
| 144 | + }).$mount() |
| 145 | + |
| 146 | + vm.$el.firstChild.focus(); |
| 147 | + vm.total = 3000; |
| 148 | + |
| 149 | + Vue.nextTick(() => { |
| 150 | + expect(vm.$el.firstChild.value.trim()).toEqual('3000'); |
| 151 | + done() |
| 152 | + }); |
| 153 | + |
| 154 | + }); |
| 155 | + |
| 156 | + |
| 157 | +}); |
0 commit comments