Skip to content

Commit

Permalink
BAH-3175| Deepti,Sweety | Add test cases for previous month reports
Browse files Browse the repository at this point in the history
  • Loading branch information
deeptirawat1510 committed Nov 28, 2023
1 parent 656362d commit 85ae1d6
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 16 deletions.
4 changes: 2 additions & 2 deletions ui/app/reports/controllers/reportsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ angular.module('bahmni.reports')
};

var isPreviousMonth = function (date) {
return (new Date(date).getMonth() === new Date().getMonth() - 1 && new Date(date).getFullYear() === new Date().getFullYear())
|| (new Date(date).getMonth() === 11 && new Date(date).getFullYear() === new Date().getFullYear() - 1);
return (date.getMonth() === new Date().getMonth() - 1 && date.getFullYear() === new Date().getFullYear())
|| (date.getMonth() === 11 && date.getFullYear() === new Date().getFullYear() - 1);
};

var getPreviousMonthEndDate = function () {
Expand Down
48 changes: 34 additions & 14 deletions ui/test/unit/reports/controllers/reportsController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,15 @@ describe("ReportsController", function () {
}
}
};

var originalDate;

beforeEach(module('bahmni.reports'));

beforeEach(inject(function ($controller, $rootScope) {
scope = $rootScope.$new();
rootScope = $rootScope;
originalDate = window.Date;

messagingServiceMock = jasmine.createSpyObj('messagingService', ['showMessage']);
spinnerMock = jasmine.createSpyObj('spinner', ['forPromise']);
Expand Down Expand Up @@ -70,6 +74,10 @@ describe("ReportsController", function () {
});
}));

afterEach(function () {
window.Date = originalDate;
});

it("initializes report sets based on whether date range required or not", function () {
expect(mockAppDescriptor.getConfigForPage).toHaveBeenCalledWith("reports");
expect(rootScope.reportsRequiringDateRange.length).toBe(2);
Expand Down Expand Up @@ -104,20 +112,32 @@ describe("ReportsController", function () {
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(new Date().getDate());
});

it("should return date and month for the previous month", function () {
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date('1-Dec-2022')
};

scope.setDefault('dateRangeType', 'reportsRequiringDateRange');

expect(_.keys(rootScope.default.reportsRequiringDateRange).length).toBe(3);
expect((rootScope.default.reportsRequiringDateRange.dateRangeType).getDate()).toBe(1);
expect(rootScope.reportsRequiringDateRange[0].startDate.getDate()).toBe(1);
expect(rootScope.reportsRequiringDateRange[0].startDate.getMonth()).toBe(11);
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(31);
expect(rootScope.reportsRequiringDateRange[0].stopDate.getMonth()).toBe(11);

const previousMonthTestCases = [
{ currentDate: '1-Dec-2022', expectedStartDate: '1-Nov-2022', expectedStopDate: '30-Nov-2022' },
{ currentDate: '3-Jan-2023', expectedStartDate: '1-Dec-2022', expectedStopDate: '31-Dec-2022' },
{ currentDate: '5-Mar-2023', expectedStartDate: '1-Feb-2023', expectedStopDate: '28-Feb-2023' },
{ currentDate: '5-Mar-2024', expectedStartDate: '1-Feb-2024', expectedStopDate: '29-Feb-2024' }
];

previousMonthTestCases.forEach(({ currentDate, expectedStartDate, expectedStopDate }) => {
it(`should return previous date and month when ${currentDate}`, function () {
var mockedDate = new Date(currentDate);
spyOn(window, 'Date').and.callFake(function (year, month, day) {
if (arguments.length === 3) {
return new originalDate(year, month, day);
} else {
return mockedDate;
}
});

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());
});
});

it('should initialise all available formats when supportedFormats config is not specified', function () {
Expand Down

0 comments on commit 85ae1d6

Please sign in to comment.