+ describe('when clearing the activity\'s default assignee type values', function() {
+ var activity;
+
+ beforeEach(function() {
+ activity = {
+ default_assignee_relationship: 1,
+ default_assignee_contact: 2
+ };
+
+ scope.clearActivityDefaultAssigneeValues(activity);
+ });
+
+ it('clears the default assignee relationship for the activity', function() {
+ expect(activity.default_assignee_relationship).toBe(null);
+ });
+
+ it('clears the default assignee contact for the activity', function() {
+ expect(activity.default_assignee_contact).toBe(null);
+ });
+ });
+
+ describe('when adding a new activity to a set', function() {
+ var activitySet;
+
+ beforeEach(function() {
+ activitySet = { activityTypes: [] };
+ scope.activityTypes = { comment: { label: 'Add a new comment' } };
+
+ scope.addActivity(activitySet, 'comment');
+ });
+
+ it('adds a new Comment activity to the set', function() {
+ expect(activitySet.activityTypes[0]).toEqual({
+ name: 'comment',
+ label: scope.activityTypes.comment.label,
+ status: 'Scheduled',
+ reference_activity: 'Open Case',
+ reference_offset: '1',
+ reference_select: 'newest',
+ default_assignee_type: defaultAssigneeDefaultValue.value
+ });
+ });
+ });
+
+ describe('when creating a new workflow', function() {
+ beforeEach(inject(function ($controller) {
+ apiCalls.caseType = null;
+
+ ctrl = $controller('CaseTypeCtrl', {$scope: scope, apiCalls: apiCalls});
+ }));
+
+ it('sets default values for the case type title, name, and active status', function() {
+ expect(scope.caseType).toEqual(jasmine.objectContaining({
+ title: '',
+ name: '',
+ is_active: '1'
+ }));
+ });
+
+ it('adds an Open Case activty to the default activty set', function() {
+ expect(scope.caseType.definition.activitySets[0].activityTypes).toEqual([{
+ name: 'Open Case',
+ label: 'Open Case',
+ status: 'Completed',
+ default_assignee_type: defaultAssigneeDefaultValue.value
+ }]);
+ });
+ });
+ });
+
+ describe('crmAddName', function () {
+ var element;
+
+ beforeEach(function() {
+ scope = $rootScope.$new();
+ scope.activityTypeOptions = [1, 2, 3];
+ element = '<span crm-add-name crm-options="activityTypeOptions"></span>';
+
+ 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 });
+ });
+ });
+ });
+
+ describe('crmEditableTabTitle', function () {
+ var element, titleLabel, penIcon, saveButton, cancelButton;
+
+ beforeEach(function() {
+ scope = $rootScope.$new();
+ element = '<div crm-editable-tab-title title="Click to edit">' +
+ '<span>{{ activitySet.label }}</span>' +
+ '</div>';
+
+ scope.activitySet = { label: 'Title'};
+ element = $compile(element)(scope);
+
+ titleLabel = $(element).find('span');
+ penIcon = $(element).find('i.fa-pencil');
+ saveButton = $(element).find('button[type=button]');
+ cancelButton = $(element).find('button[type=cancel]');
+
+ scope.$digest();
+ });
+
+ describe('when initialized', function () {
+ it('hides the save and cancel button', function () {
+ expect(saveButton.parent().css('display') === 'none').toBe(true);
+ expect(cancelButton.parent().css('display') === 'none').toBe(true);
+ });
+ });
+
+ describe('when clicked on title label', function () {
+ beforeEach(function () {
+ titleLabel.click();
+ });
+
+ it('hides the pen icon', function () {
+ expect(penIcon.css('display') === 'none').toBe(true);
+ });
+
+ it('shows the save button', function () {
+ expect(saveButton.parent().css('display') !== 'none').toBe(true);
+ });
+
+ it('makes the title editable', function () {
+ expect(titleLabel.attr('contenteditable')).toBe('true');
+ });
+ });
+
+ describe('when clicked outside of the editable area', function () {
+ beforeEach(function () {
+ titleLabel.click();
+ titleLabel.text('Updated Title');
+ titleLabel.blur();
+ $timeout.flush();
+ scope.$digest();
+ });
+
+ it('shows the pen icon', function () {
+ expect(penIcon.css('display') !== 'none').toBe(true);
+ });
+
+ it('hides the save and cancel button', function () {
+ expect(saveButton.parent().css('display') === 'none').toBe(true);
+ expect(cancelButton.parent().css('display') === 'none').toBe(true);
+ });
+
+ it('makes the title non editable', function () {
+ expect(titleLabel.attr('contenteditable')).not.toBe('true');
+ });
+
+ it('does not update the title in angular context', function () {
+ expect(scope.activitySet.label).toBe('Title');
+ });
+ });
+
+ describe('when ESCAPE key is pressed while typing', function () {
+ beforeEach(function () {
+ var eventObj = $.Event('keydown');
+ eventObj.key = 'Escape';
+
+ titleLabel.click();
+ titleLabel.text('Updated Title');
+ titleLabel.trigger(eventObj);
+ scope.$digest();
+ });
+
+ it('shows the pen icon', function () {
+ expect(penIcon.css('display') !== 'none').toBe(true);
+ });
+
+ it('hides the save and cancel button', function () {
+ expect(saveButton.parent().css('display') === 'none').toBe(true);
+ expect(cancelButton.parent().css('display') === 'none').toBe(true);
+ });
+
+ it('makes the title non editable', function () {
+ expect(titleLabel.attr('contenteditable')).not.toBe('true');
+ });
+
+ it('does not update the title', function () {
+ expect(scope.activitySet.label).toBe('Title');
+ });
+ });
+
+ describe('when ENTER key is pressed while typing', function () {
+ beforeEach(function () {
+ var eventObj = $.Event('keydown');
+ eventObj.key = 'Enter';
+
+ titleLabel.click();
+ titleLabel.text('Updated Title');
+ titleLabel.trigger(eventObj);
+ scope.$digest();
+ });
+
+ it('shows the pen icon', function () {
+ expect(penIcon.css('display') !== 'none').toBe(true);
+ });
+
+ it('hides the save and cancel button', function () {
+ expect(saveButton.parent().css('display') === 'none').toBe(true);
+ expect(cancelButton.parent().css('display') === 'none').toBe(true);
+ });
+
+ it('makes the title non editable', function () {
+ expect(titleLabel.attr('contenteditable')).not.toBe('true');
+ });
+
+ it('updates the title in angular context', function () {
+ expect(scope.activitySet.label).toBe('Updated Title');
+ });
+ });
+
+ describe('when SAVE button is clicked', function () {
+ beforeEach(function () {
+ titleLabel.click();
+ titleLabel.text('Updated Title');
+ saveButton.click();
+ scope.$digest();
+ });
+
+ it('shows the pen icon', function () {
+ expect(penIcon.css('display') !== 'none').toBe(true);
+ });
+
+ it('hides the save and cancel button', function () {
+ expect(saveButton.parent().css('display') === 'none').toBe(true);
+ expect(cancelButton.parent().css('display') === 'none').toBe(true);
+ });
+
+ it('makes the title non editable', function () {
+ expect(titleLabel.attr('contenteditable')).not.toBe('true');
+ });
+
+ it('updates the title in angular context', function () {
+ expect(scope.activitySet.label).toBe('Updated Title');
+ });
+ });
+
+ describe('when CANCEL button is clicked', function () {
+ beforeEach(function () {
+ titleLabel.click();
+ titleLabel.text('Updated Title');
+ cancelButton.click();
+ scope.$digest();
+ });
+
+ it('shows the pen icon', function () {
+ expect(penIcon.css('display') !== 'none').toBe(true);
+ });
+
+ it('hides the save and cancel button', function () {
+ expect(saveButton.parent().css('display') === 'none').toBe(true);
+ expect(cancelButton.parent().css('display') === 'none').toBe(true);
+ });
+
+ it('makes the title non editable', function () {
+ expect(titleLabel.attr('contenteditable')).not.toBe('true');
+ });
+
+ it('does not update the title in angular context', function () {
+ expect(scope.activitySet.label).toBe('Title');
+ });
+ });
+ });
+
+ describe('CaseTypeListCtrl', function() {
+ var caseTypes, crmApiSpy;
+
+ beforeEach(function() {
+ caseTypes = {
+ values: {
+ 1: { id: 1 },
+ 2: { id: 2 },
+ 3: { id: 3 }
+ }
+ };
+ crmApiSpy = jasmine.createSpy('crmApi').and.returnValue($q.resolve());
+ scope = $rootScope.$new();
+ ctrl = $controller('CaseTypeListCtrl', {
+ $scope: scope,
+ caseTypes: caseTypes,
+ crmApi: crmApiSpy
+ });
+ });
+
+ it('should store an index of case types', function() {
+ expect(scope.caseTypes).toEqual(caseTypes.values);
+ });
+
+ describe('toggleCaseType', function() {
+ var caseType = { id: _.uniqueId() };
+
+ describe('when the case is active', function() {
+ beforeEach(function() {
+ caseType.is_active = '1';
+
+ scope.toggleCaseType(caseType);
+ });
+
+ it('sets the case type as inactive', function() {
+ expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'create', jasmine.objectContaining({
+ id: caseType.id,
+ is_active: '0'
+ }), true);
+ });
+ });
+
+ describe('when the case is inactive', function() {
+ beforeEach(function() {
+ caseType.is_active = '0';
+
+ scope.toggleCaseType(caseType);
+ });
+
+ it('sets the case type as active', function() {
+ expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'create', jasmine.objectContaining({
+ id: caseType.id,
+ is_active: '1'
+ }), true);
+ });
+ });
+ });
+
+ describe('deleteCaseType', function() {
+ var caseType = { id: _.uniqueId() };
+
+ beforeEach(function() {
+ crmApiSpy.and.returnValue($q.resolve(caseType));
+ scope.caseTypes[caseType.id] = caseType;
+
+ scope.deleteCaseType(caseType);
+ scope.$digest();
+ });
+
+ describe('when the case type can be deleted', function() {
+ it('deletes the case from the api', function() {
+ expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'delete', { id: caseType.id }, jasmine.any(Object));
+ });
+
+ it('removes the case type from the list', function() {
+ expect(scope.caseTypes[caseType.id]).toBeUndefined();
+ });
+ });
+
+ describe('when the case type cannot be delted', function() {
+ var error = { error_message: 'Error Message' };
+
+ beforeEach(function() {
+ var errorHandler;
+
+ crmApiSpy.and.returnValue($q.reject(error));
+ scope.caseTypes[caseType.id] = caseType;
+
+ spyOn(CRM, 'alert');
+ scope.deleteCaseType(caseType);
+ scope.$digest();
+
+ errorHandler = crmApiSpy.calls.mostRecent().args[3].error;
+ errorHandler(error);
+ });
+
+ it('displays the error message', function() {
+ expect(CRM.alert).toHaveBeenCalledWith(error.error_message, 'Error', 'error');
+ });
+ });
+
+ describe('revertCaseType', function() {
+ var caseType = {
+ id: _.uniqueId(),
+ definition: {},
+ is_forked: '1'
+ };
+
+ describe('when reverting a case type', function() {
+ beforeEach(function() {
+ scope.revertCaseType(caseType);
+ });
+
+ it('resets the case type information using the api', function() {
+ expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'create', jasmine.objectContaining({
+ id: caseType.id,
+ definition: 'null',
+ is_forked: '0'
+ }), true);
+ });
+ });
+ });
+ });