diff --git a/demo/demo.component.ts b/demo/demo.component.ts index e576f90..a777208 100644 --- a/demo/demo.component.ts +++ b/demo/demo.component.ts @@ -29,7 +29,7 @@ export class DemoComponent { public form: FormGroup; public value: number; public ngxCurrencyOptions = { - prefix: '', + prefix: 'R$ ', thousands: '.', decimal: ',', allowNegative: false, diff --git a/package.json b/package.json index 0c20b5c..978dc95 100644 --- a/package.json +++ b/package.json @@ -89,7 +89,7 @@ "mocha": "3.3.0", "nyc": "^11.0.3", "rxjs": "5.0.1", - "sinon": "2.0.0", + "sinon": "^6.2.0", "sinon-chai": "2.8.0", "standard-version": "4.0.0", "ts-loader": "2.1.0", diff --git a/src/input.service.ts b/src/input.service.ts index 591aab4..bdb3fb3 100644 --- a/src/input.service.ts +++ b/src/input.service.ts @@ -3,7 +3,7 @@ import { CurrencyMaskConfig } from "./currency-mask.config"; export class InputService { - private inputManager: InputManager; + inputManager: InputManager; constructor(private htmlInputElement: any, private options: CurrencyMaskConfig) { this.inputManager = new InputManager(htmlInputElement); @@ -91,7 +91,7 @@ export class InputService { } let move = this.rawValue.substr(selectionStart - 1, 1).match(/\d/) ? 0 : -1; - move = ( + if ( ( keyCode == 8 && selectionStart - 1 === 0 && @@ -102,7 +102,9 @@ export class InputService { selectionStart === 0 && !(this.rawValue.substr(selectionStart + 1, 1).match(/\d/)) ) - ) ? 1 : move; + ) { + move = 1; + }; selectionEnd = keyCode == 46 || keyCode == 63272 ? selectionEnd + 1 : selectionEnd; selectionStart = keyCode == 8 ? selectionStart - 1 : selectionStart; this.rawValue = this.rawValue.substring(0, selectionStart) + this.rawValue.substring(selectionEnd, this.rawValue.length); diff --git a/test/input-service.spec.ts b/test/input-service.spec.ts new file mode 100644 index 0000000..8427386 --- /dev/null +++ b/test/input-service.spec.ts @@ -0,0 +1,58 @@ +import { InputService } from './../src/input.service'; +import { fakeAsync } from "@angular/core/testing"; +import { expect } from "chai"; +import { stub } from 'sinon'; +import {CurrencyMaskConfig} from "../src/currency-mask.config"; + +describe('Testing InputService', () => { + var options: CurrencyMaskConfig = { + prefix: '', + suffix: '', + thousands: '.', + decimal: ',', + allowNegative: false, + nullable: false, + align: 'right', + allowZero: true, + precision: undefined, + }; + var inputService: InputService; + + describe('removeNumber', () => { + it('should call updateFieldValue with 1 when deleting the first number followed by a .', () => { + inputService = new InputService({ + selectionStart: 0, + selectionEnd: 0, + }, options); + + inputService.inputManager.rawValue = '1.234,50'; + inputService.updateFieldValue = stub(); + inputService.removeNumber(46); + expect(inputService.updateFieldValue).to.be.calledWith(1); + }); + + it('should call updateFieldValue with 1 when backspacing the first number followed by a .', () => { + inputService = new InputService({ + selectionStart: 1, + selectionEnd: 1, + }, options); + + inputService.inputManager.rawValue = '1.234,50'; + inputService.updateFieldValue = stub(); + inputService.removeNumber(8); + expect(inputService.updateFieldValue).to.be.calledWith(1); + }); + + it('should call updateFieldValue with 2 less than the current position when backspacing any non-number character', () => { + inputService = new InputService({ + selectionStart: 6, + selectionEnd: 6, + }, options); + + inputService.inputManager.rawValue = '1.234,50'; + inputService.updateFieldValue = stub(); + inputService.removeNumber(8); + expect(inputService.updateFieldValue).to.be.calledWith(4); + }); + }); +});