Skip to content

Commit 134a896

Browse files
committed
updated test unit
1 parent 52abecb commit 134a896

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { mount } from '@vue/test-utils'
2+
import { component as VueNumber } from '../../src'
3+
4+
describe('VueNumber', () => {
5+
it('renders the correct maskedValue and unmaskedValue when custom config', async () => {
6+
const wrapper = mount(VueNumber, {
7+
props: {
8+
modelValue: 123456.893,
9+
decimal: ',',
10+
separator: '.'
11+
}
12+
})
13+
14+
const input = wrapper.find('input')
15+
expect(input.element.value).toBe('123.456,89')
16+
wrapper.setProps({ modelValue: '1234,568' })
17+
await input.trigger('input')
18+
await input.trigger('change')
19+
20+
expect(input.element.value).toBe('1.234,57')
21+
expect(wrapper.vm.maskedValue).toBe('1.234,57')
22+
expect(wrapper.vm.unmaskedValue).toBe('1234.57')
23+
expect(wrapper.emitted()['update:model-value'][0]).toEqual(['1234.57'])
24+
expect(wrapper.emitted()['input:model-value'][0]).toEqual(['1234.57'])
25+
})
26+
})
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { mount } from '@vue/test-utils'
2+
import { component as VueNumber } from '../../src'
3+
4+
describe('VueNumber', () => {
5+
it('updates the maskedValue when input is changed', async () => {
6+
const wrapper = mount(VueNumber, {
7+
props: {
8+
modelValue: 1000.53,
9+
precision: 0
10+
}
11+
})
12+
13+
const input = wrapper.find('input')
14+
expect(input.element.value).toBe('1,001')
15+
input.setValue('1234.5')
16+
17+
expect(wrapper.vm.maskedValue).toBe('1,235')
18+
expect(wrapper.vm.unmaskedValue).toBe('1235')
19+
expect(wrapper.emitted()['input:model-value'][0]).toEqual(['1235'])
20+
expect(wrapper.emitted()['update:model-value'][0]).toEqual(['1235'])
21+
})
22+
23+
it('renders the correct maskedValue when masked prop is true', async () => {
24+
const wrapper = mount(VueNumber, {
25+
props: {
26+
modelValue: 1000,
27+
masked: true,
28+
precision: 0
29+
}
30+
})
31+
32+
const input = wrapper.find('input')
33+
expect(input.element.value).toBe('1,000')
34+
input.setValue('1234.5')
35+
36+
expect(wrapper.vm.maskedValue).toBe('1,235')
37+
expect(wrapper.vm.unmaskedValue).toBe('1235')
38+
expect(wrapper.emitted()['input:model-value'][0]).toEqual(['1,235'])
39+
expect(wrapper.emitted()['update:model-value'][0]).toEqual(['1,235'])
40+
})
41+
})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { mount } from '@vue/test-utils'
2+
import { directive } from '../../src'
3+
4+
describe('v-number directive', () => {
5+
it('should emit input event with the new value on input', async () => {
6+
const wrapper = mount({
7+
template: `<input v-number="options" type="text" :value="value" />`,
8+
directives: {
9+
number: directive
10+
},
11+
data() {
12+
return {
13+
value: 123456.893,
14+
options: {
15+
decimal: ',',
16+
separator: '.'
17+
}
18+
}
19+
}
20+
})
21+
22+
const input = wrapper.find('input')
23+
expect(input.element.value).toBe('123.456,89')
24+
25+
await input.trigger('blur')
26+
expect(input.element.value).toBe('123.456,89')
27+
28+
input.setValue('1234,568')
29+
expect(input.element.value).toBe('1.234,568')
30+
31+
await input.trigger('blur')
32+
expect(input.element.value).toBe('1.234,57')
33+
})
34+
})
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { mount } from '@vue/test-utils'
2+
import { directive } from '../../src'
3+
4+
describe('v-number directive', () => {
5+
it('should emit input event with the new value on input', async () => {
6+
const wrapper = mount({
7+
template: `<input v-number="options" type="text" :value="value" />`,
8+
directives: {
9+
number: directive
10+
},
11+
data() {
12+
return {
13+
value: '',
14+
options: {
15+
precision: 0
16+
}
17+
}
18+
}
19+
})
20+
21+
const input = wrapper.find('input')
22+
input.setValue('1234')
23+
expect(input.element.value).toBe('1,234')
24+
25+
input.setValue('1234.529')
26+
expect(input.element.value).toBe('1,235')
27+
28+
await input.trigger('blur')
29+
expect(input.element.value).toBe('1,235')
30+
})
31+
})

0 commit comments

Comments
 (0)