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