Moved Formatter, KeyValueStore and Onebox jasmine tests to QUnit.

This commit is contained in:
Robin Ward 2013-06-18 16:27:40 -04:00
parent 8af9952b25
commit f0ebca51cc
9 changed files with 4545 additions and 178 deletions

View file

@ -1,128 +0,0 @@
/*global expect:true describe:true it:true beforeEach:true afterEach:true spyOn:true */
describe("Discourse.Formatter", function() {
var format = "tiny";
var leaveAgo = false;
var mins_ago = function(mins){
return new Date((new Date()) - mins * 60 * 1000);
};
var formatMins = function(mins) {
return Discourse.Formatter.relativeAge(mins_ago(mins), {format: format, leaveAgo: leaveAgo});
};
var formatHours = function(hours) {
return formatMins(hours * 60);
};
var formatDays = function(days) {
return formatHours(days * 24);
};
var formatMonths = function(months) {
return formatDays(months * 30);
};
describe("relativeTime", function() {
it("can format medium length dates", function() {
format = "medium";
var strip = function(html){
return $(html).text();
}
var shortDate = function(days){
return moment().subtract('days', days).format('D MMM');
}
var shortDateYear = function(days){
return moment().subtract('days', days).format('D MMM, YYYY');
}
leaveAgo = true;
expect(strip(formatMins(1.4))).toBe("1 minute ago");
expect(strip(formatMins(2))).toBe("2 minutes ago");
expect(strip(formatMins(56))).toBe("56 minutes ago");
expect(strip(formatMins(57))).toBe("1 hour ago");
expect(strip(formatHours(4))).toBe("4 hours ago");
expect(strip(formatHours(22))).toBe("22 hours ago");
expect(strip(formatHours(23))).toBe("1 day ago");
expect(strip(formatDays(4.85))).toBe("4 days ago");
leaveAgo = false;
expect(strip(formatMins(0))).toBe("just now");
expect(strip(formatMins(1.4))).toBe("1 minute");
expect(strip(formatMins(2))).toBe("2 minutes");
expect(strip(formatMins(56))).toBe("56 minutes");
expect(strip(formatMins(57))).toBe("1 hour");
expect(strip(formatHours(4))).toBe("4 hours");
expect(strip(formatHours(22))).toBe("22 hours");
expect(strip(formatHours(23))).toBe("1 day");
expect(strip(formatDays(4.85))).toBe("4 days");
expect(strip(formatDays(6))).toBe(shortDate(6));
expect(strip(formatDays(100))).toBe(shortDate(100)); // eg: 23 Jan
expect(strip(formatDays(500))).toBe(shortDateYear(500));
expect($(formatDays(0)).attr("title")).toBe(moment().format('MMMM D, YYYY h:mma'));
expect($(formatDays(0)).attr("class")).toBe("date");
});
it("can format dates", function() {
format = "tiny";
expect(formatMins(0)).toBe("< 1m");
expect(formatMins(2)).toBe("2m");
expect(formatMins(60)).toBe("1h");
expect(formatHours(4)).toBe("4h");
expect(formatDays(1)).toBe("1d");
expect(formatDays(20)).toBe("20d");
expect(formatMonths(3)).toBe("3mon");
expect(formatMonths(23)).toBe("23mon");
expect(formatMonths(24)).toBe("> 2y");
});
});
describe("autoUpdatingRelativeAge", function(){
it("can format dates", function(){
var d = moment().subtract('days',1).toDate();
var $elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d));
expect($elem.data('format')).toBe("tiny");
expect($elem.data('time')).toBe(d.getTime());
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d,{format: 'medium', leaveAgo: true}));
expect($elem.data('format')).toBe("medium-with-ago");
expect($elem.data('time')).toBe(d.getTime());
expect($elem.attr('title')).toBe(moment(d).longDate());
expect($elem.html()).toBe('1 day ago');
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d,{format: 'medium'}));
expect($elem.data('format')).toBe("medium");
expect($elem.data('time')).toBe(d.getTime());
expect($elem.html()).toBe('1 day');
});
});
describe("updateRelativeAge", function(){
it("can update relative dates", function(){
var d = new Date();
var $elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d));
$elem.data('time', d.getTime() - 2 * 60 * 1000);
Discourse.Formatter.updateRelativeAge($elem);
expect($elem.html()).toBe("2m");
d = new Date();
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d, {format: 'medium', leaveAgo: true}));
$elem.data('time', d.getTime() - 2 * 60 * 1000);
Discourse.Formatter.updateRelativeAge($elem);
expect($elem.html()).toBe("2 minutes ago");
});
});
});

View file

