Skip to content

Commit 9fd3cb1

Browse files
committed
added component tester
1 parent 478786e commit 9fd3cb1

File tree

6 files changed

+80
-27
lines changed

6 files changed

+80
-27
lines changed

docs/.vuepress/components/PlayGround.vue

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<template>
22
<div>
33
<div class="grid gap-y-4 md:grid-cols-2 md:gap-x-8 items-center my-8">
4-
<div class="grid">
4+
<!-- <div class="grid">
55
<div class="font-medium mb-2">Component</div>
66
<VueNumber
77
v-if="updated"
@@ -16,7 +16,7 @@
1616
<div class="mt-2">
1717
Number value: <code class="ml-2">{{ price }}</code>
1818
</div>
19-
</div>
19+
</div> -->
2020
<div class="grid">
2121
<div class="font-medium mb-2">Directive</div>
2222
<BaseInput
@@ -143,9 +143,9 @@ export default {
143143
price: 1234.5,
144144
priceDirective: 1234.5,
145145
config: {
146-
decimal: '.',
147-
separator: ',',
148-
prefix: '$',
146+
decimal: ',',
147+
separator: '.',
148+
prefix: 'Rs.',
149149
suffix: '',
150150
precision: 2,
151151
nullValue: '',
@@ -167,10 +167,6 @@ export default {
167167
methods: {
168168
onChange() {
169169
console.log('onChange', arguments)
170-
// setTimeout(() => {
171-
// this.price = 1500.629
172-
// this.priceDirective = 1500.629
173-
// }, 3000)
174170
},
175171
onInput() {
176172
console.log('onInput', arguments)

src/core.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ export function getInputElement(el: HTMLElement | HTMLInputElement): CustomInput
8383
* Updates the cursor position to the right place after the masking rule was applied
8484
*/
8585
export function updateCursor(el: HTMLInputElement, position: number) {
86+
return false
8687
const setSelectionRange = (): any => {
8788
el.setSelectionRange(position, position)
8889
}

src/number-format.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,15 @@ export default class NumberFormat {
9797
const { reverseFill, decimal } = this.options
9898
if (reverseFill) {
9999
this.number = this.toFixed().replace('.', decimal)
100+
console.log('reverseFill', this.number)
100101
} else if (typeof this.input === 'number') {
101102
this.number = this.parts(this.input.toString().replace('-', ''), '.').join(decimal)
103+
console.log('number', this.number)
102104
} else {
103105
this.number = this.parts(this.numberOnly()).join(decimal)
106+
console.log('else', this.numberRegExp, this.numberOnly(), this.number)
104107
}
108+
105109
return this.number
106110
}
107111

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { mount } from '@vue/test-utils'
2+
import VueNumber from '../src/component.vue'
3+
4+
describe('VueNumber', () => {
5+
test('should emit input event with the new value on input', async () => {
6+
const wrapper = mount(VueNumber, {
7+
propsData: {
8+
value: 123456.893,
9+
decimal: ',',
10+
separator: '.',
11+
prefix: 'Rs.',
12+
suffix: '%'
13+
}
14+
})
15+
16+
const input = wrapper.find('input')
17+
// input.element.value = '200'
18+
19+
await input.trigger('input')
20+
21+
expect(wrapper.vm.maskedValue).toBe('Rs.123.456,893%')
22+
23+
await input.trigger('blur')
24+
25+
expect(wrapper.vm.maskedValue).toBe('Rs.123.456,89%')
26+
27+
input.element.value = 1234.568
28+
29+
await input.trigger('input')
30+
31+
expect(wrapper.vm.maskedValue).toBe('Rs.1.234,568%')
32+
33+
await input.trigger('blur')
34+
35+
expect(wrapper.vm.maskedValue).toBe('Rs.1.234,57%')
36+
})
37+
})

tests/vue-number-component.spec.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { mount } from '@vue/test-utils'
2+
import VueNumber from '../src/component.vue'
3+
4+
describe('VueNumber', () => {
5+
test('should emit input event with the new value on input', async () => {
6+
const wrapper = mount(VueNumber, {
7+
propsData: {
8+
value: 123456.893
9+
}
10+
})
11+
12+
const input = wrapper.find('input')
13+
// input.element.value = '200'
14+
15+
await input.trigger('input')
16+
17+
expect(wrapper.vm.maskedValue).toBe('123,456.893')
18+
19+
await input.trigger('blur')
20+
21+
expect(wrapper.vm.maskedValue).toBe('123,456.89')
22+
23+
input.element.value = '1234.568'
24+
25+
await input.trigger('input')
26+
27+
expect(wrapper.vm.maskedValue).toBe('1,234.568')
28+
29+
await input.trigger('blur')
30+
31+
expect(wrapper.vm.maskedValue).toBe('1,234.57')
32+
})
33+
})

tests/vue-number-input.spec.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

0 commit comments

Comments
 (0)