Upgrade to ExtJS 4.0.0 - Released 04/26/2011
[extjs.git] / src / core / test / unit / spec / lang / Error.js
1 describe("Ext.Error", function() { 
2     var global,
3         consoleWarningMsg = 'An uncaught error was raised with the following data:';
4
5     beforeEach(function() {
6         global = Ext.global;
7
8         // mock the console to avoid logging to the real console during the tests
9         Ext.global = {
10             console: {
11                 dir: function(s) {
12                     return s;
13                 },
14                 warn: function(s) {
15                     return s;
16                 }
17             }
18         };
19     });
20
21     afterEach(function() {
22         Ext.global = global;
23     });
24
25     describe("raising an error via Ext.Error.raise", function() {
26
27         describe("passing a string", function() {
28     
29             it("should throw an error with a msg property", function() {
30                 try {
31                     Ext.Error.raise('foo');
32                 }
33                 catch (err) {
34                     expect(err.msg).toEqual('foo');
35                 }
36             });
37         
38             it("should log a warning to the console", function() {
39                 spyOn(Ext.global.console, 'warn');
40                 try {
41                     Ext.Error.raise('foo');
42                 } 
43                 catch (err) {}
44                 expect(Ext.global.console.warn).toHaveBeenCalledWith(consoleWarningMsg);
45             });
46         
47             it("should log the error object to the console", function() {
48                 spyOn(Ext.global.console, 'dir').andCallFake(function(err){
49                     expect(err.msg).toEqual('foo');
50                 });
51                 try {
52                     Ext.Error.raise('foo');
53                 } 
54                 catch (err) {}
55             });
56         
57             it("should do nothing when Ext.Error.ignore = true", function() {
58                 spyOn(Ext.global.console, 'warn');
59             
60                 Ext.Error.ignore = true;
61                 try {
62                     Ext.Error.raise('foo');
63                 } 
64                 catch (err) {
65                     expect('Error should not have been caught').toBe(true);
66                 }
67                 expect(Ext.global.console.warn).not.toHaveBeenCalled();
68                 Ext.Error.ignore = false;
69             });
70         
71             it("should not throw an error if handled by Ext.Error.handle", function() {
72                 spyOn(Ext.global.console, 'warn');
73             
74                 var origHandle = Ext.Error.handle;
75                 Ext.Error.handle = function(err) {
76                     expect(err.msg).toEqual('foo');
77                     return true;
78                 }
79                 try {
80                     Ext.Error.raise('foo');
81                 } 
82                 catch (err) {
83                     expect('Error should not have been caught').toBe(true);
84                 }
85                 expect(Ext.global.console.warn).not.toHaveBeenCalled();
86                 Ext.Error.handle = origHandle;
87             });
88         });
89     
90         describe("passing an object with a msg property", function() {
91     
92             it("should throw an error with a msg property", function() {
93                 try {
94                     Ext.Error.raise({msg: 'foo'});
95                 }
96                 catch (err) {
97                     expect(err.msg).toEqual('foo');
98                 }
99             });
100         
101             it("should log a warning to the console", function() {
102                 spyOn(Ext.global.console, 'warn');
103                 try {
104                     Ext.Error.raise({msg: 'foo'});
105                 } 
106                 catch (err) {}
107                 expect(Ext.global.console.warn).toHaveBeenCalledWith(consoleWarningMsg);
108             });
109         
110             it("should log the error object to the console", function() {
111                 spyOn(Ext.global.console, 'dir').andCallFake(function(err){
112                     expect(err.msg).toEqual('foo');
113                 });
114                 try {
115                     Ext.Error.raise({msg: 'foo'});
116                 } 
117                 catch (err) {}
118             });
119                             
120             it("should do nothing when Ext.Error.ignore = true", function() {
121                 spyOn(Ext.global.console, 'warn');
122             
123                 Ext.Error.ignore = true;
124                 try {
125                     Ext.Error.raise({msg: 'foo'});
126                 } 
127                 catch (err) {
128                     expect('Error should not have been caught').toBe(true);
129                 }
130                 expect(Ext.global.console.warn).not.toHaveBeenCalled();
131                 Ext.Error.ignore = false;
132             });
133         
134             it("should not throw an error if handled by Ext.Error.handle", function() {
135                 spyOn(Ext.global.console, 'warn');
136             
137                 var origHandle = Ext.Error.handle;
138                 Ext.Error.handle = function(err) {
139                     expect(err.msg).toEqual('foo');
140                     return true;
141                 }
142                 try {
143                     Ext.Error.raise({msg: 'foo'});
144                 } 
145                 catch (err) {
146                     expect('Error should not have been caught').toBe(true);
147                 }
148                 expect(Ext.global.console.warn).not.toHaveBeenCalled();
149                 Ext.Error.handle = origHandle;
150             });
151         });
152     
153         describe("passing an object with custom metadata", function() {
154     
155             it("should throw an error with matching metadata", function() {
156                 try {
157                     Ext.Error.raise({
158                         msg: 'Custom error',
159                         data: {
160                             foo: 'bar'
161                         }
162                     });
163                 }
164                 catch (err) {
165                     expect(err.msg).toEqual('Custom error');
166                     expect(err.data).not.toBe(null);
167                     expect(err.data.foo).toEqual('bar');
168                 }
169             });
170         
171             it("should log the complete metadata to the console", function() {
172                 spyOn(Ext.global.console, 'dir').andCallFake(function(err){
173                     expect(err.msg).toEqual('Custom error');
174                     expect(err.data).not.toBe(null);
175                     expect(err.data.foo).toEqual('bar');
176                 });
177                 try {
178                     Ext.Error.raise({
179                         msg: 'Custom error',
180                         data: {
181                             foo: 'bar'
182                         }
183                     });
184                 } 
185                 catch (err) {}
186             });
187         });
188     
189         describe("originating from within a class defined by Ext", function() {
190     
191             Ext.define('CustomClass', {
192                 doSomething: function(o){
193                     Ext.Error.raise({
194                         msg: 'Custom error',
195                         data: o,
196                         foo: 'bar'
197                     });
198                 }
199             });
200             var customObj = Ext.create('CustomClass');
201         
202             it("should throw an error containing the source class and method", function() {
203                 try {
204                     customObj.doSomething({
205                         extraData: 'extra'
206                     });
207                 }
208                 catch (err) {
209                     expect(err.msg).toEqual('Custom error');
210                     expect(err.sourceClass).toEqual('CustomClass');
211                     expect(err.sourceMethod).toEqual('doSomething');
212                 }
213             });
214         
215             it("should log the complete metadata to the console", function() {
216                 spyOn(Ext.global.console, 'dir').andCallFake(function(err){
217                     expect(err.msg).toEqual('Custom error');
218                     expect(err.sourceClass).toEqual('CustomClass');
219                     expect(err.sourceMethod).toEqual('doSomething');
220                     expect(err.data).not.toBe(null);
221                     expect(err.data.extraData).not.toBe(null);
222                     expect(err.data.extraData).toEqual('extra');
223                     expect(err.foo).toEqual('bar');
224                 });
225                 try {
226                     customObj.doSomething({
227                         extraData: 'extra'
228                     });
229                 } 
230                 catch (err) {}
231             });
232         });
233     });
234 });