Просмотр исходного кода

added default object to extend webservice data; tests

tags/v0.1.1
Robin Thoni 8 лет назад
Родитель
Сommit
a3bbbb539b

+ 1
- 1
dist/luticate-utils.min.js
Разница между файлами не показана из-за своего большого размера
Просмотреть файл


+ 62
- 6
src/Business/lu-webapi-crud-business.js Просмотреть файл

@@ -5,7 +5,7 @@
5 5
 (function () {
6 6
     'use strict';
7 7
     angular.module('luticate2Utils')
8
-        .factory('luWebApiCrudBusiness', ['luBusiness', 'luWebApiDataAccess', function (luBusiness, luWebApiDataAccess) {
8
+        .factory('luWebApiCrudBusiness', ['luBusiness', '$q', function (luBusiness, $q) {
9 9
 
10 10
             var luWebApiCrudBusiness = {};
11 11
 
@@ -14,24 +14,80 @@
14 14
 
15 15
                 Business.dataAccess = dataAccess;
16 16
 
17
+                Business.defaultDbo = {
18
+                    id: null
19
+                };
20
+
21
+                Business.extendDeep = function extendDeep(dst) {
22
+                    angular.forEach(arguments, function(obj) {
23
+                        if (obj !== dst) {
24
+                            angular.forEach(obj, function(value, key) {
25
+                                if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
26
+                                    extendDeep(dst[key], value);
27
+                                } else {
28
+                                    dst[key] = value;
29
+                                }
30
+                            });
31
+                        }
32
+                    });
33
+                    return dst;
34
+                };
35
+
36
+                Business.initDbo = function(dbo)
37
+                {
38
+                    return Business.extendDeep({}, Business.defaultDbo, dbo);
39
+                };
40
+
41
+                Business.initPaginatedDbo = function(data)
42
+                {
43
+                    var d = {
44
+                        count: data.count,
45
+                        data: []
46
+                    };
47
+                    for (var i = 0; i < data.data.length; ++i) {
48
+                        d.data.push(Business.initDbo(data.data[i]));
49
+                    }
50
+                    return d;
51
+                };
52
+
53
+                Business.thenInitDbo = function(promise)
54
+                {
55
+                    var deferred = $q.defer();
56
+                    promise.then(function(data)
57
+                    {
58
+                        deferred.resolve(Business.initDbo(data));
59
+                    }, deferred.reject);
60
+                    return deferred.promise;
61
+                };
62
+
63
+                Business.thenInitPaginatedDbo = function(promise)
64
+                {
65
+                    var deferred = $q.defer();
66
+                    promise.then(function(data)
67
+                    {
68
+                        deferred.resolve(Business.initPaginatedDbo(data));
69
+                    }, deferred.reject);
70
+                    return deferred.promise;
71
+                };
72
+
17 73
                 Business.getSingleById = function (id, luBusyGroups) {
18
-                    return Business.dataAccess.getSingleById(id, luBusyGroups);
74
+                    return Business.thenInitDbo(Business.dataAccess.getSingleById(id, luBusyGroups));
19 75
                 };
20 76
 
21 77
                 Business.getMultiple = function (orderBy, filter, page, perPage, luBusyGroups) {
22
-                    return Business.dataAccess.getMultiple(orderBy, filter, page, perPage, luBusyGroups);
78
+                    return Business.thenInitPaginatedDbo(Business.dataAccess.getMultiple(orderBy, filter, page, perPage, luBusyGroups));
23 79
                 };
24 80
 
25 81
                 Business.addDbo = function(data, luBusyGroups) {
26
-                    return Business.dataAccess.addDbo(data, luBusyGroups);
82
+                    return Business.thenInitDbo(Business.dataAccess.addDbo(data, luBusyGroups));
27 83
                 };
28 84
 
29 85
                 Business.editSingleByIdDbo = function(id, data, luBusyGroups) {
30
-                    return Business.dataAccess.editSingleByIdDbo(id, data, luBusyGroups);
86
+                    return Business.thenInitDbo(Business.dataAccess.editSingleByIdDbo(id, data, luBusyGroups));
31 87
                 };
32 88
 
33 89
                 Business.deleteDbo = function(id, luBusyGroups) {
34
-                    return Business.dataAccess.deleteDbo(id, luBusyGroups);
90
+                    return Business.thenInitDbo(Business.dataAccess.deleteDbo(id, luBusyGroups));
35 91
                 };
36 92
 
37 93
                 return Business;

+ 229
- 0
tests/Business/lu-webapi-crud-business.spec.js Просмотреть файл

@@ -0,0 +1,229 @@
1
+/**
2
+ * Created by robin on 12/11/16.
3
+ */
4
+
5
+describe('lu-webapi-crud-business factory', function() {
6
+    var luWebApiCrudBusiness;
7
+    var fakeDataAccess;
8
+    var $q;
9
+    var $rootScope;
10
+
11
+    // Before each test load our api.users module
12
+    beforeEach(angular.mock.module('luticate2Utils'));
13
+
14
+    // Before each test set our injected Users factory (_Users_) to our local Users variable
15
+    beforeEach(inject(function(_luWebApiCrudBusiness_, _$q_, _$rootScope_) {
16
+        luWebApiCrudBusiness = _luWebApiCrudBusiness_;
17
+        $q = _$q_;
18
+        $rootScope = _$rootScope_;
19
+    }));
20
+
21
+    beforeEach(function () {
22
+        fakeDataAccess = {
23
+            getSingleById: function(id, luBusyGroups) {
24
+                var deferred = $q.defer();
25
+                if (id == 1) {
26
+                    deferred.resolve({
27
+                        id: id,
28
+                        name: "Test."
29
+                    });
30
+                }
31
+                else if (id == 2) {
32
+                    deferred.resolve({
33
+                        id: id,
34
+                        name: "Test.2",
35
+                        obj: {
36
+                            value: 4242
37
+                        }
38
+                    });
39
+                }
40
+                return deferred.promise;
41
+            },
42
+            getMultiple: function (orderBy, filter, page, perPage, luBusyGroups) {
43
+                var deferred = $q.defer();
44
+                deferred.resolve({
45
+                    count: 42,
46
+                    data: [{
47
+                            id: 1,
48
+                            name: "Test.",
49
+                            obj: {
50
+                                value: 4242
51
+                            }
52
+                        }, {
53
+                            id: 2,
54
+                            name: "Test.2",
55
+                            obj: {
56
+                                anotherValue: 2424
57
+                            }
58
+                        }]
59
+                });
60
+                return deferred.promise;
61
+            },
62
+            addDbo: function(data, luBusyGroups) {
63
+                return this.getSingleById(1, luBusyGroups);
64
+            },
65
+            editSingleByIdDbo: function(id, data, luBusyGroups) {
66
+                return this.getSingleById(id, luBusyGroups);
67
+            },
68
+            deleteDbo: function(id, luBusyGroups) {
69
+                return this.getSingleById(id, luBusyGroups);
70
+            }
71
+        };
72
+    });
73
+
74
+
75
+    it('should add a dummy function to getSingleById dbo', function()
76
+    {
77
+        var business = luWebApiCrudBusiness.create(fakeDataAccess);
78
+        business.defaultDbo = {
79
+            id: null,
80
+            name: "",
81
+            dummyFunction: function () {
82
+                return '_' + this.name + '_';
83
+            }
84
+        };
85
+
86
+        var id = 1;
87
+        business.getSingleById(id).then(function (data) {
88
+            expect(data.id).toEqual(id);
89
+            expect(data.name).toEqual("Test.");
90
+            expect(data.dummyFunction).not.toBeNull();
91
+            expect(data.dummyFunction()).toEqual("_Test._");
92
+        }, function (error) {
93
+            expect(error).toBeNull();
94
+        });
95
+        $rootScope.$digest();
96
+    });
97
+
98
+
99
+    it('should test complex extend', function()
100
+    {
101
+        var business = luWebApiCrudBusiness.create(fakeDataAccess);
102
+        business.defaultDbo = {
103
+            id: null,
104
+            name: "",
105
+            obj: {
106
+                value: 42,
107
+                anotherValue: 24
108
+            }
109
+        };
110
+
111
+        var id = 2;
112
+        business.getSingleById(id).then(function (data) {
113
+            expect(data.id).toEqual(id);
114
+            expect(data.name).toEqual("Test.2");
115
+            expect(data.obj).not.toBeNull();
116
+            expect(data.obj.value).toEqual(4242);
117
+            expect(data.obj.anotherValue).toEqual(24);
118
+        }, function (error) {
119
+            expect(error).toBeNull();
120
+        });
121
+        $rootScope.$digest();
122
+    });
123
+
124
+
125
+    it('should add a dummy function to getSingleById dbo', function()
126
+    {
127
+        var business = luWebApiCrudBusiness.create(fakeDataAccess);
128
+        business.defaultDbo = {
129
+            id: null,
130
+            name: "",
131
+            dummyFunction: function () {
132
+                return '_' + this.name + '_';
133
+            }
134
+        };
135
+
136
+        business.getMultiple(null, null, 0, 2).then(function (data) {
137
+            expect(data.count).toEqual(42);
138
+            expect(data.data.length).toEqual(2);
139
+
140
+            var i = 0;
141
+            expect(data.data[i].id).toEqual(1);
142
+            expect(data.data[i].name).toEqual("Test.");
143
+            expect(data.data[i].dummyFunction).not.toBeNull();
144
+            expect(data.data[i].dummyFunction()).toEqual("_Test._");
145
+
146
+            i = 1;
147
+            expect(data.data[i].id).toEqual(2);
148
+            expect(data.data[i].name).toEqual("Test.2");
149
+            expect(data.data[i].dummyFunction).not.toBeNull();
150
+            expect(data.data[i].dummyFunction()).toEqual("_Test.2_");
151
+
152
+        }, function (error) {
153
+            expect(error).toBeNull();
154
+        });
155
+        $rootScope.$digest();
156
+    });
157
+
158
+
159
+    it('should add a dummy function to addDbo dbo', function()
160
+    {
161
+        var business = luWebApiCrudBusiness.create(fakeDataAccess);
162
+        business.defaultDbo = {
163
+            id: null,
164
+            name: "",
165
+            dummyFunction: function () {
166
+                return '_' + this.name + '_';
167
+            }
168
+        };
169
+
170
+        var id = 1;
171
+        business.addDbo(null).then(function (data) {
172
+            expect(data.id).toEqual(id);
173
+            expect(data.name).toEqual("Test.");
174
+            expect(data.dummyFunction).not.toBeNull();
175
+            expect(data.dummyFunction()).toEqual("_Test._");
176
+        }, function (error) {
177
+            expect(error).toBeNull();
178
+        });
179
+        $rootScope.$digest();
180
+    });
181
+
182
+
183
+    it('should add a dummy function to editSingleByIdDbo dbo', function()
184
+    {
185
+        var business = luWebApiCrudBusiness.create(fakeDataAccess);
186
+        business.defaultDbo = {
187
+            id: null,
188
+            name: "",
189
+            dummyFunction: function () {
190
+                return '_' + this.name + '_';
191
+            }
192
+        };
193
+
194
+        var id = 1;
195
+        business.editSingleByIdDbo(id, null).then(function (data) {
196
+            expect(data.id).toEqual(id);
197
+            expect(data.name).toEqual("Test.");
198
+            expect(data.dummyFunction).not.toBeNull();
199
+            expect(data.dummyFunction()).toEqual("_Test._");
200
+        }, function (error) {
201
+            expect(error).toBeNull();
202
+        });
203
+        $rootScope.$digest();
204
+    });
205
+
206
+
207
+    it('should add a dummy function to deleteDbo dbo', function()
208
+    {
209
+        var business = luWebApiCrudBusiness.create(fakeDataAccess);
210
+        business.defaultDbo = {
211
+            id: null,
212
+            name: "",
213
+            dummyFunction: function () {
214
+                return '_' + this.name + '_';
215
+            }
216
+        };
217
+
218
+        var id = 1;
219
+        business.deleteDbo(id, null).then(function (data) {
220
+            expect(data.id).toEqual(id);
221
+            expect(data.name).toEqual("Test.");
222
+            expect(data.dummyFunction).not.toBeNull();
223
+            expect(data.dummyFunction()).toEqual("_Test._");
224
+        }, function (error) {
225
+            expect(error).toBeNull();
226
+        });
227
+        $rootScope.$digest();
228
+    });
229
+});

Загрузка…
Отмена
Сохранить