www

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | Submodules | README | LICENSE

dateTest.js (6014B)


      1 describe("Zotero.Date", function() {
      2 	describe("#getMonths()", function () {
      3 		var origLocale;
      4 		var englishShort = [
      5 			"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
      6 		];
      7 		var englishLong = [
      8 			"January", "February", "March", "April", "May", "June", "July", "August", "September",
      9 			"October", "November", "December"
     10 		];
     11 		var frenchShort = [
     12 			"jan", "fév", "mar", "avr", "mai", "juin", "juil", "aoû", "sep", "oct", "nov", "déc"
     13 		];
     14 		var frenchLong = [
     15 			"janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre",
     16 			"octobre", "novembre", "décembre"
     17 		];
     18 		
     19 		before(function () {
     20 			origLocale = Zotero.locale;
     21 		});
     22 		
     23 		after(function () {
     24 			Zotero.locale = origLocale;
     25 		});
     26 		
     27 		describe("English", function () {
     28 			beforeEach(function* () {
     29 				if (Zotero.locale != 'en-US') {
     30 					Zotero.locale = 'en-US';
     31 					yield Zotero.Date.init();
     32 				}
     33 			});
     34 			
     35 			it("should get English short months", function () {
     36 				let months = Zotero.Date.getMonths().short;
     37 				assert.lengthOf(months, 12);
     38 				assert.sameMembers(months, englishShort);
     39 			});
     40 			
     41 			it("should get English long months", function () {
     42 				let months = Zotero.Date.getMonths().long;
     43 				assert.lengthOf(months, 12);
     44 				assert.sameMembers(months, englishLong);
     45 			});
     46 			
     47 			it("shouldn't repeat months in 'withEnglish' mode", function () {
     48 				let months = Zotero.Date.getMonths(true).short;
     49 				assert.lengthOf(months, 12);
     50 				assert.sameMembers(months, englishShort);
     51 			});
     52 			
     53 			it("should resolve to English from unknown locale", function* () {
     54 				Zotero.locale = 'zz';
     55 				yield Zotero.Date.init();
     56 				let months = Zotero.Date.getMonths().short;
     57 				assert.lengthOf(months, 12);
     58 				assert.sameMembers(months, englishShort);
     59 			});
     60 			
     61 			it("shouldn't repeat English with unknown locale", function* () {
     62 				Zotero.locale = 'zz';
     63 				yield Zotero.Date.init();
     64 				let months = Zotero.Date.getMonths(true).short;
     65 				assert.lengthOf(months, 12);
     66 				assert.sameMembers(months, englishShort);
     67 			});
     68 		});
     69 		
     70 		describe("French", function () {
     71 			beforeEach(function* () {
     72 				if (Zotero.locale != 'fr-FR') {
     73 					Zotero.locale = 'fr-FR';
     74 					yield Zotero.Date.init();
     75 				}
     76 			});
     77 			
     78 			it("should get French short months", function () {
     79 				let months = Zotero.Date.getMonths().short;
     80 				assert.lengthOf(months, 12);
     81 				assert.sameMembers(months, frenchShort);
     82 			});
     83 			
     84 			it("should get French long months", function () {
     85 				let months = Zotero.Date.getMonths().long;
     86 				assert.lengthOf(months, 12);
     87 				assert.sameMembers(months, frenchLong);
     88 			});
     89 			
     90 			it("should get French short months with English", function () {
     91 				let months = Zotero.Date.getMonths(true).short;
     92 				assert.lengthOf(months, 24);
     93 				assert.sameMembers(months, frenchShort.concat(englishShort));
     94 			});
     95 			
     96 			it("should get French long months with English", function () {
     97 				let months = Zotero.Date.getMonths(true).long;
     98 				assert.lengthOf(months, 24);
     99 				assert.sameMembers(months, frenchLong.concat(englishLong));
    100 			});
    101 			
    102 			it("should resolve from two-letter locale", function* () {
    103 				Zotero.locale = 'fr';
    104 				yield Zotero.Date.init();
    105 				let months = Zotero.Date.getMonths().short;
    106 				assert.lengthOf(months, 12);
    107 				assert.sameMembers(months, frenchShort);
    108 			});
    109 			
    110 			it("should resolve from unknown four-letter locale with common prefix", function* () {
    111 				Zotero.locale = 'fr-ZZ';
    112 				yield Zotero.Date.init();
    113 				let months = Zotero.Date.getMonths().short;
    114 				assert.lengthOf(months, 12);
    115 				assert.sameMembers(months, frenchShort);
    116 			});
    117 		});
    118 	});
    119 	
    120 	describe("#sqlToDate()", function () {
    121 		it("should convert an SQL local date into a JS Date object", function* () {
    122 			var d1 = new Date();
    123 			var sqlDate = d1.getFullYear()
    124 				+ '-'
    125 				+ Zotero.Utilities.lpad(d1.getMonth() + 1, '0', 2)
    126 				+ '-'
    127 				+ Zotero.Utilities.lpad(d1.getDate(), '0', 2)
    128 				+ ' '
    129 				+ Zotero.Utilities.lpad(d1.getHours(), '0', 2)
    130 				+ ':'
    131 				+ Zotero.Utilities.lpad(d1.getMinutes(), '0', 2)
    132 				+ ':'
    133 				+ Zotero.Utilities.lpad(d1.getSeconds(), '0', 2);
    134 			var offset = d1.getTimezoneOffset() * 60 * 1000;
    135 			var d2 = Zotero.Date.sqlToDate(sqlDate);
    136 			assert.equal(
    137 				Zotero.Date.sqlToDate(sqlDate).getTime(),
    138 				Math.floor(new Date().getTime() / 1000) * 1000
    139 			);
    140 		})
    141 		
    142 		it("should convert an SQL UTC date into a JS Date object", function* () {
    143 			var date = "2016-02-27 22:00:00";
    144 			date = Zotero.Date.sqlToDate(date, true);
    145 			assert.equal(date.getTime(), 1456610400000);
    146 		})
    147 	})
    148 	
    149 	describe("#isISODate()", function () {
    150 		it("should determine whether a date is an ISO 8601 date", function () {
    151 			assert.ok(Zotero.Date.isISODate("2015"));
    152 			assert.ok(Zotero.Date.isISODate("2015-04"));
    153 			assert.ok(Zotero.Date.isISODate("2015-04-29"));
    154 			assert.ok(Zotero.Date.isISODate("2015-04-29T17:28Z"));
    155 			assert.isFalse(Zotero.Date.isISODate("2015-04-29 17:28"));
    156 		})
    157 	})
    158 	
    159 	describe("#strToDate()", function () {
    160 		it("should work in translator sandbox", function* () {
    161 			var item = createUnsavedDataObject('item');
    162 			item.libraryID = Zotero.Libraries.userLibraryID;
    163 			item.setField('date', '2017-01-17');
    164 			
    165 			var called = false;
    166 			var translation = new Zotero.Translate.Export();
    167 			translation.setItems([item]);
    168 			translation.setTranslator("9cb70025-a888-4a29-a210-93ec52da40d4"); // BibTeX
    169 			translation.setHandler("done", function (obj, worked) {
    170 				called = true;
    171 				assert.isTrue(worked);
    172 				assert.include(obj.string, "{2017}");
    173 			});
    174 			yield translation.translate();
    175 			assert.ok(called);
    176 		});
    177 	});
    178 	
    179 	describe("#isHTTPDate()", function() {
    180 		it("should determine whether a date is an RFC 2822 compliant date", function() {
    181 			assert.ok(Zotero.Date.isHTTPDate("Mon, 13 Jun 2016 02:09:08   +4000"));
    182 			assert.ok(Zotero.Date.isHTTPDate("13 Jun 2016 02:09:08 +4000"));
    183 			assert.ok(Zotero.Date.isHTTPDate("13 Jun 2016   02:09 +4000"));
    184 			assert.ok(Zotero.Date.isHTTPDate("13  Jun  2016 02:09 EDT"));
    185 		})
    186 	})
    187 })