Completed
Push — master ( 334bf8...741e42 )
by Felipe
38s
created

test/formatter.js   C

Complexity

Total Complexity 60
Complexity/F 1

Size

Lines of Code 397
Function Count 60

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
c 1
b 0
f 0
nc 1
dl 0
loc 397
rs 5.1111
wmc 60
mnd 0
bc 60
fnc 60
bpm 1
cpm 1
noi 59

How to fix   Complexity   

Complexity

Complex classes like test/formatter.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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 CellFormatter", function () {
9
10
  it("must have a function fromRaw that accepts 1 parameter and passes the argument thru", function () {
11
12
    var formatter = new Backgrid.CellFormatter;
13
14
    expect(formatter.fromRaw).toBeDefined();
15
    expect(formatter.fromRaw("raw")).toBe("raw");
16
  });
17
18
  it("must have a function toRaw that accepts 1 parameter and passes the argument thru", function () {
19
20
    var formatter = new Backgrid.CellFormatter;
21
22
    expect(formatter.toRaw).toBeDefined();
23
    expect(formatter.toRaw("formatted")).toBe("formatted");
24
  });
25
26
});
27
28
describe("A NumberFormatter", function () {
29
30
  it("throws RangeError if a options.decimals is less than 0 or greater than 20", function () {
31
32
    expect(function () {
33
      new Backgrid.NumberFormatter({
34
        decimals: -1
35
      });
36
    }).toThrow(new RangeError("decimals must be between 0 and 20"));
37
38
    expect(function () {
39
      new Backgrid.NumberFormatter({
40
        decimals: 21
41
      });
42
    }).toThrow(new RangeError("decimals must be between 0 and 20"));
43
44
  });
45
46
  it(".fromRaw() converts a number to a number string with 2 decimals, with " +
47
    "1000s separated by ',' and the decimal part separated by '.' by default",
48
    function () {
49
      var formatter = new Backgrid.NumberFormatter();
50
      expect(formatter.fromRaw(1000003.1415926)).toBe("1,000,003.14");
51
    });
52
53
  it(".fromRaw() can convert a number to a number string with any number of " +
54
    "decimals between 0 and 20 inclusive, and 1000s separated by any string" +
55
    " and the decimal part separated by and string",
56
    function () {
57
58
      var formatter = new Backgrid.NumberFormatter({
59
        decimals: 3,
60
        orderSeparator: '.',
61
        decimalSeparator: ','
62
      });
63
64
      expect(formatter.fromRaw(1000003.1415926)).toBe("1.000.003,142");
65
    });
66
67
  it(".fromRaw() returns an empty string if the input is null or undefined", function () {
68
    var formatter = new Backgrid.NumberFormatter();
69
    expect(formatter.fromRaw()).toBe('');
70
    expect(formatter.fromRaw(undefined)).toBe('');
71
  });
72
73
  it(".toRaw() converts a blank string to null", function () {
74
    var formatter = new Backgrid.NumberFormatter();
75
    expect(formatter.toRaw('')).toBe(null);
76
    expect(formatter.toRaw(' ')).toBe(null);
77
  });
78
79
  it(".toRaw() converts a number string with any number of decimals, with " +
80
    "1000s separated by ',' and the decimal part separated by '.' to a " +
81
    "number by default",
82
    function () {
83
84
      var formatter = new Backgrid.NumberFormatter();
85
      expect(formatter.toRaw("1,000,003.141592653589793238462")).toBe(1000003.14);
86
    });
87
88
  it(".toRaw() can convert a number string with any number of decimals, 1000s" +
89
    " separated by any string and the decimal part separated by any string to" +
90
    " a number",
91
    function () {
92
93
      var formatter = new Backgrid.NumberFormatter({
94
        decimals: 3,
95
        orderSeparator: '.',
96
        decimalSeparator: ','
97
      });
98
99
      expect(formatter.toRaw("1.000.003,141592653589793238462")).toBe(1000003.142);
100
    });
101
102
  it(".toRaw() returns undefined for invalid number strings", function () {
103
    var formatter = new Backgrid.NumberFormatter();
104
    expect(formatter.toRaw('ads')).toBeUndefined();
105
  });
106
107
});
108
109
describe("A PercentFormatter", function () {
110
111
  it(".fromRaw() converts a number to a string by multipling it by a multiplier and appending a symbol", function () {
112
    var formatter = new Backgrid.PercentFormatter();
113
    expect(formatter.fromRaw(99.8)).toBe("99.80%");
114
115
    formatter.multiplier = 100;
116
    expect(formatter.fromRaw(0.998)).toBe("99.80%");
117
118
    formatter.symbol = "pct";
119
    expect(formatter.fromRaw(0.998)).toBe("99.80pct");
120
  });
121
122
  it(".toRaw() converts a string to a number by removing the symbol and dividing it by the multiplier", function () {
123
    var formatter = new Backgrid.PercentFormatter();
124
    expect(formatter.toRaw("99.8%")).toBe(99.8);
125
    expect(formatter.toRaw("99.8")).toBe(99.8);
126
127
    formatter.multiplier = 100;
128
    expect(formatter.toRaw("99.8%")).toBe(0.998);
129
130
    formatter.symbol = "pct";
131
    expect(formatter.toRaw("99.8pct")).toBe(0.998);
132
  });
133
134
  it(".toRaw() returns undefined for invalid number or percent strings", function () {
135
    var formatter = new Backgrid.PercentFormatter();
136
    expect(formatter.toRaw("abc")).toBeUndefined();
137
    expect(formatter.toRaw("0.1pct")).toBeUndefined();
138
  });
139
140
});
141
142
describe("A DatetimeFormatter", function () {
143
144
  it("throws Error if includeDate and includeTime are both false", function () {
145
146
    expect(function () {
147
      new Backgrid.DatetimeFormatter({
148
        includeDate: false,
149
        includeTime: false
150
      });
151
    }).toThrow(new Error("Either includeDate or includeTime must be true"));
152
153
  });
154
155
  it(".fromRaw() can convert an UNIX offset to an ISO datetime string", function () {
156
    var formatter = new Backgrid.DatetimeFormatter;
157
    expect(formatter.fromRaw(1356998400000)).toBe("2013-01-01T00:00:00Z");
158
  });
159
160
  it(".fromRaw() can convert an ISO datetime string to an ISO date string", function () {
161
    var formatter = new Backgrid.DatetimeFormatter({
162
      includeTime: false
163
    });
164
    expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe("2012-02-29");
165
  });
166
167
  it(".fromRaw() can convert an ISO datetime string to an ISO time string", function () {
168
    var formatter = new Backgrid.DatetimeFormatter({
169
      includeDate: false
170
    });
171
    expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe("05:30:00");
172
  });
173
174
  it(".fromRaw() can convert an ISO datetime string to an ISO time string with milliseconds", function () {
175
    var formatter = new Backgrid.DatetimeFormatter({
176
      includeDate: false,
177
      includeMilli: true
178
    });
179
    expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe("05:30:00.100");
180
  });
181
182
  it(".fromRaw() can convert an ISO datetime string to an ISO datetime string with milliseconds", function () {
183
    var formatter = new Backgrid.DatetimeFormatter({
184
      includeMilli: true
185
    });
186
    expect(formatter.fromRaw("2012-02-29T05:30:00.100Z")).toBe("2012-02-29T05:30:00.100Z");
187
  });
188
189
  it(".fromRaw() can convert an ISO date string to an ISO datetime string", function () {
190
    var formatter = new Backgrid.DatetimeFormatter;
191
    expect(formatter.fromRaw("2012-02-29")).toBe("2012-02-29T00:00:00Z");
192
  });
193
194
  it(".fromRaw() can convert an ISO date string to an ISO date string", function () {
195
    var formatter = new Backgrid.DatetimeFormatter({
196
      includeTime: false
197
    });
198
    expect(formatter.fromRaw("2012-02-29")).toBe("2012-02-29");
199
  });
200
201
  it(".fromRaw() can convert an ISO time string to an ISO time string", function () {
202
    var formatter = new Backgrid.DatetimeFormatter({
203
      includeDate: false
204
    });
205
    expect(formatter.fromRaw("05:30:29.123")).toBe("05:30:29");
206
  });
207
208
  it(".fromRaw() can convert an ISO time string to an ISO time string with milliseconds", function () {
209
    var formatter = new Backgrid.DatetimeFormatter({
210
      includeDate: false,
211
      includeMilli: true
212
    });
213
    expect(formatter.fromRaw("05:30:29.123")).toBe("05:30:29.123");
214
  });
215
216
  it(".fromRaw() returns an empty string for a null value", function () {
217
    var formatter = new Backgrid.DatetimeFormatter({
218
      includeDate: true
219
    });
220
    expect(formatter.fromRaw(null)).toBe('');
221
  });
222
223
  it(".fromRaw() returns an empty string for an undefined value", function () {
224
    var formatter = new Backgrid.DatetimeFormatter({
225
      includeDate: true
226
    });
227
    expect(formatter.fromRaw(undefined)).toBe('');
228
  });
229
230
  it(".fromRaw() throws an exception on invalid values", function () {
231
    var formatter = new Backgrid.DatetimeFormatter({
232
      includeDate: true
233
    });
234
235
    expect(function () {
236
      formatter.fromRaw(false);
237
    }).toThrow();
238
  });
239
240
  it(".toRaw() returns null when a blank string is supplied", function () {
241
    var formatter = new Backgrid.DatetimeFormatter();
242
    expect(formatter.toRaw('')).toBe(null);
243
    expect(formatter.toRaw(' ')).toBe(null);
244
  });
245
246
  it(".toRaw() returns undefined when converting an ISO datetime string to an ISO date string", function () {
247
    var formatter = new Backgrid.DatetimeFormatter({
248
      includeTime: false
249
    });
250
    expect(formatter.toRaw("2012-02-29T05:30:00.100Z")).toBe(undefined);
251
  });
252
253
  it(".toRaw() returns undefined when converting an ISO datetime string to an ISO time string", function () {
254
    var formatter = new Backgrid.DatetimeFormatter({
255
      includeDate: false
256
    });
257
    expect(formatter.toRaw("2012-02-29T05:30:00.100Z")).toBe(undefined);
258
  });
259
260
  it(".toRaw() returns undefined when converting an ISO datetime string to an ISO time string with milliseconds", function () {
261
    var formatter = new Backgrid.DatetimeFormatter({
262
      includeDate: false,
263
      includeMilli: true
264
    });
265
    expect(formatter.toRaw("2012-02-29T05:30:00.100Z")).toBe(undefined);
266
  });
267
268
  it(".toRaw() can convert an ISO datetime string to an ISO datetime string", function () {
269
    var formatter = new Backgrid.DatetimeFormatter;
270
    expect(formatter.toRaw("2012-02-29T05:30:00.100Z")).toBe("2012-02-29T05:30:00Z");
271
  });
272
273
  it(".toRaw() can convert an ISO datetime string to an ISO datetime string with milliseconds", function () {
274
    var formatter = new Backgrid.DatetimeFormatter({
275
      includeMilli: true
276
    });
277
    expect(formatter.toRaw("2012-02-29T05:30:00.100Z")).toBe("2012-02-29T05:30:00.100Z");
278
  });
279
280
  it(".toRaw() returns undefined when converting an ISO date string to an ISO datetime string", function () {
281
    var formatter = new Backgrid.DatetimeFormatter;
282
    expect(formatter.toRaw("2012-02-29")).toBe(undefined);
283
  });
284
285
  it(".toRaw() returns undefined when converting an ISO date string to an ISO datetime string with milliseconds", function () {
286
    var formatter = new Backgrid.DatetimeFormatter({
287
      includeMilli: true
288
    });
289
    expect(formatter.toRaw("2012-02-29")).toBe(undefined);
290
  });
291
292
  it(".toRaw() returns undefined when converting an ISO date string to an ISO time string", function () {
293
    var formatter = new Backgrid.DatetimeFormatter({
294
      includeDate: false
295
    });
296
    expect(formatter.toRaw("2012-02-29")).toBe(undefined);
297
  });
298
299
  it(".toRaw() returns undefined when converting an ISO date string to an ISO time string with milliseconds", function () {
300
    var formatter = new Backgrid.DatetimeFormatter({
301
      includeDate: false,
302
      includeMilli: true
303
    });
304
    expect(formatter.toRaw("2012-02-29")).toBe(undefined);
305
  });
306
307
  it(".toRaw() can convert an ISO date string to an ISO date string", function () {
308
    var formatter = new Backgrid.DatetimeFormatter({
309
      includeTime: false
310
    });
311
    expect(formatter.toRaw("2012-02-29")).toBe("2012-02-29");
312
  });
313
314
  it(".toRaw() returns undefined when converting an ISO time string to an ISO date string", function () {
315
    var formatter = new Backgrid.DatetimeFormatter({
316
      includeTime: false
317
    });
318
    expect(formatter.toRaw("05:30:29.123")).toBe(undefined);
319
  });
320
321
  it(".toRaw() returns undefined when converting an ISO time string to an ISO datetime string", function () {
322
    var formatter = new Backgrid.DatetimeFormatter;
323
    expect(formatter.toRaw("05:30:29.123")).toBe(undefined);
324
  });
325
326
  it(".toRaw() returns undefined when converting an ISO time string to an ISO datetime string with milliseconds", function () {
327
    var formatter = new Backgrid.DatetimeFormatter({
328
      includeMilli: true
329
    });
330
    expect(formatter.toRaw("05:30:29.123")).toBe(undefined);
331
  });
332
333
  it(".toRaw() can convert an ISO time string to an ISO time string", function () {
334
    var formatter = new Backgrid.DatetimeFormatter({
335
      includeDate: false
336
    });
337
    expect(formatter.toRaw("05:30:29.123")).toBe("05:30:29");
338
  });
339
340
  it(".toRaw() can convert an ISO time string to an ISO time string with milliseconds", function () {
341
    var formatter = new Backgrid.DatetimeFormatter({
342
      includeDate: false,
343
      includeMilli: true
344
    });
345
    expect(formatter.toRaw("05:30:29.123")).toBe("05:30:29.123");
346
  });
347
});
348
349
describe("A StringFormatter", function () {
350
351
  var formatter;
352
  beforeEach(function () {
353
    formatter = new Backgrid.StringFormatter();
354
  });
355
356
  it(".fromRaw() converts anything besides null and undefind to a string", function () {
357
    expect(formatter.fromRaw(1)).toBe("1");
358
    expect(formatter.fromRaw(1.1)).toBe("1.1");
359
    expect(formatter.fromRaw("string")).toBe("string");
360
    expect(formatter.fromRaw('')).toBe('');
361
    expect(formatter.fromRaw(NaN)).toBe('NaN');
362
  });
363
364
  it(".fromRaw() converts null and undefind to a empty string", function () {
365
    expect(formatter.fromRaw(null)).toBe('');
366
    expect(formatter.fromRaw(undefined)).toBe('');
367
  });
368
369
  it(".toRaw() pass any string thru", function () {
370
    expect(formatter.toRaw("string")).toBe("string");
371
    expect(formatter.toRaw("")).toBe('');
372
    expect(formatter.toRaw(" ")).toBe(' ');
373
  });
374
375
});
376
377
describe("An EmailFormatter", function () {
378
  var formatter;
379
380
  beforeEach(function () {
381
    formatter = new Backgrid.EmailFormatter();
382
  });
383
384
  it(".fromRaw() accepts any string without conversion", function () {
385
    expect(formatter.fromRaw("[email protected]")).toBe("[email protected]");
386
    expect(formatter.fromRaw('')).toBe('');
387
    expect(formatter.fromRaw(" ")).toBe(" ");
388
  });
389
390
  it(".toRaw() returns undefined for invalid email addresses", function () {
391
    expect(formatter.toRaw('')).toBeUndefined();
392
    expect(formatter.toRaw(' ')).toBeUndefined();
393
    expect(formatter.toRaw('@')).toBeUndefined();
394
    expect(formatter.toRaw(' @ ')).toBeUndefined();
395
    expect(formatter.toRaw("a@")).toBeUndefined();
396
    expect(formatter.toRaw("@b")).toBeUndefined();
397
    expect(formatter.toRaw("a@b@")).toBeUndefined();
398
  });
399
400
  it(".toRaw() returns the input if it contains a '@' and the strings before and after '@' are not empty", function () {
401
    expect(formatter.toRaw("a@b")).toBe("a@b");
402
  });
403
404
});
405