PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/web-app/client/ext/packages/sencha-core/test/specs/lang/Error.js

https://github.com/mxrguspxrt/klassifikaator
JavaScript | 282 lines | 249 code | 31 blank | 2 comment | 0 complexity | bdc07e39793c4a6ad9e0bbd792144197 MD5 | raw file
  1describe("Ext.Error", function() { 
  2    var global;
  3
  4    beforeEach(function() {
  5        global = Ext.global;
  6
  7        // mock the console to avoid logging to the real console during the tests
  8        Ext.global = {
  9            console: {
 10                dir: function(s) {
 11                    return s;
 12                },
 13                log: function(s) {
 14                    return s;
 15                },                
 16                error: function(s) {
 17                    return s;
 18                },
 19                warn: function(s) {
 20                    return s;
 21                }
 22            }
 23        };
 24    });
 25
 26    afterEach(function() {
 27        Ext.global = global;
 28    });
 29
 30    describe("raising an error via Ext.Error.raise", function() {
 31
 32        describe("passing a string", function() {
 33    
 34            it("should throw an error with a msg property", function() {
 35                var error;
 36                try {
 37                    Ext.Error.raise('foo');
 38                }
 39                catch (err) {
 40                    error = err;
 41                }
 42                expect(error.msg).toEqual('foo');
 43            });
 44        
 45            it("should log an error to the console", function() {
 46                spyOn(Ext.global.console, 'error');
 47                try {
 48                    Ext.Error.raise('foo');
 49                } 
 50                catch (err) {}
 51                expect(Ext.global.console.error).toHaveBeenCalledWith('[E] foo');
 52            });
 53        
 54            it("should log the error object to the console", function() {
 55                spyOn(Ext.global.console, 'dir').andCallFake(function(err){
 56                    expect(err.msg).toEqual('foo');
 57                });
 58                try {
 59                    Ext.Error.raise('foo');
 60                } 
 61                catch (err) {}
 62            });
 63        
 64            it("should do nothing when Ext.Error.ignore = true", function() {
 65                spyOn(Ext.global.console, 'warn');
 66            
 67                Ext.Error.ignore = true;
 68                try {
 69                    Ext.Error.raise('foo');
 70                } 
 71                catch (err) {
 72                    expect('Error should not have been caught').toBe(true);
 73                }
 74                expect(Ext.global.console.warn).not.toHaveBeenCalled();
 75                Ext.Error.ignore = false;
 76            });
 77        
 78            it("should not throw an error if handled by Ext.Error.handle", function() {
 79                spyOn(Ext.global.console, 'warn');
 80            
 81                var origHandle = Ext.Error.handle;
 82                Ext.Error.handle = function(err) {
 83                    expect(err.msg).toEqual('foo');
 84                    return true;
 85                }
 86                try {
 87                    Ext.Error.raise('foo');
 88                } 
 89                catch (err) {
 90                    expect('Error should not have been caught').toBe(true);
 91                }
 92                expect(Ext.global.console.warn).not.toHaveBeenCalled();
 93                Ext.Error.handle = origHandle;
 94            });
 95        });
 96    
 97        describe("passing an object with a msg property", function() {
 98    
 99            it("should throw an error with a msg property", function() {
100                var error;
101                try {
102                    Ext.Error.raise({msg: 'foo'});
103                }
104                catch (err) {
105                    error = err;
106                }
107                expect(error.msg).toEqual('foo');
108            });
109        
110            it("should log an error to the console", function() {
111                spyOn(Ext.global.console, 'error');
112                try {
113                    Ext.Error.raise({msg: 'foo'});
114                } 
115                catch (err) {}
116                expect(Ext.global.console.error).toHaveBeenCalledWith('[E] foo');
117            });
118        
119            it("should log the error object to the console", function() {
120                spyOn(Ext.global.console, 'dir').andCallFake(function(err){
121                    expect(err.msg).toEqual('foo');
122                });
123                try {
124                    Ext.Error.raise({msg: 'foo'});
125                } 
126                catch (err) {}
127            });
128                            
129            it("should do nothing when Ext.Error.ignore = true", function() {
130                spyOn(Ext.global.console, 'warn');
131            
132                Ext.Error.ignore = true;
133                try {
134                    Ext.Error.raise({msg: 'foo'});
135                } 
136                catch (err) {
137                    expect('Error should not have been caught').toBe(true);
138                }
139                expect(Ext.global.console.warn).not.toHaveBeenCalled();
140                Ext.Error.ignore = false;
141            });
142        
143            it("should not throw an error if handled by Ext.Error.handle", function() {
144                spyOn(Ext.global.console, 'warn');
145            
146                var origHandle = Ext.Error.handle;
147                Ext.Error.handle = function(err) {
148                    expect(err.msg).toEqual('foo');
149                    return true;
150                }
151                try {
152                    Ext.Error.raise({msg: 'foo'});
153                } 
154                catch (err) {
155                    expect('Error should not have been caught').toBe(true);
156                }
157                expect(Ext.global.console.warn).not.toHaveBeenCalled();
158                Ext.Error.handle = origHandle;
159            });
160        });
161    
162        describe("passing an object with custom metadata", function() {
163    
164            it("should throw an error with matching metadata", function() {
165                var error;
166                try {
167                    Ext.Error.raise({
168                        msg: 'Custom error',
169                        data: {
170                            foo: 'bar'
171                        }
172                    });
173                }
174                catch (err) {
175                    error = err;
176                }
177                expect(error.msg).toEqual('Custom error');
178                expect(error.data).not.toBe(null);
179                expect(error.data.foo).toEqual('bar');
180            });
181        
182            it("should log the complete metadata to the console", function() {
183                spyOn(Ext.global.console, 'dir').andCallFake(function(err){
184                    expect(err.msg).toEqual('Custom error');
185                    expect(err.data).not.toBe(null);
186                    expect(err.data.foo).toEqual('bar');
187                });
188                try {
189                    Ext.Error.raise({
190                        msg: 'Custom error',
191                        data: {
192                            foo: 'bar'
193                        }
194                    });
195                } 
196                catch (err) {}
197            });
198        });
199    
200        describe("originating from within a class defined by Ext", function() {
201            var customObj;
202    
203            beforeEach(function() {
204                Ext.define('spec.CustomClass', {
205                    doSomething: function(o){
206                        Ext.Error.raise({
207                            msg: 'Custom error',
208                            data: o,
209                            foo: 'bar'
210                        });
211                    }
212                });
213                customObj = Ext.create('spec.CustomClass');
214            });
215
216            afterEach(function() {
217                Ext.undefine('spec.CustomClass');
218            });
219        
220            it("should throw an error containing the source class and method", function() {
221                var error;
222                try {
223                    customObj.doSomething({
224                        extraData: 'extra'
225                    });
226                }
227                catch (err) {
228                    error = err;
229                }
230                expect(error.msg).toEqual('Custom error');
231                expect(error.sourceClass).toEqual('spec.CustomClass');
232                expect(error.sourceMethod).toEqual('doSomething');
233                expect(error.toString()).toBe('spec.CustomClass.doSomething(): Custom error');
234            });
235        
236            it("should log the complete metadata to the console", function() {
237                spyOn(Ext.global.console, 'dir').andCallFake(function(err){
238                    expect(err.msg).toEqual('Custom error');
239                    expect(err.sourceClass).toEqual('spec.CustomClass');
240                    expect(err.sourceMethod).toEqual('doSomething');
241                    expect(err.data).not.toBe(null);
242                    expect(err.data.extraData).not.toBe(null);
243                    expect(err.data.extraData).toEqual('extra');
244                    expect(err.foo).toEqual('bar');
245                });
246                try {
247                    customObj.doSomething({
248                        extraData: 'extra'
249                    });
250                } 
251                catch (err) {
252                }
253            });
254        });
255    });
256    
257    describe("Throwing an an Ext.Error directly intantiated", function() {
258        describe("Passing an string as constructor argument", function() {
259           it("should contain a msg property with the given string as value", function() {
260              expect(function() {
261                  throw new Ext.Error("expected message");
262              }).toRaiseExtError("expected message");
263           });
264        });
265     });
266    
267    xdescribe("Ext.deprecated", function() {
268       // failing only on CI
269       it("should return a function that raises an error with the given suggestion", function() {
270          Ext.ClassManager.enableNamespaceParseCache = false;
271          Ext.define("spec.MyClass", {
272             deprecatedMethod : Ext.deprecated("use another function")
273          });
274          expect(function() {
275              new spec.ThisClassContainsADeprecatedMethod().deprecatedMethod();
276          }).toThrow('The method "spec.MyClass.deprecatedMethod" has been removed. use another function');
277          
278          Ext.undefine('spec.MyClass');
279          Ext.ClassManager.enableNamespaceParseCache = true;
280       });
281    });
282});