Browse Source

imported luticate1 utils; tests

tags/v0.1.1
Robin Thoni 8 years ago
parent
commit
3eda97d520

+ 24
- 0
Gruntfile.js View File

@@ -0,0 +1,24 @@
1
+/**
2
+ * Created by robin on 11/10/15.
3
+ */
4
+
5
+module.exports = function(grunt) {
6
+
7
+    grunt.initConfig({
8
+        uglify: {
9
+            demo: {
10
+                files: {
11
+                    'dist/luticate-utils.min.js': [
12
+                        'src/luticateutils.js',
13
+                        'src/**/*.js'
14
+                    ]
15
+                }
16
+            }
17
+        }
18
+    });
19
+
20
+    grunt.loadNpmTasks('grunt-contrib-uglify');
21
+
22
+    grunt.registerTask('default', ['uglify']);
23
+
24
+};

+ 18
- 0
bower.json View File

@@ -0,0 +1,18 @@
1
+{
2
+  "name": "luticate2-utils",
3
+  "version": "0.1.0",
4
+  "authors": [
5
+    "Robin THONI <robin@rthoni.com>"
6
+  ],
7
+  "description": "Luticate front utils",
8
+  "license": "MIT",
9
+  "dependencies": {
10
+  },
11
+  "ignore": [
12
+    "**/.*",
13
+    "node_modules",
14
+    "bower_components",
15
+    "test",
16
+    "tests"
17
+  ]
18
+}

+ 1
- 0
dist/luticate-utils.min.js View File

@@ -0,0 +1 @@
1
+!function(){angular.module("luticate2-utils",[])}(),function(){"use strict";angular.module("luticateUtils").factory("luPromise",[function(){var a={};return a.promises={},a.loaders=[],a.errors=[],a.STATUS_RUNNING=0,a.STATUS_RESOLVED=1,a.STATUS_REJECTED=2,a.build=function(b){a[b]={};for(var c in a.promises)if(a.promises.hasOwnProperty(c)){var d=a.promises[c];d[b].forEach(function(c){null==a[b][c]&&(a[b][c]=[]),a[b][c].push(d)})}},a.newPromise=function(a,b,c,d){return{id:a,groups:b,loaderGroups:c,errorGroups:d}},a.initPromise=function(b){var c={loaders:[],errors:[],status:a.STATUS_RUNNING,id:b.id};b.groups instanceof Array?b.groups.forEach(function(a){c.loaders.push(a),c.errors.push(a)}):"string"==typeof b.groups&&(c.loaders.push(b.groups),c.errors.push(b.groups)),b.loaderGroups instanceof Array?b.loaderGroups.forEach(function(a){c.loaders.push(a)}):"string"==typeof b.loaderGroups&&c.loaders.push(b.loaderGroups),b.errorGroups instanceof Array?b.errorGroups.forEach(function(a){c.errors.push(a)}):"string"==typeof b.errorGroups&&c.errors.push(b.errorGroups),a.promises[b.id]=c,a.build("loaders"),a.build("errors")},a.getLoadersGroup=function(b){return null==a.loaders[b]?null:a.loaders[b]},a.getErrorsGroup=function(b){return null==a.errors[b]?null:a.errors[b]},a.reject=function(b,c){a.promises[b.id].status=a.STATUS_REJECTED,a.promises[b.id].value=c},a.resolve=function(b,c){a.promises[b.id].status=a.STATUS_RESOLVED,a.promises[b.id].value=c},a}])}(),function(){"use strict";angular.module("luticateUtils").factory("luRequest",["$q","$http","luPromise",function(a,b,c){function d(a,b,d,e){var f={Data:b,Status:d};a.reject(f),null!=e&&c.reject(e,f)}function e(a,b,d){a.resolve(b),null!=d&&c.resolve(d,b)}var f={};return f.get=function(a,b,c){return f.request(a,"GET",b,null,c)},f.post=function(a,b,c,d){return f.request(a,"POST",b,c,d)},f.put=function(a,b,c,d){return f.request(a,"PUT",b,c,d)},f["delete"]=function(a,b,c,d){return f.request(a,"DELETE",b,c,d)},f.request=function(f,g,h,i,j){null!=j&&c.initPromise(j);var k=a.defer();f=f.replace(/\/+$/,"");var l={url:f,method:g,params:h,data:null==i?null:$.param(i),headers:{"Content-Type":"application/x-www-form-urlencoded"}};return b(l).success(function(a,b){if(null==a||null!=a.Message){var c=null==a?"Failed to parse response":a.Message;d(k,c,b,j)}else e(k,a.Data,j)}).error(function(a,b){if(null==a||null!=a.Message){var c=null==a?"Failed to parse response":a.Message;d(k,c,b,j)}else d(k,a,b,j)}),k.promise},f}])}();