@ -1,24 +0,0 @@
/*global waitsFor:true expect:true describe:true beforeEach:true it:true */
describe("Discourse.KeyValueStore", function() {
describe("Setting values", function() {
var store = Discourse.KeyValueStore;
store.init("test");
it("is able to get the value back from the store", function() {
store.set({ key: "bob", value: "uncle" });
expect(store.get("bob")).toBe("uncle");
});
it("is able to nuke the store", function() {
store.set({ key: "bob1", value: "uncle" });
store.abandonLocal();
localStorage.a = 1;
expect(store.get("bob1")).toBe(void 0);
expect(localStorage.a).toBe("1");
});
});
});

View file

@ -1,24 +0,0 @@
/*global waitsFor:true expect:true describe:true beforeEach:true it:true spyOn:true */
describe("Discourse.Onebox", function() {
var anchor;
beforeEach(function() {
spyOn(Discourse, 'ajax').andCallThrough();
anchor = $("<a href='http://bla.com'></a>")[0];
});
it("Stops rapid calls with cache true", function() {
Discourse.Onebox.load(anchor, true);
Discourse.Onebox.load(anchor, true);
expect(Discourse.ajax.calls.length).toBe(1);
});
it("Stops rapid calls with cache false", function() {
Discourse.Onebox.load(anchor, false);
Discourse.Onebox.load(anchor, false);
expect(Discourse.ajax.calls.length).toBe(1);
});
});

View file

@ -0,0 +1,121 @@
/*global module:true test:true ok:true visit:true equal:true exists:true count:true equal:true present:true md5:true */
module("Discourse.Formatter");
var format = "tiny";
var leaveAgo = false;
var mins_ago = function(mins){
return new Date((new Date()) - mins * 60 * 1000);
};
var formatMins = function(mins) {
return Discourse.Formatter.relativeAge(mins_ago(mins), {format: format, leaveAgo: leaveAgo});
};
var formatHours = function(hours) {
return formatMins(hours * 60);
};
var formatDays = function(days) {
return formatHours(days * 24);
};
var formatMonths = function(months) {
return formatDays(months * 30);
};
test("formating medium length dates", function() {
format = "medium";
var strip = function(html){
return $(html).text();
}
var shortDate = function(days){
return moment().subtract('days', days).format('D MMM');
}
var shortDateYear = function(days){
return moment().subtract('days', days).format('D MMM, YYYY');
}
leaveAgo = true;
equal(strip(formatMins(1.4)), "1 minute ago");
equal(strip(formatMins(2)), "2 minutes ago");
equal(strip(formatMins(56)), "56 minutes ago");
equal(strip(formatMins(57)), "1 hour ago");
equal(strip(formatHours(4)), "4 hours ago");
equal(strip(formatHours(22)), "22 hours ago");
equal(strip(formatHours(23)), "1 day ago");
equal(strip(formatDays(4.85)), "4 days ago");
leaveAgo = false;
equal(strip(formatMins(0)), "just now");
equal(strip(formatMins(1.4)), "1 minute");
equal(strip(formatMins(2)), "2 minutes");
equal(strip(formatMins(56)), "56 minutes");
equal(strip(formatMins(57)), "1 hour");
equal(strip(formatHours(4)), "4 hours");
equal(strip(formatHours(22)), "22 hours");
equal(strip(formatHours(23)), "1 day");
equal(strip(formatDays(4.85)), "4 days");
equal(strip(formatDays(6)), shortDate(6));
equal(strip(formatDays(100)), shortDate(100)); // eg: 23 Jan
equal(strip(formatDays(500)), shortDateYear(500));
equal($(formatDays(0)).attr("title"), moment().format('MMMM D, YYYY h:mma'));
equal($(formatDays(0)).attr("class"), "date");
});
test("formating tiny dates", function() {
format = "tiny";
equal(formatMins(0), "< 1m");
equal(formatMins(2), "2m");
equal(formatMins(60), "1h");
equal(formatHours(4), "4h");
equal(formatDays(1), "1d");
equal(formatDays(20), "20d");
equal(formatMonths(3), "3mon");
equal(formatMonths(23), "23mon");
equal(formatMonths(24), "> 2y");
});
test("autoUpdatingRelativeAge", function() {
var d = moment().subtract('days',1).toDate();
var $elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d));
equal($elem.data('format'), "tiny");
equal($elem.data('time'), d.getTime());
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d,{format: 'medium', leaveAgo: true}));
equal($elem.data('format'), "medium-with-ago");
equal($elem.data('time'), d.getTime());
equal($elem.attr('title'), moment(d).longDate());
equal($elem.html(), '1 day ago');
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d,{format: 'medium'}));
equal($elem.data('format'), "medium");
equal($elem.data('time'), d.getTime());
equal($elem.html(), '1 day');
});
test("updateRelativeAge", function(){
var d = new Date();
var $elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d));
$elem.data('time', d.getTime() - 2 * 60 * 1000);
Discourse.Formatter.updateRelativeAge($elem);
equal($elem.html(), "2m");
d = new Date();
$elem = $(Discourse.Formatter.autoUpdatingRelativeAge(d, {format: 'medium', leaveAgo: true}));
$elem.data('time', d.getTime() - 2 * 60 * 1000);
Discourse.Formatter.updateRelativeAge($elem);
equal($elem.html(), "2 minutes ago");
});

