Skip to content

Commit 94116d7

Browse files
committed
test: increase test coverage
1 parent ecbacbd commit 94116d7

File tree

3 files changed

+51
-29
lines changed

3 files changed

+51
-29
lines changed

src/component.vue

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,22 @@ import directive from './directive'
2424
export default {
2525
name: 'InputFacade',
2626
props: {
27+
/**
28+
* A function to format the value after applying the mask. The function will receive an
29+
* object with the masked and unmasked value. The result of this function will determine
30+
* what happens with the value.
31+
* <br />
32+
* If a string is returned, then that string will pass through the masker function once more and its value
33+
* will be set to the input. If false (boolean) is returned, the input will be rejected and the
34+
* previous value will be restored. Otherwise the facade logic will continue as usual.
35+
* @since v1.3
36+
*/
37+
formatter: Function,
2738
/**
2839
* Vue's v-model .lazy modifier does not currently work with custom components. If you wish to have your v-model
2940
* updated only during the change event instead of on input, enable this property. <b>Note: This works by supressing
3041
* input events and only emitting a single input event at the same time as the change event.</b>
31-
* @since v1.2
42+
* @since v1.3
3243
*/
3344
lazy: {
3445
type: Boolean,
@@ -45,20 +56,9 @@ export default {
4556
type: Boolean,
4657
default: false
4758
},
48-
/**
49-
* A function to format the value after applying the mask. The function will receive an
50-
* object with the masked and unmasked value. The result of this function will determine
51-
* what happens with the value.
52-
* <br />
53-
* If a string is returned, then that string will pass through the masker function once more and its value
54-
* will be set to the input. If false (boolean) is returned, the input will be rejected and the
55-
* previous value will be restored. Otherwise the facade logic will continue as usual.
56-
* @since v1.2
57-
*/
58-
formatter: Function,
5959
/**
6060
* If the mask starts with static characters, show them in the field initially before typing anything.
61-
* @since v1.2
61+
* @since v1.3
6262
*/
6363
prepend: {
6464
type: Boolean,

tests/component.test.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe('Component', () => {
5050
expect(wrapper.vm.maskedValue).toBe('(444)555')
5151
})
5252

53-
test('When lazy is set to true, should only input onChange', async () => {
53+
test('When lazy is set to true, should only emit input onChange', async () => {
5454
// default settings
5555
const wrapper = createWrapper({ lazy: true })
5656
const input = wrapper.find('input')
@@ -60,6 +60,16 @@ describe('Component', () => {
6060

6161
input.trigger('change')
6262
expect(wrapper.emitted().input).toBeTruthy()
63+
expect(wrapper.emitted().change).toBeTruthy()
64+
})
65+
66+
test('When lazy is set to false, should not emit input on change', async () => {
67+
// default settings
68+
const wrapper = createWrapper({ lazy: false })
69+
const input = wrapper.find('input')
70+
71+
input.trigger('change')
72+
expect(wrapper.emitted().input).toBeFalsy()
6373
})
6474

6575
test('Adding a format function should call that function on input', async () => {

tests/directive.test.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ describe('Directive', () => {
66
let wrapper
77
const inputListener = jest.fn()
88

9-
const buildWrapper = ({ template, mask = '##.##', value = '', ...rest } = {}) => {
10-
if (!template) template = `<input v-facade="mask" value="${value}" @input="inputListener" />`
9+
const buildWrapper = ({ template, mask = '##.##', modifiers, value = '', ...rest } = {}) => {
10+
const directive = modifiers ? `v-facade.${modifiers}` : 'v-facade'
11+
if (!template) template = `<input ${directive}="mask" value="${value}" @input="inputListener" />`
1112

1213
const component = {
1314
template,
@@ -67,26 +68,37 @@ describe('Directive', () => {
6768
expect(wrapper.element.unmaskedValue).toBe('1122')
6869
})
6970

70-
test('Should honor short modifier', async () => {
71-
buildWrapper({
72-
template: `<input v-facade.short="mask" value="12" @input="inputListener" />`
73-
})
74-
expect(wrapper.element.value).toBe('12')
75-
76-
wrapper.element.value = '1234'
77-
wrapper.find('input').trigger('input')
78-
79-
expect(wrapper.element.value).toBe('12.34')
80-
expect(wrapper.element.unmaskedValue).toBe('1234')
81-
})
82-
8371
test('Should not update the cursor position if not the active element', () => {
8472
buildWrapper({ value: 'ABCDE' })
8573

8674
jest.spyOn(wrapper.element, 'setSelectionRange')
8775
expect(wrapper.element.setSelectionRange).not.toBeCalled()
8876
})
8977

78+
describe('Directive Modifiers', () => {
79+
test('Should honor short modifier', async () => {
80+
buildWrapper({ modifiers: 'short', value: '12' })
81+
expect(wrapper.element.value).toBe('12')
82+
83+
wrapper.element.value = '1234'
84+
wrapper.find('input').trigger('input')
85+
86+
expect(wrapper.element.value).toBe('12.34')
87+
expect(wrapper.element.unmaskedValue).toBe('1234')
88+
})
89+
90+
test('Should honor prepend modifier', async () => {
91+
buildWrapper({ modifiers: 'prepend', mask: '+1 ###', value: '' })
92+
expect(wrapper.element.value).toBe('+1 ')
93+
94+
wrapper.element.value = '777'
95+
wrapper.find('input').trigger('input')
96+
97+
expect(wrapper.element.value).toBe('+1 777')
98+
expect(wrapper.element.unmaskedValue).toBe('777')
99+
})
100+
})
101+
90102
describe('Cursor updates', () => {
91103
let element
92104

0 commit comments

Comments
 (0)