+ 74
- 0
karma.conf.js View File

@@ -0,0 +1,74 @@
1
+// Karma configuration
2
+// Generated on Sun Dec 11 2016 16:29:58 GMT-0500 (EST)
3
+
4
+module.exports = function(config) {
5
+  config.set({
6
+
7
+    // base path that will be used to resolve all patterns (eg. files, exclude)
8
+    basePath: '',
9
+
10
+
11
+    // frameworks to use
12
+    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
13
+    frameworks: ['jasmine'],
14
+
15
+
16
+    // list of files / patterns to load in the browser
17
+    files: [
18
+        './node_modules/angular/angular.js',
19
+        './node_modules/angular-ui-router/release/angular-ui-router.js',
20
+        './node_modules/angular-mocks/angular-mocks.js',
21
+        './src/luticate2-utils.js',
22
+        './src/**/*.js',
23
+        './tests/**/*.spec.js'
24
+    ],
25
+
26
+
27
+    // list of files to exclude
28
+    exclude: [
29
+    ],
30
+
31
+
32
+    // preprocess matching files before serving them to the browser
33
+    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
34
+    preprocessors: {
35
+    },
36
+
37
+
38
+    // test results reporter to use
39
+    // possible values: 'dots', 'progress'
40
+    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
41
+    reporters: ['progress'],
42
+
43
+
44
+    // web server port
45
+    port: 9876,
46
+
47
+
48
+    // enable / disable colors in the output (reporters and logs)
49
+    colors: true,
50
+
51
+
52
+    // level of logging
53
+    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
54
+    logLevel: config.LOG_INFO,
55
+
56
+
57
+    // enable / disable watching file and executing tests whenever any file changes
58
+    autoWatch: true,
59
+
60
+
61
+    // start these browsers
62
+    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
63
+    browsers: ['Chrome'],
64
+
65
+
66
+    // Continuous Integration mode
67
+    // if true, Karma captures browsers, runs the tests and exits
68
+    singleRun: false,
69
+
70
+    // Concurrency level
71
+    // how many browser should be started simultaneous
72
+    concurrency: Infinity
73
+  })
74
+}

+ 28
- 0
package.json View File

@@ -0,0 +1,28 @@
1
+{
2
+  "name": "luticate2-utils",
3
+  "version": "0.1.0",
4
+  "description": "Luticate front utils",
5
+  "main": "dist/luticate-utils.min.js",
6
+  "dependencies": {
7
+    "grunt": "~0.4.5",
8
+    "grunt-contrib-uglify": "~0.10.0"
9
+  },
10
+  "devDependencies": {
11
+    "angular": "^1.6.0",
12
+    "angular-mocks": "^1.6.0",
13
+    "angular-ui-router": "^0.3.2",
14
+    "jasmine-core": "^2.5.2",
15
+    "karma": "^1.3.0",
16
+    "karma-chrome-launcher": "^2.0.0",
17
+    "karma-jasmine": "^1.1.0"
18
+  },
19
+  "scripts": {
20
+    "test": "echo \"Error: no test specified\" && exit 1"
21
+  },
22
+  "repository": {
23
+    "type": "git",
24
+    "url": "https://git.rthoni.com/luticate2/front-utils"
25
+  },
26
+  "author": "",
27
+  "license": "MIT"
28
+}

+ 113
- 0
src/Business/lu-promise.js View File

