Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BAH 3049 | Samridhi | Fix start date undefined value #716

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ui/app/reports/controllers/reportsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ angular.module('bahmni.reports')
report['stopDate'] = 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 = dateRange[0];
Expand Down Expand Up @@ -76,6 +77,9 @@ angular.module('bahmni.reports')
if (!report.stopDate) {
msg.push("end date");
}
if ((report.startDate > report.stopDate)) {
msg.push("start date can not be greater than stop date");
parvathy00 marked this conversation as resolved.
Show resolved Hide resolved
}
messagingService.showMessage("error", "Please select the " + msg.join(" and "));
return false;
}
Expand Down
8 changes: 3 additions & 5 deletions ui/app/reports/views/reports.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ <h2 class="section-title">{{::'REPORTS_TITLE_KEY' | translate}}</h2>
<th class="reports-stop-date">{{::'REPORTS_END_DATE_HEADER_KEY' |translate}}
<span class="asterick">*</span>
<input ng-model="default.reportsRequiringDateRange.stopDate" date-converter
class="form-field start-date" type="date" min="{{default.reportsRequiringDateRange.startDate | date:'yyyy-MM-dd'}}"
class="form-field stop-date" type="date" min="{{default.reportsRequiringDateRange.startDate | date:'yyyy-MM-dd'}}"
ng-change="setDefault('stopDate', 'reportsRequiringDateRange')">
</th>
<th class="reports-format">
Expand All @@ -45,12 +45,10 @@ <h2 class="section-title">{{::'REPORTS_TITLE_KEY' | translate}}</h2>
<tr ng-repeat="report in ::reportsRequiringDateRange" show-if-privilege="{{:: report.requiredPrivilege}}">
<td>{{:: report.name |translate }}</td>
<td class="reports-start-date">
<input date-converter class="form-field start-date" type="date" max="{{report.stopDate + 1 | date:'yyyy-MM-dd'}}"
ng-model="report.startDate">
<input date-converter class="form-field start-date" type="date" ng-model="report.startDate">
</td>
<td class="reports-stop-date">
<input date-converter class="form-field stop-date" type="date" min="{{report.startDate | date:'yyyy-MM-dd'}}"
ng-model="report.stopDate">
<input date-converter class="form-field stop-date" type="date" min="{{report.startDate | date:'yyyy-MM-dd'}}" ng-model="report.stopDate">
</td>
<td class="reports-format">
<select ng-model="report.responseType" ng-options="type as label for (label , type) in ::formats">
Expand Down
76 changes: 76 additions & 0 deletions ui/test/unit/reports/controllers/reportsController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,82 @@ describe("ReportsController", function () {
expect(rootScope.reportsRequiringDateRange[0].stopDate).toBe(rootScope.default.reportsRequiringDateRange.stopDate);
});

it("should return date and month for the this month", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(currentDate.getFullYear(), currentDate.getMonth(), 1)
};

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(currentDate.getMonth());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(currentDate.getDate());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getMonth()).toBe(currentDate.getMonth());
});

it("should return date and month for the this quarter", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(currentDate.getFullYear(), Math.floor(currentDate.getMonth() / 3) * 3, 1)
};

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(currentDate.getMonth());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(currentDate.getDate());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getMonth()).toBe(currentDate.getMonth());
});

it("should return date and month for the this year", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(currentDate.getFullYear(), 0, 1)
};

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(0);
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(currentDate.getDate());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getMonth()).toBe(currentDate.getMonth());
});

it("should return date and month for last 7 days", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(new Date().setDate(currentDate.getDate() - 6))
};

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

expect(_.keys(rootScope.default.reportsRequiringDateRange).length).toBe(3);
expect(rootScope.reportsRequiringDateRange[0].startDate.getDate()).toBe(rootScope.default.reportsRequiringDateRange.startDate.getDate());
expect(rootScope.reportsRequiringDateRange[0].startDate.getMonth()).toBe(currentDate.getMonth());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(currentDate.getDate());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getMonth()).toBe(currentDate.getMonth());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the currentDate is 2-Oct-2023 (Mon), then for Menu option Last 7 days, the dates selected automatically should be: 26-Sep-2023 (Tue) to 2-Oct-2023 (Mon). In this case the startDate month = Sep (and not Oct). But I think your test is checking startDate.getMonth() to be currentDate.getMonth(). That isn't right I think. This test will break on certain days I think. Please reconfirm.

It would have been best if somehow tests could be written independent of actual date (today), and instead could be passed something like: If Current Date is X, then option Last 7 Days should show DateA and DateB, and so on. I am guessing that will require a little refactoring if your code, to take currentDate as a parameter, and then do calculations.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe you can otherwise try and use Jasmine clock to mock the current Date. See this ChatGPT discussion I just did regarding this code block (scroll to end for Jasmine clock): https://chat.openai.com/share/42d88843-c43c-4dfc-81bc-6ef80438efbf

});

it("should return date and month for last 30 days", function () {
var currentDate = new Date();
rootScope.default.reportsRequiringDateRange = {
dateRangeType: new Date(new Date().setDate(currentDate.getDate() - 30))
};

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

expect(_.keys(rootScope.default.reportsRequiringDateRange).length).toBe(3);
expect(rootScope.reportsRequiringDateRange[0].startDate.getDate()).toBe(rootScope.default.reportsRequiringDateRange.startDate.getDate());
expect(rootScope.reportsRequiringDateRange[0].stopDate.getDate()).toBe(currentDate.getDate());
});

it('should initialise all available formats when supportedFormats config is not specified', function () {
mockAppDescriptor.getConfigValue.and.returnValue(undefined);

Expand Down
Loading