From ad7287b5e62d1991832d522e359debac35549ab2 Mon Sep 17 00:00:00 2001 From: Kmaschta Date: Wed, 8 Mar 2017 15:47:39 +0100 Subject: [PATCH] Fix DateField watches --- Makefile | 4 +++ .../ng-admin/Crud/field/maDateField.js | 31 +++++++++++++++---- .../test/unit/Crud/field/maDateFieldSpec.js | 14 +++++++++ 3 files changed, 43 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index e6079fac..6b50fce1 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ transpile: cp -Rf ./src/sass/ lib/sass/ build: + rm -rf build/* make transpile NODE_ENV=production ./node_modules/webpack/bin/webpack.js -p --optimize-minimize --optimize-occurence-order --optimize-dedupe --progress --devtool source-map cp -Rf build examples/blog/ @@ -33,6 +34,9 @@ check-only-in-tests: test-unit: ./node_modules/.bin/karma start src/javascripts/test/karma.conf.js --single-run +test-unit-watch: + ./node_modules/.bin/karma start src/javascripts/test/karma.conf.js --watch + test-e2e: prepare-test-e2e ./node_modules/.bin/protractor src/javascripts/test/protractor.conf.js diff --git a/src/javascripts/ng-admin/Crud/field/maDateField.js b/src/javascripts/ng-admin/Crud/field/maDateField.js index 9fd9c63e..2957ab1a 100644 --- a/src/javascripts/ng-admin/Crud/field/maDateField.js +++ b/src/javascripts/ng-admin/Crud/field/maDateField.js @@ -1,3 +1,11 @@ +const valueToRawValue = (value) => { + if (value instanceof Date) { + return value; + } + + return new Date(value); +}; + /** * Edition field for a date - a text input with a datepicker. * @@ -13,23 +21,34 @@ export default function maDateField() { link: function(scope, element) { var field = scope.field(); scope.name = field.name(); - scope.rawValue = scope.value == null ? null : (scope.value instanceof Date ? scope.value : new Date(scope.value)); + scope.rawValue = scope.value == null ? null : valueToRawValue(scope.value); - scope.$watch('rawValue', function(newValue) { - scope.value = field.parse()(newValue); + scope.$watch('rawValue', function(newRawValue) { + const newValue = field.parse()(newRawValue); + + if (!angular.equals(scope.value, newValue)) { + scope.value = newValue; + } }); scope.$watch('value', (newValue, oldValue) => { - if (newValue === oldValue) { + if (angular.equals(newValue, oldValue)) { return; } if (!newValue) { - scope.rawValue = null; + if (scope.rawValue !== null) { + scope.rawValue = null; + } + return; } - scope.rawValue = scope.value instanceof Date ? scope.value : new Date(scope.value); + const newRawValue = valueToRawValue(scope.value); + + if (!angular.equals(scope.rawValue, newRawValue)) { + scope.rawValue = newRawValue; + } }); scope.format = field.format(); diff --git a/src/javascripts/test/unit/Crud/field/maDateFieldSpec.js b/src/javascripts/test/unit/Crud/field/maDateFieldSpec.js index d38a544f..e1a16455 100644 --- a/src/javascripts/test/unit/Crud/field/maDateFieldSpec.js +++ b/src/javascripts/test/unit/Crud/field/maDateFieldSpec.js @@ -75,4 +75,18 @@ describe('directive: date-field', function() { expect(element.find('input').eq(0).val()).toBe('2010-01-01'); }); + + it('should update value when updating the rawValue directly', () => { + const now = new Date('2016-09-18'); + scope.value = now; + scope.field = new DateField(); + + const element = $compile(directiveUsage)(scope); + const isolatedScope = element.isolateScope(); + + isolatedScope.rawValue = new Date('2010-01-01'); + isolatedScope.$digest(); + + expect(isolatedScope.value).toBe('2010-01-01'); + }); });