@@ -0,0 +1,113 @@
1
+/**
2
+ * Created by robin on 10/24/15.
3
+ */
4
+
5
+(function () {
6
+    'use strict';
7
+    angular.module('luticate2Utils')
8
+        .factory('luPromise', [function() {
9
+
10
+            var luPromise = {};
11
+            luPromise.promises = {};
12
+            luPromise.loaders = [];
13
+            luPromise.errors = [];
14
+
15
+            luPromise.STATUS_RUNNING = 0;
16
+            luPromise.STATUS_RESOLVED = 1;
17
+            luPromise.STATUS_REJECTED = 2;
18
+
19
+            luPromise.build = function(array)
20
+            {
21
+                luPromise[array] = {};
22
+                for (var id in luPromise.promises) {
23
+                    if (luPromise.promises.hasOwnProperty(id)) {
24
+                        var promise = luPromise.promises[id];
25
+                        promise[array].forEach(function (group) {
26
+                            if (luPromise[array][group] == null) {
27
+                                luPromise[array][group] = [];
28
+                            }
29
+                            luPromise[array][group].push(promise);
30
+                        });
31
+                    }
32
+                }
33
+            };
34
+
35
+            luPromise.newPromise = function(id, groups, loaderGroups, errorGroups) {
36
+                return {
37
+                    id: id,
38
+                    groups: groups,
39
+                    loaderGroups: loaderGroups,
40
+                    errorGroups: errorGroups
41
+                };
42
+            };
43
+
44
+            luPromise.initPromise = function(data) {
45
+                var p = {
46
+                    loaders : [],
47
+                    errors: [],
48
+                    status: luPromise.STATUS_RUNNING,
49
+                    id: data.id
50
+                };
51
+
52
+                if (data.groups instanceof Array) {
53
+                    data.groups.forEach(function (group) {
54
+                        p.loaders.push(group);
55
+                        p.errors.push(group);
56
+                    });
57
+                }
58
+                else if (typeof data.groups === 'string') {
59
+                    p.loaders.push(data.groups);
60
+                    p.errors.push(data.groups);
61
+                }
62
+
63
+                if (data.loaderGroups instanceof Array) {
64
+                    data.loaderGroups.forEach(function (group) {
65
+                        p.loaders.push(group);
66
+                    });
67
+                }
68
+                else if (typeof data.loaderGroups === 'string') {
69
+                    p.loaders.push(data.loaderGroups);
70
+                }
71
+
72
+                if (data.errorGroups instanceof Array) {
73
+                    data.errorGroups.forEach(function (group) {
74
+                        p.errors.push(group);
75
+                    });
76
+                }
77
+                else if (typeof data.errorGroups === 'string') {
78
+                    p.errors.push(data.errorGroups);
79
+                }
80
+
81
+                luPromise.promises[data.id] = p;
82
+
83
+                luPromise.build("loaders");
84
+                luPromise.build("errors");
85
+            };
86
+
87
+            luPromise.getLoadersGroup = function(group) {
88
+                if (luPromise.loaders[group] == null) {
89
+                    return null;
90
+                }
91
+                return luPromise.loaders[group];
92
+            };
93
+
94
+            luPromise.getErrorsGroup = function(group) {
95
+                if (luPromise.errors[group] == null) {
96
+                    return null;
97
+                }
98
+                return luPromise.errors[group];
99
+            };
100
+
101
+            luPromise.reject = function(promise, error) {
102
+                luPromise.promises[promise.id].status = luPromise.STATUS_REJECTED;
103
+                luPromise.promises[promise.id].value = error;
104
+            };
105
+
106
+            luPromise.resolve = function(promise, data) {
107
+                luPromise.promises[promise.id].status = luPromise.STATUS_RESOLVED;
108
+                luPromise.promises[promise.id].value = data;
109
+            };
110
+
111
+            return luPromise;
112
+        }]);
113
+})();

+ 46
- 0
src/DataAccess/lu-dataaccess.js View File

