From c0bb8bd4bc7ac901ec5941b3c6209934d1ec6238 Mon Sep 17 00:00:00 2001 From: Debarshi Bhaumik Date: Mon, 16 Apr 2018 16:57:37 +0530 Subject: [PATCH] Fix select2 value updation for crmAddName Directive --- ang/crmCaseType.js | 9 +++------ tests/karma/unit/crmCaseTypeSpec.js | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/ang/crmCaseType.js b/ang/crmCaseType.js index e3b4a16c6a..e58f371943 100644 --- a/ang/crmCaseType.js +++ b/ang/crmCaseType.js @@ -103,7 +103,9 @@ }; $(input).crmSelect2({ - data: scope[attrs.crmOptions], + data: function () { + return { results: scope[attrs.crmOptions] }; + }, createSearchChoice: function(term) { return {id: term, text: term + ' (' + ts('new') + ')'}; }, @@ -116,11 +118,6 @@ scope.$evalAsync('_resetSelection()'); e.preventDefault(); }); - - scope.$watch(attrs.crmOptions, function(value) { - $(input).select2('data', scope[attrs.crmOptions]); - $(input).select2('val', ''); - }); } }; }); diff --git a/tests/karma/unit/crmCaseTypeSpec.js b/tests/karma/unit/crmCaseTypeSpec.js index 9c87916489..a7295b6944 100644 --- a/tests/karma/unit/crmCaseTypeSpec.js +++ b/tests/karma/unit/crmCaseTypeSpec.js @@ -253,6 +253,34 @@ describe('crmCaseType', function() { expect(newSet.timeline).toBe('1'); expect(newSet.label).toBe('Timeline #2'); }); + }); + + describe('crmAddName', function () { + var scope; + var element; + + beforeEach(inject(function($rootScope, $compile) { + scope = $rootScope.$new(); + scope.activityTypeOptions = [1, 2, 3]; + element = ''; + + spyOn(CRM.$.fn, 'crmSelect2').and.callThrough(); + element = $compile(element)(scope); + scope.$digest(); + })); + + describe('when initialized', function () { + var returnValue; + + beforeEach (function () { + var dataFunction = CRM.$.fn.crmSelect2.calls.argsFor(0)[0].data; + returnValue = dataFunction(); + }); + + it('updates the UI with updated value of scope variable', function () { + expect(returnValue).toEqual({ results: scope.activityTypeOptions }); + }); + }); }); }); -- 2.25.1