Skip to content

Commit

Permalink
fix(backspace): Added testing
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliotGM committed Sep 6, 2018
1 parent 4e2128d commit 900c67e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion demo/demo.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class DemoComponent {
public form: FormGroup;
public value: number;
public ngxCurrencyOptions = {
prefix: '',
prefix: 'R$ ',
thousands: '.',
decimal: ',',
allowNegative: false,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 5 additions & 3 deletions src/input.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 &&
Expand All @@ -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);
Expand Down
58 changes: 58 additions & 0 deletions test/input-service.spec.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});

0 comments on commit 900c67e

Please sign in to comment.