~caneypuggies/reformedchurcheslocator/couchapp-backbone

« back to all changes in this revision

Viewing changes to _attachments/js/vendor/backgrid/test/extensions/moment-cell.js

  • Committer: Tim Black
  • Date: 2013-09-16 22:50:16 UTC
  • Revision ID: tim@alwaysreformed.com-20130916225016-zk8jiba25z33ew7h
Versioned Bower vendor directory

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  backgrid
 
3
  http://github.com/wyuenho/backgrid
 
4
 
 
5
  Copyright (c) 2013 Jimmy Yuen Ho Wong and contributors
 
6
  Licensed under the MIT @license.
 
7
*/
 
8
describe("A MomentFormatter", function () {
 
9
 
 
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
 
13
    });
 
14
    expect(formatter.fromRaw(1318781876406)).toBe('2011-10-16T16:17:56+00:00');
 
15
  });
 
16
 
 
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
 
20
    });
 
21
    expect(formatter.fromRaw(1318781876.721)).toBe('2011-10-16T16:17:56+00:00');
 
22
  });
 
23
 
 
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");
 
28
  });
 
29
 
 
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({
 
33
      displayInUTC: false
 
34
    });
 
35
    expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe(moment.utc("2012-02-29T05:30:00.100Z").local().format());
 
36
  });
 
37
 
 
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
 
42
    });
 
43
    expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe("Wed, 29-Feb-2012 05:30:00 GMT");
 
44
  });
 
45
 
 
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",
 
50
      displayLang: "zh-tw"
 
51
    });
 
52
    expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe("週三, 29-2月-2012 05:30:00 GMT");
 
53
  });
 
54
 
 
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",
 
60
      displayLang: "zh-tw",
 
61
      displayInUTC: false
 
62
    });
 
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"));
 
64
  });
 
65
 
 
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"
 
72
    });
 
73
    expect(formatter.fromRaw("Wed, 29-Feb-2012 05:30:00")).toBe("02-29-2012 05:30:00");
 
74
  });
 
75
 
 
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({
 
80
      modelLang: "zh-tw",
 
81
      modelFormat: "ddd, DD-MMM-YYYY HH:mm:ss",
 
82
      displayLang: "fr",
 
83
      displayFormat: "dddd, YYYY-MMM-DD HH:mm:ss"
 
84
    });
 
85
    expect(formatter.fromRaw("週三, 29-2月-2012 05:30:00")).toBe("mercredi, 2012-févr.-29 05:30:00");
 
86
  });
 
87
 
 
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({
 
90
      modelInUTC: false,
 
91
      modelLang: "zh-tw",
 
92
      modelFormat: "ddd, DD-MMM-YYYY HH:mm:ss ZZ",
 
93
      displayInUTC: false,
 
94
      displayLang: "fr",
 
95
      displayFormat: "dddd, YYYY-MMM-DD HH:mm:ss ZZ"
 
96
    });
 
97
    expect(formatter.fromRaw("週三, 29-2月-2012 05:30:00 +0800")).toBe("mercredi, 2012-févr.-29 05:30:00 +0800");
 
98
  });
 
99
 
 
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('');
 
103
  });
 
104
 
 
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('');
 
108
  });
 
109
 
 
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
 
113
    });
 
114
    expect(formatter.toRaw(1318781876406)).toBe('2011-10-16T16:17:56+00:00');
 
115
  });
 
116
 
 
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
 
120
    });
 
121
    expect(formatter.toRaw(1318781876.721)).toBe('2011-10-16T16:17:56+00:00');
 
122
  });
 
123
 
 
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"
 
127
    });
 
128
    expect(formatter.toRaw("Wed, 29-Feb-2012 05:30:00")).toBe("2012-02-29T05:30:00+00:00");
 
129
  });
 
130
 
 
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"
 
135
    });
 
136
    expect(formatter.toRaw("週三, 29-2月-2012 05:30:00")).toBe("2012-02-29T05:30:00+00:00");
 
137
  });
 
138
 
 
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({
 
141
      displayInUTC: false,
 
142
      displayLang: "zh-tw",
 
143
      displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss ZZ"
 
144
    });
 
145
    expect(formatter.toRaw("週三, 29-2月-2012 05:30:00 +08:00")).toBe(moment("2012-02-29T05:30:00+08:00").utc().format());
 
146
  });
 
147
 
 
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",
 
151
      displayInUTC: false,
 
152
      displayLang: "zh-tw",
 
153
      displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss"
 
154
    });
 
155
    expect(formatter.toRaw("週三, 29-2月-2012 05:30:00")).toBe("Tuesday, 2012-Feb-28 21:30:00");
 
156
  });
 
157
 
 
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({
 
160
      modelLang: "fr",
 
161
      modelFormat: "dddd, YYYY-MMM-DD HH:mm:ss",
 
162
      displayInUTC: false,
 
163
      displayLang: "zh-tw",
 
164
      displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss"
 
165
    });
 
166
    expect(formatter.toRaw("週三, 29-2月-2012 05:30:00")).toBe("mardi, 2012-févr.-28 21:30:00");
 
167
  });
 
168
 
 
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({
 
171
      modelInUTC: false,
 
172
      modelLang: "fr",
 
173
      modelFormat: "dddd, YYYY-MMM-DD HH:mm:ss ZZ",
 
174
      displayInUTC: false,
 
175
      displayLang: "zh-tw",
 
176
      displayFormat: "ddd, DD-MMM-YYYY HH:mm:ss Z"
 
177
    });
 
178
    expect(formatter.toRaw("週三, 29-2月-2012 05:30:00 +08:00")).toBe("mercredi, 2012-févr.-29 05:30:00 +0800");
 
179
  });
 
180
 
 
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();
 
185
  });
 
186
 
 
187
});
 
188
 
 
189
describe("A MomentCell", function () {
 
190
 
 
191
  var Book = Backbone.Model.extend({});
 
192
  var book;
 
193
  var cell;
 
194
 
 
195
  beforeEach(function () {
 
196
 
 
197
    book = new Book({
 
198
      birthday: "2012-11-30T12:34:56.789+00:00"
 
199
    });
 
200
 
 
201
    cell = new Backgrid.Extension.MomentCell({
 
202
      column: {
 
203
        name: "birthday",
 
204
        cell: "moment"
 
205
      },
 
206
      model: book
 
207
    });
 
208
 
 
209
    this.addMatchers({
 
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;
 
215
        };
 
216
 
 
217
        return actual instanceof expected;
 
218
      }
 
219
    });
 
220
  });
 
221
 
 
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);
 
225
  });
 
226
 
 
227
  it("applies a moment-cell class to the cell", function () {
 
228
    expect(cell.render().$el.hasClass("moment-cell")).toBe(true);
 
229
  });
 
230
 
 
231
  it("renders a placeholder for the input format for the editor", function () {
 
232
    cell.render();
 
233
    cell.$el.click();
 
234
    expect(cell.currentEditor.$el.attr("placeholder")).toBe(Backgrid.Extension.MomentFormatter.prototype.defaults.displayFormat);
 
235
  });
 
236
 
 
237
});