@@ -0,0 +1,46 @@
1
+/**
2
+ * Created by robin on 12/11/16.
3
+ */
4
+
5
+(function () {
6
+    'use strict';
7
+    angular.module('luticate2Utils')
8
+        .factory('luDataAccess', ['$q', 'luRequest', function ($q, luRequest) {
9
+
10
+            var luDataAccess = {};
11
+
12
+            luDataAccess.create = function (entry_point) {
13
+                var da = {};
14
+
15
+                da.entry_point = entry_point;
16
+                if (da.entry_point.substr(-1) !== '/') {
17
+                    da.entry_point += '/';
18
+                }
19
+
20
+                da.get = function(url, dataGet, promise)
21
+                {
22
+                    return luRequest.get(da.entry_point + url, dataGet, promise);
23
+                };
24
+
25
+                da.post = function(url, dataGet, dataPost, promise)
26
+                {
27
+                    return luRequest.post(da.entry_point + url, dataGet, dataPost, promise);
28
+                };
29
+
30
+                da.put = function(url, dataGet, dataPost, promise)
31
+                {
32
+                    return luRequest.put(da.entry_point + url, dataGet, dataPost, promise);
33
+                };
34
+
35
+                da.delete = function(url, dataGet, dataPost, promise)
36
+                {
37
+                    return luRequest.delete(da.entry_point + url, dataGet, dataPost, promise);
38
+                };
39
+
40
+                return da;
41
+            };
42
+
43
+            return luDataAccess;
44
+
45
+        }]);
46
+})();

+ 95
- 0
src/DataAccess/lu-request.js View File

@@ -0,0 +1,95 @@
1
+/**
2
+ * Created by robin on 10/24/15.
3
+ */
4
+
5
+(function () {
6
+    'use strict';
7
+    angular.module('luticate2Utils')
8
+        .factory('luRequest', ['$q', '$http', 'luPromise', function ($q, $http, luPromise) {
9
+
10
+            var luRequest = {};
11
+
12
+            luRequest.get = function(url, dataGet, promise)
13
+            {
14
+                return luRequest.request(url, 'GET', dataGet, null, promise);
15
+            };
16
+
17
+            luRequest.post = function(url, dataGet, dataPost, promise)
18
+            {
19
+                return luRequest.request(url, 'POST', dataGet, dataPost, promise);
20
+            };
21
+
22
+            luRequest.put = function(url, dataGet, dataPost, promise)
23
+            {
24
+                return luRequest.request(url, 'PUT', dataGet, dataPost, promise);
25
+            };
26
+
27
+            luRequest.delete = function(url, dataGet, dataPost, promise)
28
+            {
29
+                return luRequest.request(url, 'DELETE', dataGet, dataPost, promise);
30
+            };
31
+
32
+            function reject(deferred, data, status, promise)
33
+            {
34
+                var obj = {
35
+                    Data: data,
36
+                    Status: status
37
+                };
38
+                deferred.reject(obj);
39
+                if (promise != null) {
40
+                    luPromise.reject(promise, obj);
41
+                }
42
+            }
43
+
44
+            function resolve(deferred, data, promise)
45
+            {
46
+                deferred.resolve(data);
47
+                if (promise != null) {
48
+                    luPromise.resolve(promise, data);
49
+                }
50
+            }
51
+
52
+            luRequest.request = function(url, method, dataGet, dataPost, promise)
53
+            {
54
+                if (promise != null) {
55
+                    luPromise.initPromise(promise);
56
+                }
57
+                var deferred = $q.defer();
58
+
59
+                url = url.replace(/\/+$/, '');
60
+
61
+                var params = {
62
+                    url: url,
63
+                    method: method,
64
+                    params: dataGet,
65
+                    data: dataPost == null ? null : $.param(dataPost),
66
+                    headers: {
67
+                        'Content-Type': "application/json"
68
+                    }
69
+                };
70
+
71
+                $http(params)
72
+                    .success(function (result, status) {
73
+                        if (result == null || result.Message != null) {
74
+                            var message = result == null ? "Failed to parse response" : result.Message;
75
+                            reject(deferred, message, status, promise);
76
+                        }
77
+                        else {
78
+                            resolve(deferred, result.Data, promise);
79
+                        }
80
+                    })
81
+                    .error(function (result, status) {
82
+                        if (result == null || result.Message != null) {
83
+                            var message = result == null ? "Failed to parse response" : result.Message;
84
+                            reject(deferred, message, status, promise);
85
+                        }
86
+                        else {
87
+                            reject(deferred, result, status, promise);
88
+                        }
89
+                    });
90
+                return deferred.promise;
91
+            };
92
+
93
+            return luRequest;
94
+        }]);
95
+})();

+ 7
- 0
src/luticate2-utils.js View File

