Skip to content

Commit

Permalink
Added everything for Step 15: Writing a Short Date Formatter Using TDD
Browse files Browse the repository at this point in the history
Formatting Categories:

Sample Input

Expected Output (for en-US)

Today

2013/02/13 12:05:20

12:05 PM

---

Yesterday

2013/02/12 12:05:20

Yesterday

---

Last 7 days

2013/02/08 12:05:20

Friday

---

Others

2011/02/05 12:05:20

Dec 5, 2011
  • Loading branch information
wridgeu committed Jun 11, 2020
1 parent f907811 commit 5cccb62
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 1 deletion.
50 changes: 50 additions & 0 deletions webapp/model/DateFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
sap.ui.define(
["sap/ui/base/Object", "sap/ui/core/format/DateFormat"],
function (Object, DateFormat) {
return Object.extend("com.mrb.UI5-Testing.model.DateFormatter", {
constructor: function (oProperties) {
this.timeFormat = DateFormat.getTimeInstance(
{
style: "short",
},
oProperties.locale
);
this.weekdayFormat = DateFormat.getDateInstance(
{
pattern: "EEEE",
},
oProperties.locale
);
this.dateFormat = DateFormat.getDateInstance(
{
style: "medium",
},
oProperties.locale
);
this.now = oProperties.now;
},

format: function (oDate) {
if (!oDate) {
return "";
}
var iElapsedDays = this._getElapsedDays(oDate);
if (iElapsedDays === 0) {
return this.timeFormat.format(oDate);
} else if (iElapsedDays === 1) {
return "Yesterday";
} else if (iElapsedDays < 7) {
return this.weekdayFormat.format(oDate);
} else {
return this.dateFormat.format(oDate);
}
},

_getElapsedDays: function (oDate) {
var iElapsedMilliseconds = this.now() - oDate.getTime();
var fElapsedDays = iElapsedMilliseconds / 1000 / 60 / 60 / 24;
return Math.floor(fElapsedDays);
},
});
}
);
2 changes: 1 addition & 1 deletion webapp/test/unit/AllTests.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
sap.ui.define(["./model/models", "./model/formatter", "./model/FlaggedType"], function () {
sap.ui.define(["./model/models", "./model/formatter", "./model/FlaggedType", "./model/DateFormatter"], function () {
"use strict";
});
61 changes: 61 additions & 0 deletions webapp/test/unit/model/DateFormatter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
sap.ui.define(
["com/mrb/UI5-Testing/model/DateFormatter", "sap/ui/core/Locale"],
function (DateFormatter, Locale) {
var oFormatter = null;
QUnit.module("DateFormatter", {
beforeEach: function () {
oFormatter = new DateFormatter({
now: function () {
return new Date(2015, 2, 14, 14, 0, 0, 0).getTime();
},
locale: new Locale("en-US"),
});
},
});

// QUnit.test("initial", function (assert) {
// assert.ok(new DateFormatter());
// });

QUnit.test("Should return empty string if no date is given", function (
assert
) {
// var oFormatter = new DateFormatter();
var sFormattedDate = oFormatter.format(null);
assert.strictEqual(sFormattedDate, "");
});

QUnit.test("Should return time if date from today", function (assert) {
// var oFormatter = new DateFormatter({
// locale: new Locale("en-US"),
// });
var oDate = new Date(2015, 2, 14, 12, 5, 0, 0);
var sFormattedDate = oFormatter.format(oDate);
assert.strictEqual(sFormattedDate, "12:05 PM");
});

QUnit.test("Should return 'Yesterday' if date from yesterday", function (
assert
) {
var oDate = new Date(2015, 2, 13);
var sFormattedDate = oFormatter.format(oDate);
assert.strictEqual(sFormattedDate, "Yesterday");
});

QUnit.test("Should return day of the week if date < 7 days ago", function (
assert
) {
var oDate = new Date(2015, 2, 8);
var sFormattedDate = oFormatter.format(oDate);
assert.strictEqual(sFormattedDate, "Sunday");
});

QUnit.test("Should return date w/o time if date > 7 days ago", function (
assert
) {
var oDate = new Date(2015, 2, 7);
var sFormattedDate = oFormatter.format(oDate);
assert.strictEqual(sFormattedDate, "Mar 7, 2015");
});
}
);

0 comments on commit 5cccb62

Please sign in to comment.