Skip to content
This repository has been archived by the owner on May 5, 2022. It is now read-only.

Commit

Permalink
v1.1.7
Browse files Browse the repository at this point in the history
  • Loading branch information
aguirrel committed Mar 18, 2017
2 parents f8551b9 + 7dc02b5 commit c912c55
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<!-- Example: The page turns pink. -->

### Live Demo
<!-- Example: https://jsbin.com/hoputodequ/edit?html,output -->
<!-- Example: https://jsbin.com/pajuhaf/edit?html,output -->

### Steps to reproduce

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Then add a `<script>` to your index.html:

## Example

[See it in action!](https://jsbin.com/hoputodequ/edit?html,output)
[See it in action!](https://jsbin.com/pajuhaf/edit?html,output)

## Quick start

Expand Down
2 changes: 1 addition & 1 deletion dist/ng-currency.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/ng-currency.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ng-currency",
"version": "1.1.6",
"version": "1.1.7",
"main": "dist/ng-currency.js",
"description": "Directive that works in conjunction with currency filter.",
"homepage": "http://alaguirre.com",
Expand Down
15 changes: 11 additions & 4 deletions src/ng-currency.directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ export default function ngCurrency($filter, $locale) {
return {
require: 'ngModel',
link: (scope, element, attrs, controller) => {
let hardCap, min, max, currencySymbol, ngRequired = ['', 'true'].indexOf(attrs.ngRequired) !== -1;
let hardCap, min, max, currencySymbol;
let ngRequired = ['', 'true'].indexOf(attrs.ngRequired) !== -1;
let active = true;
let fraction = 2;

Expand Down Expand Up @@ -83,14 +84,20 @@ export default function ngCurrency($filter, $locale) {
function reformat() {
if (active) {
let value;
let updateOn;
let updateOn, debounce;
if (controller.$options) {
// HACK(cecilia-sanare): this is to maintain backwards compatability with Angular 1.5.9 and lower.
// TODO(cecilia-sanare): This should be removed when ngCurrency does a 2.0.0 release
// Reference: https://github.com/angular/angular.js/commit/296cfce40c25e9438bfa46a0eb27240707a10ffa
updateOn = controller.$options.getOption ? controller.$options.getOption('updateOn') : controller.$options.updateOn;
if (controller.$options.getOption) {
updateOn = controller.$options.getOption('updateOn');
debounce = controller.$options.getOption('debounce');
} else {
updateOn = controller.$options.updateOn;
debounce = controller.$options.debounce;
}
}
if (updateOn === 'blur') {
if (updateOn === 'blur' || debounce) {
value = controller.$viewValue;
for (let i = controller.$parsers.length - 1; i >= 0; i--) {
value = controller.$parsers[i](value);
Expand Down
127 changes: 85 additions & 42 deletions test/ng-currency/ng-currency.directive.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'ng-select-all-on-focus';
import ngCurrency from '../../src/ng-currency.module.js';
import defaults from './templates/defaults.html';
import variables from './templates/variables.html';
import variablesUpdateOnBlur from './templates/variables-update-on-blur.html';
import centsToDollars from './templates/cents-to-dollars.html';
import selectAllOnFocus from './templates/select-all-on-focus.html';

Expand Down Expand Up @@ -283,7 +282,7 @@ describe('ngCurrency directive tests', () => {
scope.$broadcast('currencyRedraw');

expect(controller.$validate.callCount).toEqual(1);
})
});
});
});

Expand Down Expand Up @@ -410,51 +409,95 @@ describe('ngCurrency directive tests', () => {
});

describe('Model Options', () => {
beforeEach(angular.mock.inject(($rootScope, $compile, $timeout) => {
scope = $rootScope.$new();
scope.value = 0;
scope.active = true;
scope.currencySymbol = '$';
scope.required = true;
scope.$digest();
element = $compile(variablesUpdateOnBlur)(scope);
element = element.find('input');
$timeout.flush();
let $timeout;
beforeEach(angular.mock.inject((_$timeout_) => {
$timeout = _$timeout_;
}));

it('should remain pristine when updating via the scope value', () => {
scope.value = 123.45;
scope.$digest();
element.triggerHandler('input');
element.triggerHandler('blur');
expect(element.hasClass('ng-pristine')).toBeTruthy();
expect(scope.form.currency.$pristine).toBeTruthy();
expect(scope.form.currency.$dirty).toBeFalsy();
expect(scope.form.$pristine).toBeTruthy();
expect(scope.form.$dirty).toBeFalsy();
expect(element.val()).toEqual('$123.45');
});
describe('updateOn: blur', () => {
beforeEach(angular.mock.inject(($rootScope, $compile, $timeout) => {
scope = $rootScope.$new();
scope.value = 0;
scope.active = true;
scope.currencySymbol = '$';
scope.required = true;
scope.modelOptions = {
updateOn: 'blur'
};
scope.$digest();
element = $compile(variables)(scope);
element = element.find('input');
$timeout.flush();
}));

it('should support updating on blur', () => {
element.val('$123.45');
element.triggerHandler('input');
element.triggerHandler('blur');
expect(scope.value).toEqual(123.45);
expect(element.val()).toEqual('$123.45');
scope.min = 0.01;
scope.max = 100;
scope.$digest();
expect(scope.value).toEqual(undefined);
expect(element.val()).toEqual('$123.45');
it('should remain pristine when updating via the scope value', () => {
scope.value = 123.45;
scope.$digest();
element.triggerHandler('input');
element.triggerHandler('blur');
expect(element.hasClass('ng-pristine')).toBeTruthy();
expect(scope.form.currency.$pristine).toBeTruthy();
expect(scope.form.currency.$dirty).toBeFalsy();
expect(scope.form.$pristine).toBeTruthy();
expect(scope.form.$dirty).toBeFalsy();
expect(element.val()).toEqual('$123.45');
});

it('should support updating on blur', () => {
element.val('$123.45');
element.triggerHandler('input');
expect(scope.value).toEqual(0);
expect(element.val()).toEqual('$123.45');
element.triggerHandler('blur');
expect(scope.value).toEqual(123.45);
expect(element.val()).toEqual('$123.45');
scope.min = 0.01;
scope.max = 100;
scope.$digest();
expect(scope.value).toEqual(undefined);
expect(element.val()).toEqual('$123.45');
});

it('should support a custom fraction value when updating on blur', () => {
scope.fraction = 5;
scope.$digest();
element.val('$123.45678');
element.triggerHandler('input');
expect(scope.value).toEqual(0);
expect(element.val()).toEqual('$123.45678');
element.triggerHandler('blur');
expect(element.val()).toEqual('$123.45678');
});
});

it('should support a custom fraction value when updating on blur', () => {
scope.fraction = 5;
scope.$digest();
element.val('$123.45678');
element.triggerHandler('input');
element.triggerHandler('blur');
expect(element.val()).toEqual('$123.45678');
describe('debounce', () => {
beforeEach(angular.mock.inject(($rootScope, $compile, $timeout) => {
scope = $rootScope.$new();
scope.value = 0;
scope.active = true;
scope.currencySymbol = '$';
scope.required = true;
scope.modelOptions = {
debounce: 1000
};
scope.$digest();
element = $compile(variables)(scope);
element = element.find('input');
$timeout.flush();
}));

it('should support updating on debounce', () => {
element.val('$123.45');
element.triggerHandler('input');
expect(scope.value).toEqual(0);
expect(element.val()).toEqual('$123.45');
element.triggerHandler('blur');
expect(scope.value).toEqual(0);
expect(element.val()).toEqual('$123.45');
$timeout.flush();
expect(scope.value).toEqual(123.45);
expect(element.val()).toEqual('$123.45');
});
});
});

Expand Down
12 changes: 0 additions & 12 deletions test/ng-currency/templates/variables-update-on-blur.html

This file was deleted.

0 comments on commit c912c55

Please sign in to comment.