Commit d8a2e586 authored by Sean Hammond's avatar Sean Hammond

Allow tests to pass in a mock Intl

Make Intl an optional argument in time.js so that tests can pass in a
mock Intl (or null) instead of having to patch window.Intl.
parent 3f23934d
...@@ -52,15 +52,8 @@ describe('time', function () { ...@@ -52,15 +52,8 @@ describe('time', function () {
describe('.toFuzzyString', function () { describe('.toFuzzyString', function () {
var originalIntl; function mockIntl() {
return {
before(function () {
// Intl isn't implemented in PhantomJS so we have to mock it in these
// tests. The mock version returns the results we'd expect from the real
// one for the test values that we use.
originalIntl = window.Intl;
window.Intl = {
DateTimeFormat: function () { DateTimeFormat: function () {
return { return {
format: function () { format: function () {
...@@ -73,16 +66,12 @@ describe('time', function () { ...@@ -73,16 +66,12 @@ describe('time', function () {
}; };
} }
}; };
}); }
after(function () {
window.Intl = originalIntl;
});
it('Handles empty dates', function () { it('Handles empty dates', function () {
var t = null; var t = null;
var expect = ''; var expect = '';
assert.equal(time.toFuzzyString(t), expect); assert.equal(time.toFuzzyString(t, mockIntl()), expect);
}); });
var testFixture = function (f) { var testFixture = function (f) {
...@@ -90,7 +79,7 @@ describe('time', function () { ...@@ -90,7 +79,7 @@ describe('time', function () {
var t = new Date(); var t = new Date();
var expect = f[1]; var expect = f[1];
sandbox.clock.tick(f[0] * 1000); sandbox.clock.tick(f[0] * 1000);
assert.equal(time.toFuzzyString(t), expect); assert.equal(time.toFuzzyString(t, mockIntl()), expect);
}; };
}; };
...@@ -103,21 +92,19 @@ describe('time', function () { ...@@ -103,21 +92,19 @@ describe('time', function () {
it('falls back to simple strings for >24hrs ago', function () { it('falls back to simple strings for >24hrs ago', function () {
// If window.Intl is not available then the date formatting for dates // If window.Intl is not available then the date formatting for dates
// more than one day ago falls back to a simple date string. // more than one day ago falls back to a simple date string.
window.Intl = undefined;
var d = new Date(); var d = new Date();
sandbox.clock.tick(day * 2 * 1000); sandbox.clock.tick(day * 2 * 1000);
assert.equal(time.toFuzzyString(d), 'Thu Jan 01 1970'); assert.equal(time.toFuzzyString(d, null), 'Thu Jan 01 1970');
}); });
it('falls back to simple strings for >1yr ago', function () { it('falls back to simple strings for >1yr ago', function () {
// If window.Intl is not available then the date formatting for dates // If window.Intl is not available then the date formatting for dates
// more than one year ago falls back to a simple date string. // more than one year ago falls back to a simple date string.
window.Intl = undefined;
var d = new Date(); var d = new Date();
sandbox.clock.tick(year * 2 * 1000); sandbox.clock.tick(year * 2 * 1000);
assert.equal(time.toFuzzyString(d), 'Thu Jan 01 1970'); assert.equal(time.toFuzzyString(d, null), 'Thu Jan 01 1970');
}); });
}); });
......
...@@ -54,8 +54,14 @@ var formatters = {}; ...@@ -54,8 +54,14 @@ var formatters = {};
* @returns {string} * @returns {string}
* *
*/ */
function format(date, options) { function format(date, options, Intl) {
if (typeof Intl !== 'undefined' && Intl.DateTimeFormat) { // If the tests have passed in a mock Intl then use it, otherwise use the
// real one.
if (typeof Intl === 'undefined') {
Intl = window.Intl;
}
if (Intl && Intl.DateTimeFormat) {
var key = JSON.stringify(options); var key = JSON.stringify(options);
var formatter = formatters[key]; var formatter = formatters[key];
...@@ -71,19 +77,12 @@ function format(date, options) { ...@@ -71,19 +77,12 @@ function format(date, options) {
} }
} }
function dayAndMonth(date) { function dayAndMonth(date, now, Intl) {
return format(date, { return format(date, {month: 'short', day: '2-digit'}, Intl);
month: 'short',
day: '2-digit',
});
} }
function dayAndMonthAndYear(date) { function dayAndMonthAndYear(date, now, Intl) {
return format(date, { return format(date, {day: '2-digit', month: 'short', year: 'numeric'}, Intl);
day: '2-digit',
month: 'short',
year: 'numeric'
});
} }
var BREAKPOINTS = [ var BREAKPOINTS = [
...@@ -186,13 +185,13 @@ function decayingInterval(date, callback) { ...@@ -186,13 +185,13 @@ function decayingInterval(date, callback) {
* @param {number} date - The absolute timestamp to format. * @param {number} date - The absolute timestamp to format.
* @return {string} A 'fuzzy' string describing the relative age of the date. * @return {string} A 'fuzzy' string describing the relative age of the date.
*/ */
function toFuzzyString(date) { function toFuzzyString(date, Intl) {
if (!date) { if (!date) {
return ''; return '';
} }
var now = new Date(); var now = new Date();
return getBreakpoint(date, now).format(new Date(date), now); return getBreakpoint(date, now).format(new Date(date), now, Intl);
} }
module.exports = { module.exports = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment