3
http://github.com/wyuenho/backgrid
5
Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
6
Licensed under the MIT @license.
8
describe("A MomentFormatter", function () {
10
it(".fromRaw() can convert an UNIX offset to a datetime string in moment.js", function () {
11
var formatter = new Backgrid.Extension.MomentFormatter({
12
modelInUnixOffset: true
14
expect(formatter.fromRaw(1318781876406)).toBe('2011-10-16T16:17:56+00:00');
17
it(".fromRaw() can convert an UNIX timestamp to a datetime string in moment.js", function () {
18
var formatter = new Backgrid.Extension.MomentFormatter({
19
modelInUnixTimestamp: true
21
expect(formatter.fromRaw(1318781876.721)).toBe('2011-10-16T16:17:56+00:00');
24
it(".fromRaw() can convert an ISO datetime string in UTC to a datetime " +
25
"string in moment.js' default format in UTC", function () {
26
var formatter = new Backgrid.Extension.MomentFormatter();
27
expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe("2012-02-29T05:30:00+00:00");
30
it(".fromRaw() can convert an ISO datetime string in UTC to a datetime " +
31
"string in moment.js' default format in local time", function () {
32
var formatter = new Backgrid.Extension.MomentFormatter({
35
expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe(moment.utc("2012-02-29T05:30:00.100Z").local().format());
38
it(".fromRaw() can convert an ISO datetime string in UTC to a datetime " +
39
"string in another moment.js supported format in the default locale", function () {
40
var formatter = new Backgrid.Extension.MomentFormatter({
41
displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss G\\MT" //cookie expires format
43
expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe("Wed, 29-Feb-2012 05:30:00 GMT");
46
it(".fromRaw() can convert an ISO datetime string in UTC to a datetime " +
47
"string in another moment.js supported format in a different locale", function () {
48
var formatter = new Backgrid.Extension.MomentFormatter({
49
displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss G\\MT",
52
expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe("週三, 29-2月-2012 05:30:00 GMT");
55
it(".fromRaw() can convert an ISO datetime string in UTC to a datetime " +
56
"string in another moment.js supported format in a different locale in " +
57
"local time", function () {
58
var formatter = new Backgrid.Extension.MomentFormatter({
59
displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss ZZ",
63
expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe(moment.utc("2012-02-29T05:30:00.100Z").local().lang("zh-tw").format("ddd, DD-MMM-YYYY HH:mm:ss ZZ"));
66
it(".fromRaw() can convert a datetime string in UTC in a moment.js " +
67
"supported format to another datetime string in UTC in another moment.js" +
68
" supported format", function () {
69
var formatter = new Backgrid.Extension.MomentFormatter({
70
modelFormat: "ddd, DD-MMM-YYYY HH:mm:ss",
71
displayFormat: "MM-DD-YYYY HH:mm:ss"
73
expect(formatter.fromRaw("Wed, 29-Feb-2012 05:30:00")).toBe("02-29-2012 05:30:00");
76
it(".fromRaw() can convert a datetime string in UTC in a moment.js " +
77
"supported format in one locale to another datetime string in UTC in " +
78
"another moment.js supported format in another locale", function () {
79
var formatter = new Backgrid.Extension.MomentFormatter({
81
modelFormat: "ddd, DD-MMM-YYYY HH:mm:ss",
83
displayFormat: "dddd, YYYY-MMM-DD HH:mm:ss"
85
expect(formatter.fromRaw("週三, 29-2月-2012 05:30:00")).toBe("mercredi, 2012-févr.-29 05:30:00");
88
it(".fromRaw() can convert a datetime string in a timezone offset in a moment.js supported format in one locale to another datetime string in another timezone offset in another moment.js supported format in another locale", function () {
89
var formatter = new Backgrid.Extension.MomentFormatter({
92
modelFormat: "ddd, DD-MMM-YYYY HH:mm:ss ZZ",
95
displayFormat: "dddd, YYYY-MMM-DD HH:mm:ss ZZ"
97
expect(formatter.fromRaw("週三, 29-2月-2012 05:30:00 +0800")).toBe("mercredi, 2012-févr.-29 05:30:00 +0800");
100
it(".fromRaw() returns an empty string for a null value", function () {
101
var formatter = new Backgrid.Extension.MomentFormatter();
102
expect(formatter.fromRaw(null)).toBe('');
105
it(".fromRaw() returns an empty string for an undefined value", function () {
106
var formatter = new Backgrid.Extension.MomentFormatter();
107
expect(formatter.fromRaw(undefined)).toBe('');
110
it(".toRaw() can convert an UNIX offset to a datetime string in moment.js", function () {
111
var formatter = new Backgrid.Extension.MomentFormatter({
112
displayInUnixOffset: true
114
expect(formatter.toRaw(1318781876406)).toBe('2011-10-16T16:17:56+00:00');
117
it(".toRaw() can convert an UNIX timestamp to a datetime string in moment.js", function () {
118
var formatter = new Backgrid.Extension.MomentFormatter({
119
displayInUnixTimestamp: true
121
expect(formatter.toRaw(1318781876.721)).toBe('2011-10-16T16:17:56+00:00');
124
it(".toRaw() can convert a non-ISO datetime string in UTC in the default locale to the ISO datetime string in UTC in the default locale", function () {
125
var formatter = new Backgrid.Extension.MomentFormatter({
126
displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss"
128
expect(formatter.toRaw("Wed, 29-Feb-2012 05:30:00")).toBe("2012-02-29T05:30:00+00:00");
131
it(".toRaw() can convert a non-ISO datetime string in UTC in a different locale to the ISO datetime string in UTC in a default locale", function () {
132
var formatter = new Backgrid.Extension.MomentFormatter({
133
displayLang: "zh-tw",
134
displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss"
136
expect(formatter.toRaw("週三, 29-2月-2012 05:30:00")).toBe("2012-02-29T05:30:00+00:00");
139
it(".toRaw() can convert a non-ISO datetime string in a timezone offset in a different locale to the ISO datetime string in UTC in a default locale", function () {
140
var formatter = new Backgrid.Extension.MomentFormatter({
142
displayLang: "zh-tw",
143
displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss ZZ"
145
expect(formatter.toRaw("週三, 29-2月-2012 05:30:00 +08:00")).toBe(moment("2012-02-29T05:30:00+08:00").utc().format());
148
it(".toRaw() can convert a non-ISO datetime string in a timezone offset in a different locale to another non-ISO datetime string in UTC in a default locale", function () {
149
var formatter = new Backgrid.Extension.MomentFormatter({
150
modelFormat: "dddd, YYYY-MMM-DD HH:mm:ss",
152
displayLang: "zh-tw",
153
displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss"
155
expect(formatter.toRaw("週三, 29-2月-2012 05:30:00")).toBe("Tuesday, 2012-Feb-28 21:30:00");
158
it(".toRaw() can convert a non-ISO datetime string in a timezone offset in a different locale to another non-ISO datetime string in UTC in a different locale", function () {
159
var formatter = new Backgrid.Extension.MomentFormatter({
161
modelFormat: "dddd, YYYY-MMM-DD HH:mm:ss",
163
displayLang: "zh-tw",
164
displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss"
166
expect(formatter.toRaw("週三, 29-2月-2012 05:30:00")).toBe("mardi, 2012-févr.-28 21:30:00");
169
it(".toRaw() can convert a non-ISO datetime string in a timezone offset in a different locale to another non-ISO datetime string in a timezone offset in a different locale", function () {
170
var formatter = new Backgrid.Extension.MomentFormatter({
173
modelFormat: "dddd, YYYY-MMM-DD HH:mm:ss ZZ",
175
displayLang: "zh-tw",
176
displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss Z"
178
expect(formatter.toRaw("週三, 29-2月-2012 05:30:00 +08:00")).toBe("mercredi, 2012-févr.-29 05:30:00 +0800");
181
it(".toRaw() returns undefined when converting an empty string or a string of whitespaces", function () {
182
var formatter = new Backgrid.Extension.MomentFormatter();
183
expect(formatter.toRaw('')).toBeUndefined();
184
expect(formatter.toRaw(' ')).toBeUndefined();
189
describe("A MomentCell", function () {
191
var Book = Backbone.Model.extend({});
195
beforeEach(function () {
198
birthday: "2012-11-30T12:34:56.789+00:00"
201
cell = new Backgrid.Extension.MomentCell({
210
toBeAnInstanceOf: function (expected) {
211
var actual = this.actual;
212
var notText = this.isNot ? " not" : "";
213
this.message = function () {
214
return "Expected " + actual + notText + " to be an instance of " + expected;
217
return actual instanceof expected;
222
it("uses a default MomentFormatter", function () {
223
expect(cell.formatter).toBeAnInstanceOf(Backgrid.Extension.MomentFormatter);
224
expect(_.pick(cell.formatter, _.keys(cell.formatter))).toEqual(Backgrid.Extension.MomentFormatter.prototype.defaults);
227
it("applies a moment-cell class to the cell", function () {
228
expect(cell.render().$el.hasClass("moment-cell")).toBe(true);
231
it("renders a placeholder for the input format for the editor", function () {
234
expect(cell.currentEditor.$el.attr("placeholder")).toBe(Backgrid.Extension.MomentFormatter.prototype.defaults.displayFormat);