-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}, | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
} | ||
); |