Skip to content

Commit 5cccb62

Browse files
committed
Added everything for Step 15: Writing a Short Date Formatter Using TDD
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
1 parent f907811 commit 5cccb62

3 files changed

Lines changed: 112 additions & 1 deletion

File tree

webapp/model/DateFormatter.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
sap.ui.define(
2+
["sap/ui/base/Object", "sap/ui/core/format/DateFormat"],
3+
function (Object, DateFormat) {
4+
return Object.extend("com.mrb.UI5-Testing.model.DateFormatter", {
5+
constructor: function (oProperties) {
6+
this.timeFormat = DateFormat.getTimeInstance(
7+
{
8+
style: "short",
9+
},
10+
oProperties.locale
11+
);
12+
this.weekdayFormat = DateFormat.getDateInstance(
13+
{
14+
pattern: "EEEE",
15+
},
16+
oProperties.locale
17+
);
18+
this.dateFormat = DateFormat.getDateInstance(
19+
{
20+
style: "medium",
21+
},
22+
oProperties.locale
23+
);
24+
this.now = oProperties.now;
25+
},
26+
27+
format: function (oDate) {
28+
if (!oDate) {
29+
return "";
30+
}
31+
var iElapsedDays = this._getElapsedDays(oDate);
32+
if (iElapsedDays === 0) {
33+
return this.timeFormat.format(oDate);
34+
} else if (iElapsedDays === 1) {
35+
return "Yesterday";
36+
} else if (iElapsedDays < 7) {
37+
return this.weekdayFormat.format(oDate);
38+
} else {
39+
return this.dateFormat.format(oDate);
40+
}
41+
},
42+
43+
_getElapsedDays: function (oDate) {
44+
var iElapsedMilliseconds = this.now() - oDate.getTime();
45+
var fElapsedDays = iElapsedMilliseconds / 1000 / 60 / 60 / 24;
46+
return Math.floor(fElapsedDays);
47+
},
48+
});
49+
}
50+
);

webapp/test/unit/AllTests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
sap.ui.define(["./model/models", "./model/formatter", "./model/FlaggedType"], function () {
1+
sap.ui.define(["./model/models", "./model/formatter", "./model/FlaggedType", "./model/DateFormatter"], function () {
22
"use strict";
33
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
sap.ui.define(
2+
["com/mrb/UI5-Testing/model/DateFormatter", "sap/ui/core/Locale"],
3+
function (DateFormatter, Locale) {
4+
var oFormatter = null;
5+
QUnit.module("DateFormatter", {
6+
beforeEach: function () {
7+
oFormatter = new DateFormatter({
8+
now: function () {
9+
return new Date(2015, 2, 14, 14, 0, 0, 0).getTime();
10+
},
11+
locale: new Locale("en-US"),
12+
});
13+
},
14+
});
15+
16+
// QUnit.test("initial", function (assert) {
17+
// assert.ok(new DateFormatter());
18+
// });
19+
20+
QUnit.test("Should return empty string if no date is given", function (
21+
assert
22+
) {
23+
// var oFormatter = new DateFormatter();
24+
var sFormattedDate = oFormatter.format(null);
25+
assert.strictEqual(sFormattedDate, "");
26+
});
27+
28+
QUnit.test("Should return time if date from today", function (assert) {
29+
// var oFormatter = new DateFormatter({
30+
// locale: new Locale("en-US"),
31+
// });
32+
var oDate = new Date(2015, 2, 14, 12, 5, 0, 0);
33+
var sFormattedDate = oFormatter.format(oDate);
34+
assert.strictEqual(sFormattedDate, "12:05 PM");
35+
});
36+
37+
QUnit.test("Should return 'Yesterday' if date from yesterday", function (
38+
assert
39+
) {
40+
var oDate = new Date(2015, 2, 13);
41+
var sFormattedDate = oFormatter.format(oDate);
42+
assert.strictEqual(sFormattedDate, "Yesterday");
43+
});
44+
45+
QUnit.test("Should return day of the week if date < 7 days ago", function (
46+
assert
47+
) {
48+
var oDate = new Date(2015, 2, 8);
49+
var sFormattedDate = oFormatter.format(oDate);
50+
assert.strictEqual(sFormattedDate, "Sunday");
51+
});
52+
53+
QUnit.test("Should return date w/o time if date > 7 days ago", function (
54+
assert
55+
) {
56+
var oDate = new Date(2015, 2, 7);
57+
var sFormattedDate = oFormatter.format(oDate);
58+
assert.strictEqual(sFormattedDate, "Mar 7, 2015");
59+
});
60+
}
61+
);

0 commit comments

Comments
 (0)