Skip to content

Commit a2019bf

Browse files
Marzio Superinaoriondean
authored andcommitted
fix: prevent setRawValue to convert 0 to empty string
Closes #113
1 parent 95e8cab commit a2019bf

File tree

3 files changed

+27
-4
lines changed

3 files changed

+27
-4
lines changed

src/finput.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,12 @@ class Finput {
118118
*/
119119
setRawValue(val) {
120120
let value;
121-
if (!val) {
122-
value = '';
123-
} else if (typeof val === 'number' && !isNaN(val)) {
121+
if (typeof val === 'number' && !isNaN(val)) {
124122
value = helpers.rawToFormatted(val, this.options);
125123
} else if (typeof val === 'string') {
126124
value = val;
125+
} else if (!val) {
126+
value = '';
127127
} else {
128128
return;
129129
}

test/jestConfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"setupTestFrameworkScriptFile": "./setupTests.js",
33
"testMatch": [
4-
"**/specs/**/*.js"
4+
"**/specs/**/*.js",
5+
"**/unit/**/*.js"
56
],
67
"globals": {
78
"__baseUrl__": "http://localhost:3000"

test/unit/setRawValue.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import finput from '../../src/finput';
2+
3+
describe('setRawValue', () => {
4+
5+
let element;
6+
let destroy;
7+
8+
beforeEach(() => {
9+
element = document.createElement('input');
10+
destroy = finput(element);
11+
});
12+
13+
afterEach(() => {
14+
destroy();
15+
});
16+
17+
it('when passed 0 sets value 0', () => {
18+
element.setRawValue(0);
19+
expect(element.value).toBe('0.00');
20+
});
21+
22+
});

0 commit comments

Comments
 (0)