From 663499d93bd5395e02a0df0a394323681410c512 Mon Sep 17 00:00:00 2001 From: Marzio Superina Date: Wed, 25 Jul 2018 15:24:24 +0100 Subject: [PATCH] fix: prevent setRawValue to convert 0 to empty string Closes #113 --- src/finput.js | 6 +++--- test/jestConfig.json | 3 ++- test/unit/setRawValue.js | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 test/unit/setRawValue.js diff --git a/src/finput.js b/src/finput.js index 0ebda72..a740d5c 100644 --- a/src/finput.js +++ b/src/finput.js @@ -118,12 +118,12 @@ class Finput { */ setRawValue(val) { let value; - if (!val) { - value = ''; - } else if (typeof val === 'number' && !isNaN(val)) { + if (typeof val === 'number' && !isNaN(val)) { value = helpers.rawToFormatted(val, this.options); } else if (typeof val === 'string') { value = val; + } else if (!val) { + value = ''; } else { return; } diff --git a/test/jestConfig.json b/test/jestConfig.json index f871d20..355d819 100644 --- a/test/jestConfig.json +++ b/test/jestConfig.json @@ -1,7 +1,8 @@ { "setupTestFrameworkScriptFile": "./setupTests.js", "testMatch": [ - "**/specs/**/*.js" + "**/specs/**/*.js", + "**/unit/**/*.js" ], "globals": { "__baseUrl__": "http://localhost:3000" diff --git a/test/unit/setRawValue.js b/test/unit/setRawValue.js new file mode 100644 index 0000000..34701e0 --- /dev/null +++ b/test/unit/setRawValue.js @@ -0,0 +1,22 @@ +import finput from '../../src/finput'; + +describe('setRawValue', () => { + + let element; + let destroy; + + beforeEach(() => { + element = document.createElement('input'); + destroy = finput(element); + }); + + afterEach(() => { + destroy(); + }); + + it('when passed 0 sets value 0', () => { + element.setRawValue(0); + expect(element.value).toBe('0.00'); + }); + +});