Skip to content

Commit d1adcd9

Browse files
committed
added vitest for testing
1 parent c28a409 commit d1adcd9

19 files changed

+1907
-284
lines changed

.vscode/settings.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"editor.formatOnSave": true,
3+
"editor.formatOnSaveMode": "modificationsIfAvailable"
4+
}

package.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,37 @@
2323
"build": "tsc --emitDeclarationOnly --skipLibCheck --declaration --outDir .temp/types && rollup --c rollup.config.js",
2424
"docs:build": "vuepress build docs",
2525
"docs:dev": "vuepress dev docs",
26-
"test": "jest tests/*",
26+
"test": "vitest",
27+
"coverage": "vitest run --coverage",
2728
"lint": "eslint 'src/**/*.{js,vue}'",
2829
"push": "clear && git config core.ignorecase false && branch=\"$(git symbolic-ref -q HEAD)\" || \"dev\" && branch=${branch##refs/heads/} && branch=${branch:-HEAD} && echo Pushing to Branch \"$branch\" && echo Please type your commit message && read msg && clear && git add . && git commit -m \"$msg\" && git push origin \"$branch\""
2930
},
3031
"devDependencies": {
32+
"@rollup/plugin-node-resolve": "^15.0.1",
3133
"@rushstack/eslint-patch": "^1.2.0",
34+
"@types/jest": "^29.4.0",
3235
"@typescript-eslint/eslint-plugin": "^5.38.1",
3336
"@typescript-eslint/parser": "^5.38.1",
34-
"@rollup/plugin-node-resolve": "^15.0.1",
3537
"@vue/eslint-config-prettier": "^7.0.0",
3638
"@vue/eslint-config-typescript": "^11.0.2",
37-
"prettier": "^2.7.1",
38-
"rimraf": "^3.0.2",
39+
"@vue/test-utils": "^1.3.0",
3940
"eslint": "^8.34.0",
4041
"eslint-config-prettier": "^8.5.0",
4142
"eslint-plugin-vue": "^9.9.0",
43+
"jsdom": "latest",
4244
"lint-staged": "^13.1.2",
45+
"prettier": "^2.7.1",
46+
"rimraf": "^3.0.2",
4347
"rollup": "^2.6.1",
44-
"rollup-plugin-dts": "^5.1.0",
48+
"rollup-plugin-dts": "^4.2.2",
4549
"rollup-plugin-filesize": "^9.1.2",
4650
"rollup-plugin-typescript2": "^0.34.1",
4751
"typescript": "^4.9.5",
48-
"vue": "^2.6.0"
52+
"vite": "^4.1.1",
53+
"vite-plugin-vue2": "^1.9.3",
54+
"vitest": "^0.28.5",
55+
"vue": "^2.6.0",
56+
"vue-template-compiler": "^2.6.0"
4957
},
5058
"bugs": {
5159
"url": "https://github.com/coders-tm/vue-number-format/issues"

rollup.config.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import typescript from 'rollup-plugin-typescript2';
2-
import filesize from 'rollup-plugin-filesize';
3-
import resolve from '@rollup/plugin-node-resolve';
4-
import dts from 'rollup-plugin-dts';
1+
import typescript from 'rollup-plugin-typescript2'
2+
import filesize from 'rollup-plugin-filesize'
3+
import resolve from '@rollup/plugin-node-resolve'
4+
import dts from 'rollup-plugin-dts'
55
import pkg from './package.json'
66

77
const banner = `/**
@@ -10,7 +10,6 @@ const banner = `/**
1010
* @license ${pkg.license}
1111
*/`
1212

13-
1413
export default [
1514
{
1615
input: 'src/index.ts',
@@ -46,4 +45,4 @@ export default [
4645
output: [{ file: 'dist/index.d.ts', format: 'es' }],
4746
plugins: [dts()]
4847
}
49-
];
48+
]

src/component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
2-
import Vue from 'vue';
1+
import Vue from 'vue'
32
import directive from './directive'
43
import { cloneDeep, CustomInputEvent, Input } from './core'
54
import defaultOptions from './options'

src/core.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,11 +110,8 @@ export function updateCursor(el: HTMLInputElement, position: number) {
110110
* @param {Boolean} options.force Forces the update even if the old value and the new value are the same
111111
*/
112112
export function updateValue(el: CustomInputElement, vnode: VNode | null, { emit = true, force = false, clean = false } = {}) {
113-
console.log(vnode);
114113
const { options, oldValue } = el
115-
116-
// TODO: let currentValue = vnode && vnode.data.model ? vnode.data.model.value : el.value
117-
const currentValue = el.value
114+
const currentValue = vnode && vnode.data && vnode.data.domProps ? vnode.data.domProps.value : el.value
118115

119116
if (force || oldValue !== currentValue) {
120117
const number = new NumberFormat(options).clean(clean && !options.reverseFill)

src/number-format.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ export default class NumberFormat {
7272
}
7373

7474
sign() {
75+
if (this.input === null || this.input === undefined) {
76+
return ''
77+
}
7578
const hasMinus = this.input.toString().indexOf('-') >= 0
7679
if (this.isClean) {
7780
return hasMinus && this.realNumber() > 0 ? '-' : ''
@@ -150,7 +153,7 @@ export default class NumberFormat {
150153
if (this.isNull() && !this.options.reverseFill) {
151154
return this.options.nullValue
152155
}
153-
return [this.sign(), this.options.prefix, this.addSeparator(), this.options.suffix].join('')
156+
return this.sign() + this.options.prefix + this.addSeparator() + this.options.suffix
154157
}
155158

156159
/**

tests/number-format.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import NumberFormat from '../src/number-format'
2+
3+
test('test number format', async () => {
4+
const number = new NumberFormat({
5+
prefix: '$',
6+
suffix: '',
7+
separator: ',',
8+
decimal: '.',
9+
precision: 2,
10+
minimumFractionDigits: 1,
11+
prefill: true,
12+
reverseFill: false,
13+
nullValue: ''
14+
})
15+
16+
expect(number.format('458.869')).toBe('$458.87')
17+
})

tests/unit/directive.spec.js

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

tests/unit/number-format.custom.spec.js

Lines changed: 0 additions & 92 deletions
This file was deleted.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import NumberFormat from '../../src/number-format'
2+
3+
test('when the value is invalid with custom config', () => {
4+
const numberFormat = new NumberFormat({
5+
prefix: '$',
6+
separator: '.',
7+
decimal: ',',
8+
nullValue: '0'
9+
})
10+
test('should return as follows', () => {
11+
expect(numberFormat.format('')).toEqual('')
12+
expect(numberFormat.format('foo')).toEqual('')
13+
expect(numberFormat.format('-foo')).toEqual('')
14+
expect(numberFormat.format('-fo,o-')).toEqual('')
15+
expect(numberFormat.format('-fo.o-')).toEqual('')
16+
expect(numberFormat.format('!@#$%^&*()')).toEqual('')
17+
})
18+
test('should return as follows', () => {
19+
expect(numberFormat.clean(true).unformat('')).toEqual('')
20+
expect(numberFormat.clean(true).unformat('foo')).toEqual('')
21+
expect(numberFormat.clean(true).unformat('-foo')).toEqual('')
22+
expect(numberFormat.clean(true).unformat('-fo.o-')).toEqual('')
23+
expect(numberFormat.clean(true).unformat('!@#$%^&*()')).toEqual('')
24+
})
25+
})
26+
test('format when options are custom', () => {
27+
const numberFormat = new NumberFormat({
28+
prefix: '$',
29+
separator: '.',
30+
decimal: ',',
31+
nullValue: ''
32+
})
33+
test('format string value', () => {
34+
expect(numberFormat.format('')).toEqual('')
35+
expect(numberFormat.format('0')).toEqual('$0')
36+
expect(numberFormat.format('0,')).toEqual('$0')
37+
expect(numberFormat.format('-0,0')).toEqual('$0')
38+
expect(numberFormat.format('0,10')).toEqual('$0,1')
39+
expect(numberFormat.format('0,0-')).toEqual('$0')
40+
expect(numberFormat.format('0,10-')).toEqual('-$0,1')
41+
expect(numberFormat.format('12.345,54921')).toEqual('$12.345,55')
42+
expect(numberFormat.format('--12.345,12345')).toEqual('-$12.345,12')
43+
expect(numberFormat.format('12.345.54321,12945')).toEqual('$1.234.554.321,13')
44+
expect(numberFormat.format('-12.345,,54321-')).toEqual('-$12.345,54')
45+
})
46+
test('format numerical value', () => {
47+
expect(numberFormat.format(0)).toEqual('$0')
48+
expect(numberFormat.format(0)).toEqual('$0')
49+
expect(numberFormat.format(0.0)).toEqual('$0')
50+
expect(numberFormat.format(-0.1)).toEqual('-$0,1')
51+
expect(numberFormat.format(-0.0)).toEqual('$0')
52+
expect(numberFormat.format(0.1)).toEqual('$0,1')
53+
expect(numberFormat.format(12345.54921)).toEqual('$12.345,55')
54+
expect(numberFormat.format(12345.12345)).toEqual('$12.345,12')
55+
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54')
56+
expect(numberFormat.format(12345.54321)).toEqual('$12.345,54')
57+
})
58+
})

0 commit comments

Comments
 (0)