From 45d5aacdb0b72d309636fda07cb4b6ed0c90addb Mon Sep 17 00:00:00 2001 From: deeptirawat1510 Date: Thu, 30 Nov 2023 19:44:13 +0530 Subject: [PATCH] BAH-3049 | Deepti, Sweety | Add. Test cases for selected date range functionality for reports --- .../reports/controllers/reportsController.js | 12 +- ui/app/reports/services/reportService.js | 2 +- .../controllers/reportsController.spec.js | 151 +++++++++++++++--- 3 files changed, 138 insertions(+), 27 deletions(-) diff --git a/ui/app/reports/controllers/reportsController.js b/ui/app/reports/controllers/reportsController.js index b215f6473a..5b643a402b 100644 --- a/ui/app/reports/controllers/reportsController.js +++ b/ui/app/reports/controllers/reportsController.js @@ -34,18 +34,19 @@ angular.module('bahmni.reports') $scope.enableReportQueue = appService.getAppDescriptor().getConfigValue("enableReportQueue"); $scope.setDefault = function (item, header) { var setToChange = header === 'reportsRequiringDateRange' ? $rootScope.reportsRequiringDateRange : $rootScope.reportsNotRequiringDateRange; + var isPreviousMonth = $rootScope.default[header][item].getTime() === dateRange[2].getTime(); setToChange.forEach(function (report) { if (item == 'dateRangeType') { $rootScope.default.reportsRequiringDateRange.startDate = $rootScope.default[header][item]; - $rootScope.default.reportsRequiringDateRange.stopDate = isPreviousMonth($rootScope.default[header][item]) ? getPreviousMonthEndDate() : dateRange[0]; + $rootScope.default.reportsRequiringDateRange.stopDate = isPreviousMonth ? getPreviousMonthEndDate() : dateRange[0]; report['startDate'] = $rootScope.default[header][item]; - report['stopDate'] = isPreviousMonth($rootScope.default[header][item]) ? getPreviousMonthEndDate() : dateRange[0]; + report['stopDate'] = isPreviousMonth ? getPreviousMonthEndDate() : dateRange[0]; } else if ($rootScope.default[header][item] === undefined) { $rootScope.default.reportsRequiringDateRange.startDate = dateRange[0]; $rootScope.reportsRequiringDateRange.forEach(function (report) { report.startDate = dateRange[0]; - report.stopDate = isPreviousMonth(dateRange[0]) ? getPreviousMonthEndDate() : dateRange[0]; + report.stopDate = isPreviousMonth ? getPreviousMonthEndDate() : dateRange[0]; report.responseType = format[1]; }); } @@ -55,11 +56,6 @@ angular.module('bahmni.reports') }); }; - var isPreviousMonth = function (date) { - return (date.getMonth() === new Date().getMonth() - 1 && date.getFullYear() === new Date().getFullYear()) - || (date.getMonth() === 11 && date.getFullYear() === new Date().getFullYear() - 1); - }; - var getPreviousMonthEndDate = function () { return new Date(new Date().getFullYear(), new Date().getMonth(), 0); }; diff --git a/ui/app/reports/services/reportService.js b/ui/app/reports/services/reportService.js index a73290b554..2da037741d 100644 --- a/ui/app/reports/services/reportService.js +++ b/ui/app/reports/services/reportService.js @@ -19,7 +19,7 @@ angular.module('bahmni.reports') "Previous Month": new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 1), "This Quarter": new Date(currentDate.getFullYear(), Math.floor(currentDate.getMonth() / 3) * 3, 1), "This Year": new Date(currentDate.getFullYear(), 0, 1), - "Last 7 days": new Date(new Date().setDate(currentDate.getDate() - 6)), + "Last 7 days": new Date(new Date().setDate(currentDate.getDate() - 7)), "Last 30 days": new Date(new Date().setDate(currentDate.getDate() - 30)) }; diff --git a/ui/test/unit/reports/controllers/reportsController.spec.js b/ui/test/unit/reports/controllers/reportsController.spec.js index 3226a26b0f..3873f75f48 100644 --- a/ui/test/unit/reports/controllers/reportsController.spec.js +++ b/ui/test/unit/reports/controllers/reportsController.spec.js @@ -59,9 +59,18 @@ describe("ReportsController", function () { }); reportServiceMock.getAvailableDateRange.and.returnValue({ "Today": new Date(), - "This Month": new Date(new Date().getFullYear(), new Date().getMonth(), 1) + "This Month": new Date(new Date().getFullYear(), new Date().getMonth(), 1), + "Previous Month": new Date(new Date().getFullYear(), new Date().getMonth() - 1, 1) }); controller = $controller; + setupController(); + })); + + afterEach(function () { + window.Date = originalDate; + }); + + function setupController() { controller('ReportsController', { $scope: scope, appService: appServiceMock, @@ -72,11 +81,7 @@ describe("ReportsController", function () { $rootScope: rootScope, FileUploader: function () { } }); - })); - - afterEach(function () { - window.Date = originalDate; - }); + } it("initializes report sets based on whether date range required or not", function () { expect(mockAppDescriptor.getConfigForPage).toHaveBeenCalledWith("reports"); @@ -86,14 +91,7 @@ describe("ReportsController", function () { it('should initialise formats based on the supportedFormats config', function () { mockAppDescriptor.getConfigValue.and.returnValue(['csv', 'html']); - controller('ReportsController', { - $scope: scope, - appService: appServiceMock, - reportService: reportServiceMock, - messagingService: messagingServiceMock, - $rootScope: rootScope, - FileUploader: function () { } - }); + setupController(); expect(_.keys(scope.formats).length).toBe(2); expect(scope.formats['CSV']).toBe('text/csv'); @@ -111,6 +109,117 @@ describe("ReportsController", function () { expect(rootScope.reportsRequiringDateRange[0].startDate.getDate()).toBe(new Date().getDate()); expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(new Date().getDate()); }); + + it("should return the correct start and stop date when selected date range is last 30 days", function () { + var mockedDate = new Date('20-Dec-2023'); + var expectedStartDate = new originalDate('20-Nov-2023'); + spyOn(window, 'Date').and.callFake(function () { + return mockedDate; + }); + reportServiceMock.getAvailableDateRange.and.returnValue({ + "Today": mockedDate, + "This Month": new originalDate('1-Dec-2023'), + "Previous Month": new originalDate('1-Nov-2023') + }); + setupController(); + + rootScope.default.reportsRequiringDateRange = { + dateRangeType: expectedStartDate, + }; + scope.setDefault('dateRangeType', 'reportsRequiringDateRange'); + + expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate); + expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate); + }); + + it("should return the correct start and stop date when selected date range is last 7 days", function () { + var mockedDate = new Date('20-Dec-2023'); + var expectedStartDate = new originalDate('13-Dec-2023'); + spyOn(window, 'Date').and.callFake(function () { + return mockedDate; + }); + reportServiceMock.getAvailableDateRange.and.returnValue({ + "Today": mockedDate, + "This Month": new originalDate('1-Dec-2023'), + "Previous Month": new originalDate('1-Nov-2023') + }); + setupController(); + + rootScope.default.reportsRequiringDateRange = { + dateRangeType: expectedStartDate, + }; + scope.setDefault('dateRangeType', 'reportsRequiringDateRange'); + + expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate); + expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate); + }); + + it("should return the correct start and stop date when selected date range is this year", function () { + var mockedDate = new Date('20-Dec-2023'); + var expectedStartDate = new originalDate('1-Jan-2023'); + spyOn(window, 'Date').and.callFake(function () { + return mockedDate; + }); + reportServiceMock.getAvailableDateRange.and.returnValue({ + "Today": mockedDate, + "This Month": new originalDate('1-Dec-2023'), + "Previous Month": new originalDate('1-Nov-2023') + }); + setupController(); + + rootScope.default.reportsRequiringDateRange = { + dateRangeType: expectedStartDate, + }; + scope.setDefault('dateRangeType', 'reportsRequiringDateRange'); + + expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate); + expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate); + }); + + it("should return the correct start and stop date when selected date range is this quarter", function () { + var mockedDate = new Date('20-Dec-2023'); + var expectedStartDate = new originalDate('1-Oct-2023'); + spyOn(window, 'Date').and.callFake(function () { + return mockedDate; + }); + reportServiceMock.getAvailableDateRange.and.returnValue({ + "Today": mockedDate, + "This Month": new originalDate('1-Dec-2023'), + "Previous Month": new originalDate('1-Nov-2023') + }); + setupController(); + + rootScope.default.reportsRequiringDateRange = { + dateRangeType: expectedStartDate, + }; + scope.setDefault('dateRangeType', 'reportsRequiringDateRange'); + + expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate); + expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate); + }); + + it("should return the correct start and stop date when selected date range is this month", function () { + var mockedDate = new Date('20-Dec-2023'); + var expectedStartDate = new originalDate('1-Dec-2023'); + spyOn(window, 'Date').and.callFake(function () { + return mockedDate; + }); + reportServiceMock.getAvailableDateRange.and.returnValue({ + "Today": mockedDate, + "This Month": new originalDate('1-Dec-2023'), + "Previous Month": new originalDate('1-Nov-2023') + }); + setupController(); + + rootScope.default.reportsRequiringDateRange = { + dateRangeType: expectedStartDate, + }; + scope.setDefault('dateRangeType', 'reportsRequiringDateRange'); + + expect(rootScope.reportsRequiringDateRange[0].startDate).toEqual(expectedStartDate); + expect(rootScope.reportsRequiringDateRange[0].stopDate).toEqual(mockedDate); + }); + const previousMonthTestCases = [ { currentDate: '1-Dec-2022', expectedStartDate: '1-Nov-2022', expectedStopDate: '30-Nov-2022' }, @@ -129,14 +238,20 @@ describe("ReportsController", function () { return mockedDate; } }); - + reportServiceMock.getAvailableDateRange.and.returnValue({ + "Today": mockedDate, + "This Month": new originalDate('1-Jan-2023'), + "Previous Month": new originalDate(expectedStartDate) + }); + setupController(); + rootScope.default.reportsRequiringDateRange = { dateRangeType: new originalDate(expectedStartDate), }; scope.setDefault('dateRangeType', 'reportsRequiringDateRange'); - - expect(rootScope.reportsRequiringDateRange[0].startDate.getTime()).toBe(new originalDate(expectedStartDate).getTime()); - expect(rootScope.reportsRequiringDateRange[0].stopDate.getTime()).toBe(new originalDate(expectedStopDate).getTime()); + + expect(rootScope.reportsRequiringDateRange[0].startDate.getTime()).toBe(new originalDate(expectedStartDate).getTime()); + expect(rootScope.reportsRequiringDateRange[0].stopDate.getTime()).toBe(new originalDate(expectedStopDate).getTime()); }); });