Skip to content

Commit

Permalink
Added a test for allowFuture = false
Browse files Browse the repository at this point in the history
No change to existing functionality. Just testing existing behavior.
  • Loading branch information
Rich Hildebrand committed Feb 25, 2014
1 parent 8946a5e commit 873f0f7
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
24 changes: 24 additions & 0 deletions test/index.html
Expand Up @@ -190,6 +190,12 @@ <h2>Settings</h2>
<li><abbr id="testMillisSettings9" class="tomillis" title="120"></abbr> [120 sec]</li>
</ul>

<h2>Future</h2>

<ul>
<li><abbr id="testAllowFutureFalse1" class="doNotAllowFuture" title="2012-01-01T09:24:17Z">(you shouldn't see this)</abbr></li>
</ul>

<h2>Disposal</h2>
<p><abbr class="disposal disposed"></abbr></p>
<p><abbr class="disposal notDisposed"></abbr></p>
Expand Down Expand Up @@ -243,6 +249,10 @@ <h2>Disposal</h2>
$("abbr.tonumbers").each(toWords);
unloadNumbers();

loadDoNotAllowFuture();
$("abbr.doNotAllowFuture").timeago();
unloadDoNotAllowFuture();

setupDisposal();

loadYoungOldYears();
Expand Down Expand Up @@ -595,6 +605,20 @@ <h2>Disposal</h2>
ok($("#testNullSpaces1").html().match(/^2minutesago$/), "Settings correctly applied");
});

module("Do Not Allow Future");

// if allowFuture is false, then a minute into the future is altually reported as a minute in the past.
// we did not want to alter the current behavior and break backwards compatability
test("allow future false with a future date moves time backwards", function () {
equal($("#testAllowFutureFalse1").html(), "2 years ago");
});

module("Allow Future");

// test("allow future false with a future date moves time backwards", function () {
// ok($("#testAllowFutureFalse1").html().match(/years ago$/));
// });

module("Disposal");

asyncTest("disposal", function() {
Expand Down
32 changes: 32 additions & 0 deletions test/test_helpers.js
Expand Up @@ -131,3 +131,35 @@ function loadYoungOldYears() {
years: function(value) { return (value < 21) ? "%d young years" : "%d old years"; }
});
}

function loadDoNotAllowFuture() {
var mockDateToUse = "2010-01-01";
$.timeago.settings.allowFuture = false;
enableMockedDate(mockDateToUse);
}

function unloadDoNotAllowFuture() {
$.timeago.settings.allowFuture = true;
disableMockedDate();
}

function enableMockedDate(dateToReturn) {
var mockDate = dateToReturn;
window.NativeDate = Date;
window.Date = function () {
if(arguments.length === 0) {
return new window.NativeDate(mockDate);
} else if(arguments.length === 1) {
return new window.NativeDate(arguments[0]);
} else if(arguments.length === 7) {
return new window.NativeDate(arguments[0], arguments[1], arguments[2], arguments[3], arguments[4], arguments[5], arguments[6]);
} else {
throw "Mocking Date with this number of parameters is not implemented.";
}
}
}

function disableMockedDate() {
window.Date = window.NativeDate;
delete window.NativeDate;
}

0 comments on commit 873f0f7

Please sign in to comment.