]> khamidou — GitHub mirrors - kite.git/commitdiff
display dates correctly + unit tests for feature
authorkarim hamidou <khamidou@gmail.com>
Fri, 1 Nov 2013 13:29:53 +0000 (14:29 +0100)
committerkarim hamidou <khamidou@gmail.com>
Fri, 1 Nov 2013 13:29:53 +0000 (14:29 +0100)
src/front/app/js/filters.js
src/front/app/js/services.js
src/front/test/unit/filters.spec.js
src/front/test/unit/services.spec.js

index cf72dbc7eb936914b5f34a4a2d081782e5ade4a3..2759a9665b82c817e9c44aa28c3fb8c9095e6f01 100644 (file)
@@ -2,7 +2,7 @@
 
 /* Filters */
 
-angular.module('KiteMail.filters', []).
+angular.module('KiteMail.filters', ['KiteMail.services']).
     filter('truncate', function() {
             return function(text) {
             return text.slice(0, 35) + "...";
@@ -10,14 +10,10 @@ angular.module('KiteMail.filters', []).
     }).
     
     /* takes a date object and returns a formatted object */
-    filter('formatDate', function() {
+    filter('formatDate', function(Utils) {
         return function(date) {
-            var date2array = function(date) {
-                return (date+'').split(' ');
-            }
-
-            var today = date2array(new Date(Date.now()));
-            var da = date2array(date);
+            var today = Utils.date2array(new Date(Date.now()));
+            var da = Utils.date2array(date);
 
             if(da[3] == today[3]) {
                 return da[1] + ", " + da[2];
index 20efc426248fc3076e27b55a3be351963603656b..175d560023b6b9edefafc216e31bff9e99f9db51 100644 (file)
@@ -13,7 +13,6 @@ factory('Emails', ['$resource',
                 transformResponse: function(data) {
                     var data = angular.fromJson(data);
 
-                    debugger;
                     // convert date string to js date objects
                     for(var i = 0; i < data.length; i++) {
                         data[i]["date"] = new Date(data[i]["date"]);
@@ -21,12 +20,17 @@ factory('Emails', ['$resource',
 
                     return data;
                 },
-                
             },
        });
         
         return res;
     }
-]);
+]).factory('Utils', function() {
+    return {
+         date2array: function(date) {
+            return (date+'').split(' ');
+        }
+    };   
+});
 
 
index c4170c944351fe133b3b60a4b6dbb4ee3b0e72b2..ffe867d3af1871a5b64eaa1bdc268d72e19386ac 100644 (file)
@@ -11,13 +11,13 @@ describe('filter', function() {
     }));
 
 
-    it('should truncate text', inject(function(truncateFilter) {
+    it('should truncate text', inject(function(Utils, truncateFilter) {
         var testStr = "This is a long string with a lot of chars";
         var resStr = "This is a long string with a lot of" + "...";
         expect(truncateFilter(testStr)).toEqual(resStr);
     }));
 
-    it('should format a date', inject(function(formatDateFilter) {
+    it('should format a date', inject(function(Utils, formatDateFilter) {
         var date = new Date(2013, 9, 13);
         expect(formatDateFilter(date)).toEqual("Oct, 13");
     }));
index 64f1b6d7c4aa4edf6097e660fb813735a84ecc82..f8811cb38729cc4ffa1bf00268d6d64e6e943d42 100644 (file)
@@ -2,19 +2,10 @@
 
 /* jasmine specs for services go here */
 
-describe('service', function() {
+describe('Emails service', function() {
     var mockResource, $httpBackend;
     beforeEach(angular.mock.module('KiteMail.services'));
     
-    beforeEach(function() {
-        inject(function($injector) {
-            /*
-            $httpBackend = $injector.get('$httpBackend');
-            mockResource = $injector.get('Emails');
-            */
-        });
-    });
-
     describe('EmailsService', function() {
     it('should do an ajax request to get the list of emails', inject(function(Emails, $httpBackend) {
         var mockResponse = [
@@ -23,14 +14,31 @@ describe('service', function() {
              "name": "Michel De Test",
              "from": "michel@example.com",
              "to": "gerard@example.com",
-             "date": new Date(1992, 4, 24),
+             "date": "September 24 2013"
             }
         ];
 
         $httpBackend.expectGET('/kite/test/mail').respond(mockResponse);
         var result = Emails.emails({username: "test"});
-        console.log(result);
-        expect(result == mockResponse).toBe(true);
+        $httpBackend.flush();
+        var date = result[0]["date"];
+        expect(date instanceof Date).toBe(true);
+        expect(date.getDate() == 24).toBe(true);
+        expect(date.getMonth() == 8).toBe(true);
+        expect(date.getFullYear() == 2013).toBe(true);
+
+    }))});
+});
 
+describe('Utils service', function() {
+    beforeEach(angular.mock.module('KiteMail.services'));
+    
+    describe('date2array', function() {
+    it("should convert dates to arrays", inject(function(Utils) {
+        var date = new Date("May 30 2013");
+        var array = Utils.date2array(date);
+        expect(array[1]).toEqual("May");
+        expect(array[2]).toEqual("30");
+        expect(array[3]).toEqual("2013");
     }))});
 });