3 This file is part of Ext JS 4
5 Copyright (c) 2011 Sencha Inc
7 Contact: http://www.sencha.com/contact
9 GNU General Public License Usage
10 This file may be used under the terms of the GNU General Public License version 3.0 as published by the Free Software Foundation and appearing in the file LICENSE included in the packaging of this file. Please review the following information to ensure the GNU General Public License version 3.0 requirements will be met: http://www.gnu.org/copyleft/gpl.html.
12 If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
15 describe("Ext.Error", function() {
18 beforeEach(function() {
21 // mock the console to avoid logging to the real console during the tests
37 afterEach(function() {
41 describe("raising an error via Ext.Error.raise", function() {
43 describe("passing a string", function() {
45 it("should throw an error with a msg property", function() {
47 Ext.Error.raise('foo');
50 expect(err.msg).toEqual('foo');
54 it("should log an error to the console", function() {
55 spyOn(Ext.global.console, 'error');
57 Ext.Error.raise('foo');
60 expect(Ext.global.console.error).toHaveBeenCalledWith('[E] foo');
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');
68 Ext.Error.raise('foo');
73 it("should do nothing when Ext.Error.ignore = true", function() {
74 spyOn(Ext.global.console, 'warn');
76 Ext.Error.ignore = true;
78 Ext.Error.raise('foo');
81 expect('Error should not have been caught').toBe(true);
83 expect(Ext.global.console.warn).not.toHaveBeenCalled();
84 Ext.Error.ignore = false;
87 it("should not throw an error if handled by Ext.Error.handle", function() {
88 spyOn(Ext.global.console, 'warn');
90 var origHandle = Ext.Error.handle;
91 Ext.Error.handle = function(err) {
92 expect(err.msg).toEqual('foo');
96 Ext.Error.raise('foo');
99 expect('Error should not have been caught').toBe(true);
101 expect(Ext.global.console.warn).not.toHaveBeenCalled();
102 Ext.Error.handle = origHandle;
106 describe("passing an object with a msg property", function() {
108 it("should throw an error with a msg property", function() {
110 Ext.Error.raise({msg: 'foo'});
113 expect(err.msg).toEqual('foo');
117 it("should log an error to the console", function() {
118 spyOn(Ext.global.console, 'error');
120 Ext.Error.raise({msg: 'foo'});
123 expect(Ext.global.console.error).toHaveBeenCalledWith('[E] foo');
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');
131 Ext.Error.raise({msg: 'foo'});
136 it("should do nothing when Ext.Error.ignore = true", function() {
137 spyOn(Ext.global.console, 'warn');
139 Ext.Error.ignore = true;
141 Ext.Error.raise({msg: 'foo'});
144 expect('Error should not have been caught').toBe(true);
146 expect(Ext.global.console.warn).not.toHaveBeenCalled();
147 Ext.Error.ignore = false;
150 it("should not throw an error if handled by Ext.Error.handle", function() {
151 spyOn(Ext.global.console, 'warn');
153 var origHandle = Ext.Error.handle;
154 Ext.Error.handle = function(err) {
155 expect(err.msg).toEqual('foo');
159 Ext.Error.raise({msg: 'foo'});
162 expect('Error should not have been caught').toBe(true);
164 expect(Ext.global.console.warn).not.toHaveBeenCalled();
165 Ext.Error.handle = origHandle;
169 describe("passing an object with custom metadata", function() {
171 it("should throw an error with matching metadata", function() {
181 expect(err.msg).toEqual('Custom error');
182 expect(err.data).not.toBe(null);
183 expect(err.data.foo).toEqual('bar');
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');
205 describe("originating from within a class defined by Ext", function() {
207 Ext.define('CustomClass', {
208 doSomething: function(o){
216 var customObj = Ext.create('CustomClass');
218 it("should throw an error containing the source class and method", function() {
220 customObj.doSomething({
225 expect(err.msg).toEqual('Custom error');
226 expect(err.sourceClass).toEqual('CustomClass');
227 expect(err.sourceMethod).toEqual('doSomething');
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');
242 customObj.doSomething({