PageRenderTime 93ms CodeModel.GetById 53ms app.highlight 36ms RepoModel.GetById 1ms app.codeStats 0ms

/Framework/ext-4.0.7/src/core/test/unit/spec/lang/Error.js

https://bitbucket.org/gtong/javascripts
JavaScript | 251 lines | 208 code | 28 blank | 15 comment | 0 complexity | 6387265b96cf69f81b7830d2a5729988 MD5 | raw file
  1/*
  2
  3This file is part of Ext JS 4
  4
  5Copyright (c) 2011 Sencha Inc
  6
  7Contact:  http://www.sencha.com/contact
  8
  9Commercial Usage
 10Licensees holding valid commercial licenses may use this file in accordance with the Commercial Software License Agreement provided with the Software or, alternatively, in accordance with the terms contained in a written agreement between you and Sencha.
 11
 12If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
 13
 14*/
 15describe("Ext.Error", function() { 
 16    var global;
 17
 18    beforeEach(function() {
 19        global = Ext.global;
 20
 21        // mock the console to avoid logging to the real console during the tests
 22        Ext.global = {
 23            console: {
 24                dir: function(s) {
 25                    return s;
 26                },
 27                error: function(s) {
 28                    return s;
 29                },
 30                warn: function(s) {
 31                    return s;
 32                }
 33            }
 34        };
 35    });
 36
 37    afterEach(function() {
 38        Ext.global = global;
 39    });
 40
 41    describe("raising an error via Ext.Error.raise", function() {
 42
 43        describe("passing a string", function() {
 44    
 45            it("should throw an error with a msg property", function() {
 46                try {
 47                    Ext.Error.raise('foo');
 48                }
 49                catch (err) {
 50                    expect(err.msg).toEqual('foo');
 51                }
 52            });
 53        
 54            it("should log an error to the console", function() {
 55                spyOn(Ext.global.console, 'error');
 56                try {
 57                    Ext.Error.raise('foo');
 58                } 
 59                catch (err) {}
 60                expect(Ext.global.console.error).toHaveBeenCalledWith('[E] foo');
 61            });
 62        
 63            it("should log the error object to the console", function() {
 64                spyOn(Ext.global.console, 'dir').andCallFake(function(err){
 65                    expect(err.msg).toEqual('foo');
 66                });
 67                try {
 68                    Ext.Error.raise('foo');
 69                } 
 70                catch (err) {}
 71            });
 72        
 73            it("should do nothing when Ext.Error.ignore = true", function() {
 74                spyOn(Ext.global.console, 'warn');
 75            
 76                Ext.Error.ignore = true;
 77                try {
 78                    Ext.Error.raise('foo');
 79                } 
 80                catch (err) {
 81                    expect('Error should not have been caught').toBe(true);
 82                }
 83                expect(Ext.global.console.warn).not.toHaveBeenCalled();
 84                Ext.Error.ignore = false;
 85            });
 86        
 87            it("should not throw an error if handled by Ext.Error.handle", function() {
 88                spyOn(Ext.global.console, 'warn');
 89            
 90                var origHandle = Ext.Error.handle;
 91                Ext.Error.handle = function(err) {
 92                    expect(err.msg).toEqual('foo');
 93                    return true;
 94                }
 95                try {
 96                    Ext.Error.raise('foo');
 97                } 
 98                catch (err) {
 99                    expect('Error should not have been caught').toBe(true);
100                }
101                expect(Ext.global.console.warn).not.toHaveBeenCalled();
102                Ext.Error.handle = origHandle;
103            });
104        });
105    
106        describe("passing an object with a msg property", function() {
107    
108            it("should throw an error with a msg property", function() {
109                try {
110                    Ext.Error.raise({msg: 'foo'});
111                }
112                catch (err) {
113                    expect(err.msg).toEqual('foo');
114                }
115            });
116        
117            it("should log an error to the console", function() {
118                spyOn(Ext.global.console, 'error');
119                try {
120                    Ext.Error.raise({msg: 'foo'});
121                } 
122                catch (err) {}
123                expect(Ext.global.console.error).toHaveBeenCalledWith('[E] foo');
124            });
125        
126            it("should log the error object to the console", function() {
127                spyOn(Ext.global.console, 'dir').andCallFake(function(err){
128                    expect(err.msg).toEqual('foo');
129                });
130                try {
131                    Ext.Error.raise({msg: 'foo'});
132                } 
133                catch (err) {}
134            });
135                            
136            it("should do nothing when Ext.Error.ignore = true", function() {
137                spyOn(Ext.global.console, 'warn');
138            
139                Ext.Error.ignore = true;
140                try {
141                    Ext.Error.raise({msg: 'foo'});
142                } 
143                catch (err) {
144                    expect('Error should not have been caught').toBe(true);
145                }
146                expect(Ext.global.console.warn).not.toHaveBeenCalled();
147                Ext.Error.ignore = false;
148            });
149        
150            it("should not throw an error if handled by Ext.Error.handle", function() {
151                spyOn(Ext.global.console, 'warn');
152            
153                var origHandle = Ext.Error.handle;
154                Ext.Error.handle = function(err) {
155                    expect(err.msg).toEqual('foo');
156                    return true;
157                }
158                try {
159                    Ext.Error.raise({msg: 'foo'});
160                } 
161                catch (err) {
162                    expect('Error should not have been caught').toBe(true);
163                }
164                expect(Ext.global.console.warn).not.toHaveBeenCalled();
165                Ext.Error.handle = origHandle;
166            });
167        });
168    
169        describe("passing an object with custom metadata", function() {
170    
171            it("should throw an error with matching metadata", function() {
172                try {
173                    Ext.Error.raise({
174                        msg: 'Custom error',
175                        data: {
176                            foo: 'bar'
177                        }
178                    });
179                }
180                catch (err) {
181                    expect(err.msg).toEqual('Custom error');
182                    expect(err.data).not.toBe(null);
183                    expect(err.data.foo).toEqual('bar');
184                }
185            });
186        
187            it("should log the complete metadata to the console", function() {
188                spyOn(Ext.global.console, 'dir').andCallFake(function(err){
189                    expect(err.msg).toEqual('Custom error');
190                    expect(err.data).not.toBe(null);
191                    expect(err.data.foo).toEqual('bar');
192                });
193                try {
194                    Ext.Error.raise({
195                        msg: 'Custom error',
196                        data: {
197                            foo: 'bar'
198                        }
199                    });
200                } 
201                catch (err) {}
202            });
203        });
204    
205        describe("originating from within a class defined by Ext", function() {
206    
207            Ext.define('CustomClass', {
208                doSomething: function(o){
209                    Ext.Error.raise({
210                        msg: 'Custom error',
211                        data: o,
212                        foo: 'bar'
213                    });
214                }
215            });
216            var customObj = Ext.create('CustomClass');
217        
218            it("should throw an error containing the source class and method", function() {
219                try {
220                    customObj.doSomething({
221                        extraData: 'extra'
222                    });
223                }
224                catch (err) {
225                    expect(err.msg).toEqual('Custom error');
226                    expect(err.sourceClass).toEqual('CustomClass');
227                    expect(err.sourceMethod).toEqual('doSomething');
228                }
229            });
230        
231            it("should log the complete metadata to the console", function() {
232                spyOn(Ext.global.console, 'dir').andCallFake(function(err){
233                    expect(err.msg).toEqual('Custom error');
234                    expect(err.sourceClass).toEqual('CustomClass');
235                    expect(err.sourceMethod).toEqual('doSomething');
236                    expect(err.data).not.toBe(null);
237                    expect(err.data.extraData).not.toBe(null);
238                    expect(err.data.extraData).toEqual('extra');
239                    expect(err.foo).toEqual('bar');
240                });
241                try {
242                    customObj.doSomething({
243                        extraData: 'extra'
244                    });
245                } 
246                catch (err) {}
247            });
248        });
249    });
250});
251