From e6b5f65fe8cab95c928983d59bd32ca760966ae5 Mon Sep 17 00:00:00 2001 From: DominikNoga Date: Mon, 22 Apr 2024 16:21:30 +0200 Subject: [PATCH] Removed default sorting from the table components in order to prevent UI errors --- .../admin-orderable-list.routes.js | 3 - .../openlmis-table-sorting.service.js | 91 +++++----- .../order-fulfillment.routes.js | 3 - .../order-fulfillment.routes.spec.js | 6 +- src/order-view/order-view.routes.js | 3 - .../requisition-approval.routes.js | 3 - .../requisition-approval.routes.spec.js | 166 ++++++++++++++++++ 7 files changed, 218 insertions(+), 57 deletions(-) create mode 100644 src/requisition-approval/requisition-approval.routes.spec.js diff --git a/src/admin-orderable-list/admin-orderable-list.routes.js b/src/admin-orderable-list/admin-orderable-list.routes.js index 34e82fd..5eea272 100644 --- a/src/admin-orderable-list/admin-orderable-list.routes.js +++ b/src/admin-orderable-list/admin-orderable-list.routes.js @@ -27,9 +27,6 @@ showInNavigation: true, label: 'adminOrderableList.products', url: '/orderables?code&name&description&program&page&size&sort', - params: { - sort: 'fullProductName,asc' - }, controller: 'OrderableListController', templateUrl: 'admin-orderable-list/orderable-list.html', controllerAs: 'vm', diff --git a/src/openlmis-table/openlmis-table-component/services/openlmis-table-sorting-service/openlmis-table-sorting.service.js b/src/openlmis-table/openlmis-table-component/services/openlmis-table-sorting-service/openlmis-table-sorting.service.js index cc5d48a..35ed5d4 100644 --- a/src/openlmis-table/openlmis-table-component/services/openlmis-table-sorting-service/openlmis-table-sorting.service.js +++ b/src/openlmis-table/openlmis-table-component/services/openlmis-table-sorting-service/openlmis-table-sorting.service.js @@ -30,12 +30,14 @@ openlmisTableSortingService.$inject = ['SORTING_SERVICE_CONSTANTS', '$state', '$stateParams', 'alertService']; function openlmisTableSortingService(SORTING_SERVICE_CONSTANTS, $state, $stateParams, alertService) { - var sortingProperites = { + var EMPTY_SORTING_PROPERTIES = { isSortedBy: undefined, - sortingOrder: undefined, - headerClass: undefined + sortingOrder: '', + headerClass: '' }; + var sortingProperties = angular.copy(EMPTY_SORTING_PROPERTIES); + return { sortTable: sortTable, setHeadersClasses: setHeadersClasses, @@ -61,7 +63,7 @@ var propertyToOrder = propertyPathParts[0]; var stateParams = JSON.parse(JSON.stringify($stateParams)); - setSortingProperties(selectedColumn.propertyPath); + setSortingProperties(propertyToOrder); stateParams.sort = getSortingParamValue(propertyToOrder); $state.go($state.current.name, stateParams); } else { @@ -81,38 +83,34 @@ } function getSortingParamValue(propertyToOrder) { - if (typeof sortingProperites.isSortedBy === 'undefined') { + if (sortingProperties.isSortedBy === undefined) { return undefined; } - return propertyToOrder + ',' + sortingProperites.sortingOrder; + return propertyToOrder + ',' + sortingProperties.sortingOrder; } function setSortingProperties(propertyToOrder) { - if (sortingProperites.isSortedBy === undefined || - sortingProperites.isSortedBy !== propertyToOrder + if (sortingProperties.isSortedBy === undefined || + sortingProperties.isSortedBy !== propertyToOrder ) { - setSortingPropertiesValue( - propertyToOrder, - SORTING_SERVICE_CONSTANTS.ASC, - SORTING_SERVICE_CONSTANTS.SORT_ASC_CLASS - ); - } else if (sortingProperites.sortingOrder === SORTING_SERVICE_CONSTANTS.ASC) { - setSortingPropertiesValue( - propertyToOrder, - SORTING_SERVICE_CONSTANTS.DESC, - SORTING_SERVICE_CONSTANTS.SORT_DESC_CLASS - ); + setSortingPropertiesValue({ + isSortedBy: propertyToOrder, + sortingOrder: SORTING_SERVICE_CONSTANTS.ASC, + headerClass: SORTING_SERVICE_CONSTANTS.SORT_ASC_CLASS + }); + } else if (sortingProperties.sortingOrder === SORTING_SERVICE_CONSTANTS.ASC) { + setSortingPropertiesValue({ + isSortedBy: propertyToOrder, + sortingOrder: SORTING_SERVICE_CONSTANTS.DESC, + headerClass: SORTING_SERVICE_CONSTANTS.SORT_DESC_CLASS + }); } else { - setSortingPropertiesValue(undefined, '', ''); + setSortingPropertiesValue(angular.copy(EMPTY_SORTING_PROPERTIES)); } } - function setSortingPropertiesValue(isSortedBy, sortingOrder, headerClass) { - sortingProperites = { - isSortedBy: isSortedBy, - sortingOrder: sortingOrder, - headerClass: headerClass - }; + function setSortingPropertiesValue(sortingPropertiesValue) { + sortingProperties = angular.copy(sortingPropertiesValue); } /** @@ -134,34 +132,45 @@ } function setInitialSortingProperties() { - var sortParam = $stateParams.sort; + var sortParam = Array.isArray($stateParams.sort) ? + $stateParams.sort[0] : + $stateParams.sort; if (sortParam) { - if (typeof sortParam === 'object' && sortParam.length !== undefined) { - sortParam = sortParam[0]; - } - var sortParamParts = sortParam.split(','); - var isSortedBy = sortParamParts[0]; - // Removing whitespaces from sorting param - var sortingOrder = sortParamParts[1].split(' ').join(''); - var headerClass = sortingOrder === SORTING_SERVICE_CONSTANTS.ASC ? - SORTING_SERVICE_CONSTANTS.SORT_ASC_CLASS : SORTING_SERVICE_CONSTANTS.SORT_DESC_CLASS; - - setSortingPropertiesValue(isSortedBy, sortingOrder, headerClass); + sortingProperties = getSortingPropertiesFromParam(sortParam); } else { - setSortingPropertiesValue(undefined, '', ''); + setSortingPropertiesValue(EMPTY_SORTING_PROPERTIES); } } + function getSortingPropertiesFromParam(sortParam) { + if (sortParam === undefined) { + setSortingPropertiesValue(EMPTY_SORTING_PROPERTIES); + } + + var sortParamParts = sortParam.split(','); + var isSortedBy = sortParamParts[0]; + // Removing whitespaces from sorting param + var sortingOrder = sortParamParts[1].split(' ').join(''); + var headerClass = sortingOrder === SORTING_SERVICE_CONSTANTS.ASC ? + SORTING_SERVICE_CONSTANTS.SORT_ASC_CLASS : SORTING_SERVICE_CONSTANTS.SORT_DESC_CLASS; + + return { + isSortedBy: isSortedBy, + sortingOrder: sortingOrder, + headerClass: headerClass + }; + } + function getColumnClass(column) { var baseClass = column.headerClasses ? column.headerClasses : ''; return isSortedByColumn(column.propertyPath) ? - baseClass + ' ' + sortingProperites.headerClass : baseClass; + baseClass + ' ' + sortingProperties.headerClass : baseClass; } function isSortedByColumn(propertyPath) { - return propertyPath === sortingProperites.isSortedBy; + return propertyPath === sortingProperties.isSortedBy; } /** diff --git a/src/order-fulfillment/order-fulfillment.routes.js b/src/order-fulfillment/order-fulfillment.routes.js index 41193f0..0f09ce4 100644 --- a/src/order-fulfillment/order-fulfillment.routes.js +++ b/src/order-fulfillment/order-fulfillment.routes.js @@ -36,9 +36,6 @@ FULFILLMENT_RIGHTS.SHIPMENTS_VIEW, FULFILLMENT_RIGHTS.SHIPMENTS_EDIT ], - params: { - sort: 'createdDate,desc' - }, areAllRightsRequired: false, resolve: { supervisedFacilities: function(facilityFactory) { diff --git a/src/order-fulfillment/order-fulfillment.routes.spec.js b/src/order-fulfillment/order-fulfillment.routes.spec.js index 9d1ad12..f8d933e 100644 --- a/src/order-fulfillment/order-fulfillment.routes.spec.js +++ b/src/order-fulfillment/order-fulfillment.routes.spec.js @@ -106,8 +106,7 @@ describe('openlmis.orders.fulfillment state', function() { programId: undefined, status: [ORDER_STATUS.FULFILLING, ORDER_STATUS.ORDERED], page: '0', - size: '10', - sort: 'createdDate,desc' + size: '10' }); }); @@ -121,8 +120,7 @@ describe('openlmis.orders.fulfillment state', function() { programId: undefined, status: [ORDER_STATUS.ORDERED], page: '0', - size: '10', - sort: 'createdDate,desc' + size: '10' }); }); diff --git a/src/order-view/order-view.routes.js b/src/order-view/order-view.routes.js index 821cfbf..167d682 100644 --- a/src/order-view/order-view.routes.js +++ b/src/order-view/order-view.routes.js @@ -78,9 +78,6 @@ orders: function(paginationService, orderRepository, $stateParams) { return paginationService.registerUrl($stateParams, function(stateParams) { if (stateParams.supplyingFacilityId) { - if (stateParams.sort === undefined) { - stateParams.sort = 'createdDate,desc'; - } return orderRepository.search(stateParams); } return undefined; diff --git a/src/requisition-approval/requisition-approval.routes.js b/src/requisition-approval/requisition-approval.routes.js index dd9e44b..866b2dd 100644 --- a/src/requisition-approval/requisition-approval.routes.js +++ b/src/requisition-approval/requisition-approval.routes.js @@ -30,9 +30,6 @@ isOffline: true, label: 'requisitionApproval.approve', url: '/approvalList?page&size&program&offline&sort', - params: { - sort: ['emergency,desc', 'authorizedDate,desc'] - }, controller: 'RequisitionApprovalListController', controllerAs: 'vm', templateUrl: 'requisition-approval/requisition-approval-list.html', diff --git a/src/requisition-approval/requisition-approval.routes.spec.js b/src/requisition-approval/requisition-approval.routes.spec.js new file mode 100644 index 0000000..86b0646 --- /dev/null +++ b/src/requisition-approval/requisition-approval.routes.spec.js @@ -0,0 +1,166 @@ +/* + * This program is part of the OpenLMIS logistics management information system platform software. + * Copyright © 2017 VillageReach + * + * This program is free software: you can redistribute it and/or modify it under the terms + * of the GNU Affero General Public License as published by the Free Software Foundation, either + * version 3 of the License, or (at your option) any later version. + *   + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  + * See the GNU Affero General Public License for more details. You should have received a copy of + * the GNU Affero General Public License along with this program. If not, see + * http://www.gnu.org/licenses.  For additional information contact info@OpenLMIS.org.  + */ + +describe('openlmis.requisitions.approvalList', function() { + + beforeEach(function() { + module('requisition-approval'); + module('requisition-view-tab'); + + inject(function($injector) { + this.$state = $injector.get('$state'); + this.$q = $injector.get('$q'); + this.$rootScope = $injector.get('$rootScope'); + this.$location = $injector.get('$location'); + + this.REQUISITION_RIGHTS = $injector.get('REQUISITION_RIGHTS'); + this.BATCH_APPROVE_SCREEN_FEATURE_FLAG = $injector.get('BATCH_APPROVE_SCREEN_FEATURE_FLAG'); + this.REQUISITION_STATUS = $injector.get('REQUISITION_STATUS'); + + this.requisitionService = $injector.get('requisitionService'); + this.authorizationService = $injector.get('authorizationService'); + this.alertService = $injector.get('alertService'); + this.featureFlagService = $injector.get('featureFlagService'); + this.requisitionApprovalService = $injector.get('requisitionApprovalService'); + this.permissionService = $injector.get('permissionService'); + + this.UserDataBuilder = $injector.get('UserDataBuilder'); + this.ProgramDataBuilder = $injector.get('ProgramDataBuilder'); + this.RequisitionDataBuilder = $injector.get('RequisitionDataBuilder'); + this.PageDataBuilder = $injector.get('PageDataBuilder'); + }); + + this.goToUrl = goToUrl; + this.getResolvedValue = getResolvedValue; + + this.requisitions = [ + new this.RequisitionDataBuilder().build(), + new this.RequisitionDataBuilder().build() + ]; + + this.cachedRequisitions = [ + new this.RequisitionDataBuilder().build(), + new this.RequisitionDataBuilder().build() + ]; + + this.programs = [ + new this.ProgramDataBuilder().build(), + new this.ProgramDataBuilder().build() + ]; + + this.user = new this.UserDataBuilder().build(); + //eslint-disable-next-line camelcase + this.user.user_id = this.user.id; + + this.requisitionsPage = new this.PageDataBuilder() + .withContent(this.requisitions) + .build(); + + this.cachedRequisitionsPage = new this.PageDataBuilder() + .withContent(this.cachedRequisitions) + .build(); + + spyOn(this.requisitionService, 'forApproval').andReturn(this.$q.resolve(this.requisitionsPage)); + spyOn(this.requisitionService, 'search').andReturn(this.$q.resolve(this.cachedRequisitionsPage)); + spyOn(this.authorizationService, 'getUser').andReturn(this.$q.resolve(this.user)); + spyOn(this.featureFlagService, 'get').andReturn(true); + spyOn(this.alertService, 'error'); + spyOn(this.requisitionApprovalService, 'getPrograms').andReturn(this.$q.resolve(this.programs)); + spyOn(this.permissionService, 'hasRoleWithRightAndFacility').andReturn(this.$q.resolve(true)); + }); + + it('should resolve isBatchApproveScreenActive', function() { + this.goToUrl('/requisitions/approvalList'); + + expect(this.getResolvedValue('isBatchApproveScreenActive')).toEqual(true); + expect(this.featureFlagService.get).toHaveBeenCalledWith(this.BATCH_APPROVE_SCREEN_FEATURE_FLAG); + }); + + describe('programs', function() { + + it('should resolve programs', function() { + this.goToUrl('requisitions/approvalList'); + + expect(this.getResolvedValue('programs')).toEqual(this.programs); + expect(this.requisitionApprovalService.getPrograms).toHaveBeenCalled(); + }); + + }); + + it('should resolve selected program', function() { + this.goToUrl('requisitions/approvalList?program=' + this.programs[0].id); + + expect(this.getResolvedValue('selectedProgram')).toEqual(this.programs[0]); + }); + + describe('requisitions', function() { + + //This seems to be a bug breaking this functionality in offline... + it('should fetch requisitions online if no program filter is specified', function() { + this.goToUrl('requisitions/approvalList'); + + expect(this.getResolvedValue('requisitions')).toEqual(this.requisitions); + expect(this.requisitionService.search).not.toHaveBeenCalled(); + expect(this.requisitionService.forApproval).toHaveBeenCalledWith({ + page: 0, + size: 10, + program: undefined, + offline: undefined + }); + }); + + it('should fetch requisitions from cache if when offline', function() { + this.goToUrl('requisitions/approvalList?offline=true&program=' + this.programs[0].id); + + expect(this.getResolvedValue('requisitions')).toEqual(this.cachedRequisitions); + expect(this.requisitionService.forApproval).not.toHaveBeenCalledWith(); + expect(this.requisitionService.search).toHaveBeenCalledWith(true, { + page: 0, + size: 10, + program: this.programs[0].id, + offline: 'true', + requisitionStatus: [ + this.REQUISITION_STATUS.AUTHORIZED, + this.REQUISITION_STATUS.IN_APPROVAL + ], + showBatchRequisitions: true + }); + }); + + it('should fetch requisitions when offline and program filter is specified', function() { + this.goToUrl('requisitions/approvalList?program=' + this.programs[0].id); + + expect(this.getResolvedValue('requisitions')).toEqual(this.requisitions); + expect(this.requisitionService.search).not.toHaveBeenCalled(); + expect(this.requisitionService.forApproval).toHaveBeenCalledWith({ + page: 0, + size: 10, + program: this.programs[0].id, + offline: undefined + }); + }); + + }); + + function goToUrl(url) { + this.$location.url(url); + this.$rootScope.$apply(); + } + + function getResolvedValue(name) { + return this.$state.$current.locals.globals[name]; + } + +});