View file

@ -0,0 +1,22 @@
/*global module:true test:true ok:true visit:true equal:true exists:true count:true equal:true present:true md5:true */
var store = Discourse.KeyValueStore;
module("Discourse.KeyValueStore", {
setup: function() {
store.init("test");
}
});
test("it's able to get the result back from the store", function() {
store.set({ key: "bob", value: "uncle" });
equal(store.get("bob"), "uncle");
});
test("is able to nuke the store", function() {
store.set({ key: "bob1", value: "uncle" });
store.abandonLocal();
localStorage.a = 1;
equal(store.get("bob1"), void 0);
equal(localStorage.a, "1");
});

View file

@ -0,0 +1,22 @@
/*global module:true test:true ok:true visit:true equal:true exists:true count:true equal:true present:true md5:true resolvingPromise:true */
module("Discourse.Onebox", {
setup: function() {
this.anchor = $("<a href='http://bla.com'></a>")[0];
}
});
test("Stops rapid calls with cache true", function() {
this.stub(Discourse, "ajax").returns(resolvingPromise);
Discourse.Onebox.load(this.anchor, true);
Discourse.Onebox.load(this.anchor, true);
ok(Discourse.ajax.calledOnce);
});
test("Stops rapid calls with cache false", function() {
this.stub(Discourse, "ajax").returns(resolvingPromise);
Discourse.Onebox.load(this.anchor, false);
Discourse.Onebox.load(this.anchor, false);
ok(Discourse.ajax.calledOnce);
});

View file

@ -1,5 +1,5 @@
/*jshint maxlen:250 */
/*global count:true find:true document:true equal:true */
/*global count:true find:true document:true equal:true sinon:true */
//= require env
@ -19,7 +19,7 @@
//= require ../../app/assets/javascripts/locales/date_locales.js
//= require ../../app/assets/javascripts/discourse/helpers/i18n_helpers
//= require ../../app/assets/javascripts/locales/en
//
// Pagedown customizations
//= require ../../app/assets/javascripts/pagedown_custom.js
@ -32,11 +32,24 @@
//= require_tree ../../app/assets/javascripts/defer
//= require main_include
//= require sinon-1.7.1.js
//= require sinon-qunit-1.0.0.js
//= require_tree .
//= require_self
//= require_tree ./fixtures
// sinon settings
sinon.config = {
injectIntoThis: true,
injectInto: null,
properties: ["spy", "stub", "mock", "clock", "sandbox"],
useFakeTimers: false,
useFakeServer: false
};
// Trick JSHint into allow document.write
var d = document;
d.write('<div id="ember-testing-container"><div id="ember-testing"></div></div>');
@ -51,6 +64,11 @@ Discourse.Router.map(function() {
return Discourse.routeBuilder.call(this);
});
// Test helpers
var resolvingPromise = Ember.Deferred.promise(function (p) {
p.resolve();
})
function exists(selector) {
return !!count(selector);
}

4299
vendor/assets/javascripts/sinon-1.7.1.js vendored Normal file

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,61 @@
/**
* sinon-qunit 1.0.0, 2010/12/09
*
* @author Christian Johansen (christian@cjohansen.no)
*
* (The BSD License)
*
* Copyright (c) 2010-2011, Christian Johansen, christian@cjohansen.no
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Christian Johansen nor the names of his contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*global sinon, QUnit, test*/
sinon.assert.fail = function (msg) {
QUnit.ok(false, msg);
};
sinon.assert.pass = function (assertion) {
QUnit.ok(true, assertion);
};
sinon.config = {
injectIntoThis: true,
injectInto: null,
properties: ["spy", "stub", "mock", "clock", "sandbox"],
useFakeTimers: true,
useFakeServer: false
};
(function (global) {
var qTest = QUnit.test;
QUnit.test = global.test = function (testName, expected, callback, async) {
if (arguments.length === 2) {
callback = expected;
expected = null;
}
return qTest(testName, expected, sinon.test(callback), async);
};
}(this));