fixing tests
[xtuple] / test / extensions / core / date.js
1 /*jshint trailing:true, white:true, indent:2, strict:true, curly:true,
2   immed:true, eqeqeq:true, forin:true, latedef:true,
3   newcap:true, noarg:true, undef:true */
4 /*global XT:true, XM:true, XV:true, process:true, module:true, require:true,
5   describe:true, before:true, enyo:true, it:true, beforeEach:true */
6
7 (function () {
8   "use strict";
9
10   var zombieAuth = require("../../lib/zombie_auth"),
11     assert = require("chai").assert;
12
13   describe('Date Widget', function () {
14     this.timeout(45 * 1000);
15     var K, newDate;
16
17     before(function (done) {
18       // setup for the date widget
19       var initializeDate = function () {
20         K = enyo.kind({kind: XV.DateWidget});
21         K = new K();
22         done();
23       };
24
25       zombieAuth.loadApp(initializeDate);
26     });
27
28     // reset the date before each test
29     beforeEach(function () {
30       newDate = new Date();
31     });
32
33     describe('Test Text to Date', function () {
34       // Test known bad dates
35       it('Test just plain bad date', function () {
36         assert.isFalse(K.textToDate("********"));
37         assert.isFalse(K.textToDate("BEWARE. I AM BAD."));
38         assert.isFalse(K.textToDate("%"));
39         assert.isFalse(K.textToDate("%123"));
40         assert.isFalse(K.textToDate("///"));
41         assert.isFalse(K.textToDate("1234*"));
42       });
43
44       // Test known good dates
45       it('Test good dates', function () {
46         assert.ok(K.textToDate("2/2/2004"));
47         assert.ok(K.textToDate("2-10-10"));
48         assert.ok(K.textToDate("2000-08-08"));
49         // test the Date ISO string
50         assert.ok(K.textToDate(newDate.toISOString()));
51       });
52
53       // Test entering "#" and a number to get x days in the year
54       it('Test day of the year using #', function () {
55         var days, daysFromStart = function (days) {
56           newDate = new Date();
57           newDate = new Date(newDate.getFullYear(), 0, days, 0, 0, 0, 0);
58           newDate = K.applyTimezoneOffset(newDate);
59           return newDate;
60         };
61
62         days = 20;
63         assert.equal(K.textToDate("#" + days).toDateString(), daysFromStart(days).toDateString());
64         days = 65;
65         assert.equal(K.textToDate("#" + days).toDateString(), daysFromStart(days).toDateString());
66
67         // really, really big one!
68         days = 66666666666666765;
69         assert.isFalse(K.textToDate("#" + days));
70         // more managable number
71         days = 9999999;
72         assert.equal(K.textToDate("#" + days).toDateString(), daysFromStart(days).toDateString());
73
74         assert.isFalse(K.textToDate("#tt"));
75         assert.isFalse(K.textToDate("#"));
76         assert.isFalse(K.textToDate("#*"));
77       });
78
79       // Testing entering "+" and a number to mean days from now
80       it('Test adding days using +', function () {
81         var daysOffset, millisecondOffset = function (offset) {
82           var milliOffset = offset * 24 * 60 * 60 * 1000;
83           newDate = new Date(newDate.getTime() + milliOffset);
84           newDate = K.applyTimezoneOffset(newDate);
85           return newDate;
86         };
87
88         daysOffset = 20;
89         assert.equal(K.textToDate("+" + daysOffset).toDateString(),  millisecondOffset(daysOffset).toDateString());
90
91         assert.isFalse(K.textToDate("+tt"));
92         assert.isFalse(K.textToDate("+"));
93         assert.isFalse(K.textToDate("+*"));
94         assert.isFalse(K.textToDate("+99999999999999"));
95       });
96
97       // Testing entering "-" and a number to mean days before now
98       it('Test subtracting days using -', function () {
99         var daysOffset, millisecondOffset = function (offset) {
100           var milliOffset = offset * 24 * 60 * 60 * 1000;
101           newDate = new Date(newDate.getTime() - milliOffset);
102           newDate = K.applyTimezoneOffset(newDate);
103           return newDate;
104         };
105
106         daysOffset = 20;
107         assert.equal(K.textToDate("-" + daysOffset).toDateString(),  millisecondOffset(daysOffset).toDateString());
108
109         assert.isFalse(K.textToDate("-tt"));
110         assert.isFalse(K.textToDate("-"));
111         assert.isFalse(K.textToDate("-*"));
112         assert.isFalse(K.textToDate("-99999999999999"));
113       });
114
115       // Test entering "0" as today's date
116       it('Test that 0 is today', function () {
117         newDate = K.applyTimezoneOffset(newDate);
118         assert.equal(K.textToDate("0").toDateString(), newDate.toDateString());
119       });
120
121       it('Test that a number alone is a day of the month', function () {
122         var day, setDay = function (day) {
123           newDate = new Date();
124           newDate.setDate(day);
125           return K.applyTimezoneOffset(newDate);
126         };
127         day = "6";
128         assert.equal(K.textToDate(day).toDateString(), setDay(day).toDateString());
129         day = "20";
130         assert.equal(K.textToDate(day).toDateString(), setDay(day).toDateString());
131       });
132     });
133   });
134
135 }());