Skip to content

Commit b92dc9d

Browse files
Robo-RinJosh Espinosa
authored and
Josh Espinosa
committed
fix(filter): replace unsupported Vue.filter
Vue 3 doesn't support Vue.filter and needed to be replaced by a standard utility function. closes #44
1 parent 14dd001 commit b92dc9d

File tree

3 files changed

+61
-5
lines changed

3 files changed

+61
-5
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
"build:docs": "vue-cli-service styleguidist:build --config styleguide/config.js",
2121
"test": "jest --coverage",
2222
"test:filter": "func() { yarn test \"$1\" --coverage-reporters=\"lcov\" --coverageThreshold={} ${@:2}; }; func",
23-
"test:watch": "jest --coverage --watchAll"
23+
"test:watch": "jest --coverage --watchAll",
24+
"preyalcpublish": "yarn build:plugin"
2425
},
2526
"main": "dist/vue-input-facade.umd.min.js",
2627
"files": [

src/plugin.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,24 @@ function install(Vue, options = {}) {
1818

1919
Vue.component(InputFacade.name, InputFacade)
2020
Vue.directive(options.name || 'facade', facade)
21-
Vue.filter(options.name || 'facade', filter)
2221
}
2322

2423
/**
25-
* Utility function to be used as a vue filter
24+
* Utility function to be used as a vue method
25+
*
26+
* This likely needs to be registered in the SFC as a method if using the options API,
27+
* or in the `setup` hook if using the composition API.
2628
*
2729
* @param {String} value the value to apply the filter to
2830
* @param {*} config the masking config
2931
* @returns {string} the masked value as returned by the masker function
3032
*/
31-
function filter(value, config) {
33+
function useFacade(value, config) {
3234
return masker(value, config).masked
3335
}
3436

3537
export default install
36-
export { InputFacade, facade, tokens, masker, filter }
38+
export { InputFacade, facade, tokens, masker, useFacade }
3739

3840
// Install by default if included from script tag
3941
if (typeof window !== 'undefined' && window.Vue) {

tests/plugin.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { useFacade } from '../src/plugin'
2+
import { shallowMount } from '@vue/test-utils'
3+
4+
describe('Plugin tests', () => {
5+
describe('useFacade', () => {
6+
const ComponentMock = {
7+
template: '<div id="maskedText">{{ useFacade(input, facadeConfig) }}</div>',
8+
data() {
9+
return {
10+
input: '',
11+
facadeConfig: { mask: '' }
12+
}
13+
},
14+
methods: { useFacade }
15+
}
16+
17+
function createWrapper() {
18+
return shallowMount(ComponentMock)
19+
}
20+
21+
test('Should mask input according to the provided config', async () => {
22+
const wrapper = createWrapper()
23+
await wrapper.setData({
24+
input: '1234',
25+
facadeConfig: { mask: '###-#' }
26+
})
27+
28+
expect(wrapper.find('#maskedText').text()).toBe('123-4')
29+
})
30+
31+
test('Should return an empty mask if initial value is null', async () => {
32+
const wrapper = createWrapper()
33+
await wrapper.setData({
34+
input: null,
35+
facadeConfig: { mask: '###-#' }
36+
})
37+
38+
expect(wrapper.find('#maskedText').text()).toBe('')
39+
})
40+
41+
test('Should update the mask if the input value changes', async () => {
42+
const wrapper = createWrapper()
43+
await wrapper.setData({
44+
input: '1234',
45+
facadeConfig: { mask: '###-#' }
46+
})
47+
expect(wrapper.find('#maskedText').text()).toBe('123-4')
48+
49+
await wrapper.setData({ input: '5555' })
50+
expect(wrapper.find('#maskedText').text()).toBe('555-5')
51+
})
52+
})
53+
})

0 commit comments

Comments
 (0)