@@ -0,0 +1,7 @@
1
+/**
2
+ * Created by robin on 10/25/15.
3
+ */
4
+
5
+(function() {
6
+    angular.module("luticate2Utils", []);
7
+})();

+ 137
- 0
tests/Business/lu-promise.spec.js View File

@@ -0,0 +1,137 @@
1
+/**
2
+ * Created by robin on 12/11/16.
3
+ */
4
+
5
+describe('lu-promise factory', function() {
6
+    var luPromise;
7
+
8
+    // Before each test load our api.users module
9
+    beforeEach(angular.mock.module('luticate2Utils'));
10
+
11
+    // Before each test set our injected Users factory (_Users_) to our local Users variable
12
+    beforeEach(inject(function(_luPromise_) {
13
+        luPromise = _luPromise_;
14
+    }));
15
+
16
+    it('should init a simple promise', function()
17
+    {
18
+        var p  = luPromise.newPromise('my-id', 'all-groups', 'loader-groups', 'error-groups');
19
+        luPromise.initPromise(p);
20
+        expect(Object.keys(luPromise.promises).length).toEqual(1);
21
+
22
+        var d = luPromise.promises['my-id'];
23
+        expect(d).not.toBe(null);
24
+        expect(d).not.toBe(undefined);
25
+        expect(d.loaders).toEqual(['all-groups', 'loader-groups']);
26
+        expect(d.errors).toEqual(['all-groups', 'error-groups']);
27
+        expect(d.status).toBe(luPromise.STATUS_RUNNING);
28
+        expect(d.id).toBe('my-id');
29
+
30
+        expect(Object.keys(luPromise.errors).length).toEqual(2);
31
+        expect(luPromise.errors['all-groups'].length).toEqual(1);
32
+        expect(luPromise.errors['all-groups'][0]['id']).toEqual('my-id');
33
+        expect(luPromise.errors['error-groups'].length).toEqual(1);
34
+        expect(luPromise.errors['error-groups'][0]['id']).toEqual('my-id');
35
+
36
+        expect(Object.keys(luPromise.loaders).length).toEqual(2);
37
+        expect(luPromise.loaders['all-groups'].length).toEqual(1);
38
+        expect(luPromise.loaders['all-groups'][0]['id']).toEqual('my-id');
39
+        expect(luPromise.loaders['loader-groups'].length).toEqual(1);
40
+        expect(luPromise.loaders['loader-groups'][0]['id']).toEqual('my-id');
41
+    });
42
+
43
+    it('should init a more complex promise 1', function()
44
+    {
45
+        var p  = luPromise.newPromise('my-id', ['all-groups', 'another-group'], 'loader-groups');
46
+        luPromise.initPromise(p);
47
+        expect(Object.keys(luPromise.promises).length).toEqual(1);
48
+
49
+        var d = luPromise.promises['my-id'];
50
+        expect(d).not.toBe(null);
51
+        expect(d).not.toBe(undefined);
52
+        expect(d.loaders).toEqual(['all-groups', 'another-group', 'loader-groups']);
53
+        expect(d.errors).toEqual(['all-groups', 'another-group']);
54
+        expect(d.status).toBe(luPromise.STATUS_RUNNING);
55
+        expect(d.id).toBe('my-id');
56
+
57
+        expect(Object.keys(luPromise.errors).length).toEqual(2);
58
+        expect(luPromise.errors['all-groups'].length).toEqual(1);
59
+        expect(luPromise.errors['all-groups'][0]['id']).toEqual('my-id');
60
+        expect(luPromise.errors['another-group'].length).toEqual(1);
61
+        expect(luPromise.errors['another-group'][0]['id']).toEqual('my-id');
62
+
63
+        expect(Object.keys(luPromise.loaders).length).toEqual(3);
64
+        expect(luPromise.loaders['all-groups'].length).toEqual(1);
65
+        expect(luPromise.loaders['all-groups'][0]['id']).toEqual('my-id');
66
+        expect(luPromise.loaders['another-group'].length).toEqual(1);
67
+        expect(luPromise.loaders['another-group'][0]['id']).toEqual('my-id');
68
+        expect(luPromise.loaders['loader-groups'].length).toEqual(1);
69
+        expect(luPromise.loaders['loader-groups'][0]['id']).toEqual('my-id');
70
+    });
71
+
72
+    it('should init a more complex promise 2', function()
73
+    {
74
+        var p  = luPromise.newPromise('my-id', null, 'loader-groups');
75
+        luPromise.initPromise(p);
76
+        expect(Object.keys(luPromise.promises).length).toEqual(1);
77
+
78
+        var d = luPromise.promises['my-id'];
79
+        expect(d).not.toBe(null);
80
+        expect(d).not.toBe(undefined);
81
+        expect(d.loaders).toEqual(['loader-groups']);
82
+        expect(d.errors).toEqual([]);
83
+        expect(d.status).toBe(luPromise.STATUS_RUNNING);
84
+        expect(d.id).toBe('my-id');
85
+
86
+        expect(Object.keys(luPromise.errors).length).toEqual(0);
87
+
88
+        expect(Object.keys(luPromise.loaders).length).toEqual(1);
89
+        expect(luPromise.loaders['loader-groups'].length).toEqual(1);
90
+        expect(luPromise.loaders['loader-groups'][0]['id']).toEqual('my-id');
91
+    });
92
+
93
+    it('should init multiple more complex promise 1', function()
94
+    {
95
+        var p1  = luPromise.newPromise('my-id', ['all-groups', 'another-group'], 'loader-groups');
96
+        luPromise.initPromise(p1);
97
+        var p2  = luPromise.newPromise('my-id2', ['all-groups', 'another-group'], null, 'an-error-group');
98
+        luPromise.initPromise(p2);
99
+        expect(Object.keys(luPromise.promises).length).toEqual(2);
100
+
101
+        var d = luPromise.promises['my-id'];
102
+        expect(d).not.toBe(null);
103
+        expect(d).not.toBe(undefined);
104
+        expect(d.loaders).toEqual(['all-groups', 'another-group', 'loader-groups']);
105
+        expect(d.errors).toEqual(['all-groups', 'another-group']);
106
+        expect(d.status).toBe(luPromise.STATUS_RUNNING);
107
+        expect(d.id).toBe('my-id');
108
+
109
+        d = luPromise.promises['my-id2'];
110
+        expect(d).not.toBe(null);
111
+        expect(d).not.toBe(undefined);
112
+        expect(d.loaders).toEqual(['all-groups', 'another-group']);
113
+        expect(d.errors).toEqual(['all-groups', 'another-group', 'an-error-group']);
114
+        expect(d.status).toBe(luPromise.STATUS_RUNNING);
115
+        expect(d.id).toBe('my-id2');
116
+
117
+        expect(Object.keys(luPromise.errors).length).toEqual(3);
118
+        expect(luPromise.errors['all-groups'].length).toEqual(2);
119
+        expect(luPromise.errors['all-groups'][0]['id']).toEqual('my-id');
120
+        expect(luPromise.errors['all-groups'][1]['id']).toEqual('my-id2');
121
+        expect(luPromise.errors['another-group'].length).toEqual(2);
122
+        expect(luPromise.errors['another-group'][0]['id']).toEqual('my-id');
123
+        expect(luPromise.errors['another-group'][1]['id']).toEqual('my-id2');
124
+        expect(luPromise.errors['an-error-group'].length).toEqual(1);
125
+        expect(luPromise.errors['an-error-group'][0]['id']).toEqual('my-id2');
126
+
127
+        expect(Object.keys(luPromise.loaders).length).toEqual(3);
128
+        expect(luPromise.loaders['all-groups'].length).toEqual(2);
129
+        expect(luPromise.loaders['all-groups'][0]['id']).toEqual('my-id');
130
+        expect(luPromise.loaders['all-groups'][1]['id']).toEqual('my-id2');
131
+        expect(luPromise.loaders['another-group'].length).toEqual(2);
132
+        expect(luPromise.loaders['another-group'][0]['id']).toEqual('my-id');
133
+        expect(luPromise.loaders['another-group'][1]['id']).toEqual('my-id2');
134
+        expect(luPromise.loaders['loader-groups'].length).toEqual(1);
135
+        expect(luPromise.loaders['loader-groups'][0]['id']).toEqual('my-id');
136
+    });
137
+});

Loading…
Cancel
Save