Skip to content

Commit

Permalink
fix(filter): prevent crashes for null values, cover filter by tests
Browse files Browse the repository at this point in the history
Fixes #510
  • Loading branch information
probil committed Jan 23, 2021
1 parent 7fde886 commit 5f1490b
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
52 changes: 51 additions & 1 deletion src/__tests__/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createLocalVue, mount } from '@vue/test-utils';
import createNumberMask from 'text-mask-addons/dist/createNumberMask';
import VueMask, { VueMaskDirective, VueMaskPlugin } from '../index';
import VueMask, { VueMaskDirective, VueMaskPlugin, VueMaskFilter } from '../index';
import { timeRangeMask } from '../utils/timeRangeMask';

describe('plugin/directive registration', () => {
Expand All @@ -18,10 +18,20 @@ describe('plugin/directive registration', () => {
expect(VueMaskPlugin).toEqual(expect.any(Function));
});

it('named export `VueMaskFilter` should be a function', () => {
expect(VueMaskFilter).toEqual(expect.any(Function));
});

it('named export `VueMaskDirective` should be an object', () => {
expect(VueMaskDirective).toEqual(expect.any(Object));
});

it('should register `VMask` filter', () => {
expect(Vue.options.filters.VMask).toBeUndefined();
Vue.use(VueMask);
expect(Vue.options.filters.VMask).toEqual(expect.any(Function));
});

it('should register `v-mask` directive', () => {
expect(Vue.options.directives.mask).toBeUndefined();
Vue.use(VueMask);
Expand Down Expand Up @@ -234,3 +244,43 @@ describe('directive usage', () => {
expect(wrapper.vm.$el.value).toBe('19:32');
});
});

describe('filter usage', () => {
let mountWithMask;

beforeEach(() => {
const localVue = createLocalVue();
localVue.use(VueMask);
mountWithMask = (arg, options) => mount(arg, { ...options, localVue });
});

it('should mask static string', () => {
const wrapper = mountWithMask({
template: '<span>{{ "9999999999" | VMask("(###) ###-####") }}</span>',
});
expect(wrapper.text()).toBe('(999) 999-9999');
});

it('should mask static number', () => {
const wrapper = mountWithMask({
template: '<span>{{ 9999999999 | VMask("(###) ###-####") }}</span>',
});
expect(wrapper.text()).toBe('(999) 999-9999');
});

it('should mask dynamic value', () => {
const wrapper = mountWithMask({
data: () => ({ val: '8888888888' }),
template: '<span>{{ val | VMask("(###) ###-####") }}</span>',
});
expect(wrapper.text()).toBe('(888) 888-8888');
});

it.each([null, undefined])('should pass through %p without modification', (val) => {
const wrapper = mountWithMask({
data: () => ({ val }),
template: '<span>{{ val | VMask("(###) ###-####") }}</span>',
});
expect(wrapper.text()).toBe('');
});
});
4 changes: 3 additions & 1 deletion src/filter.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import conformToMask from 'text-mask-core/src/conformToMask';
import { stringMaskToRegExpMask } from './maskToRegExpMask';
import { isString } from './utils';

/**
* Vue filter definition
Expand All @@ -9,6 +10,7 @@ import { stringMaskToRegExpMask } from './maskToRegExpMask';
*/
export default (value, stringMask) => {
const mask = stringMaskToRegExpMask(stringMask);
const { conformedValue } = conformToMask(value, mask, { guide: false });
if (!isString(value) && !Number.isFinite(value)) return value;
const { conformedValue } = conformToMask(`${value}`, mask, { guide: false });
return conformedValue;
};

0 comments on commit 5f1490b

Please sign in to comment.