PageRenderTime 31ms CodeModel.GetById 13ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/extjs/src/core/test/unit/spec/lang/Error.js

https://bitbucket.org/nkusumah/indoquran
JavaScript | 277 lines | 245 code | 30 blank | 2 comment | 0 complexity | 628ebf25d9ef16fe6ccc309dffc8da16 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    
202            Ext.define('CustomClass', {
203                doSomething: function(o){
204                    Ext.Error.raise({
205                        msg: 'Custom error',
206                        data: o,
207                        foo: 'bar'
208                    });
209                }
210            });
211            var customObj = Ext.create('CustomClass');
212        
213            it("should throw an error containing the source class and method", function() {
214                var error;
215                try {
216                    customObj.doSomething({
217                        extraData: 'extra'
218                    });
219                }
220                catch (err) {
221                    error = err;
222                }
223                expect(error.msg).toEqual('Custom error');
224                expect(error.sourceClass).toEqual('CustomClass');
225                expect(error.sourceMethod).toEqual('doSomething');
226                expect(error.toString()).toBe('CustomClass.doSomething(): Custom error');
227            });
228        
229            it("should log the complete metadata to the console", function() {
230                spyOn(Ext.global.console, 'dir').andCallFake(function(err){
231                    expect(err.msg).toEqual('Custom error');
232                    expect(err.sourceClass).toEqual('CustomClass');
233                    expect(err.sourceMethod).toEqual('doSomething');
234                    expect(err.data).not.toBe(null);
235                    expect(err.data.extraData).not.toBe(null);
236                    expect(err.data.extraData).toEqual('extra');
237                    expect(err.foo).toEqual('bar');
238                });
239                try {
240                    customObj.doSomething({
241                        extraData: 'extra'
242                    });
243                } 
244                catch (err) {
245                }
246            });
247        });
248    });
249    
250    describe("Throwing an an Ext.Error directly intantiated", function() {
251        describe("Passing an string as constructor argument", function() {
252           it("should contain a msg property with the given string as value", function() {
253              expect(function() {
254                  throw new Ext.Error("expected message");
255              }).toRaiseExtError("expected message");
256           });
257        });
258     });
259    
260    xdescribe("Ext.deprecated", function() {
261       // failing only on CI
262       it("should return a function that raises an error with the given suggestion", function() {
263          Ext.ClassManager.enableNamespaceParseCache = false;
264          Ext.define("Test.ThisClassContainsADeprecatedMethod", {
265             deprecatedMethod : Ext.deprecated("use another function")
266          });
267          expect(function() {
268              new Test.ThisClassContainsADeprecatedMethod().deprecatedMethod();
269          }).toThrow('The method "Test.ThisClassContainsADeprecatedMethod.deprecatedMethod" has been removed. use another function');
270          
271          try {
272              delete Test;
273          } catch(e) { }
274          Ext.ClassManager.enableNamespaceParseCache = true;
275       });
276    });
277});