PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/archesproject/arches/Media/js/ui_frameworks/ext-4.0.2a/src/core/test/unit/spec/lang/Error.js

https://bitbucket.org/arches/arches
JavaScript | 251 lines | 208 code | 28 blank | 15 comment | 0 complexity | fd5f70d7bcca16b1f584909750b327c9 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. /*
  2. This file is part of Ext JS 4
  3. Copyright (c) 2011 Sencha Inc
  4. Contact: http://www.sencha.com/contact
  5. Commercial Usage
  6. Licensees 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.
  7. If you are unsure which license is appropriate for your use, please contact the sales department at http://www.sencha.com/contact.
  8. */
  9. describe("Ext.Error", function() {
  10. var global;
  11. beforeEach(function() {
  12. global = Ext.global;
  13. // mock the console to avoid logging to the real console during the tests
  14. Ext.global = {
  15. console: {
  16. dir: function(s) {
  17. return s;
  18. },
  19. error: function(s) {
  20. return s;
  21. },
  22. warn: function(s) {
  23. return s;
  24. }
  25. }
  26. };
  27. });
  28. afterEach(function() {
  29. Ext.global = global;
  30. });
  31. describe("raising an error via Ext.Error.raise", function() {
  32. describe("passing a string", function() {
  33. it("should throw an error with a msg property", function() {
  34. try {
  35. Ext.Error.raise('foo');
  36. }
  37. catch (err) {
  38. expect(err.msg).toEqual('foo');
  39. }
  40. });
  41. it("should log an error to the console", function() {
  42. spyOn(Ext.global.console, 'error');
  43. try {
  44. Ext.Error.raise('foo');
  45. }
  46. catch (err) {}
  47. expect(Ext.global.console.error).toHaveBeenCalledWith('foo');
  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. it("should do nothing when Ext.Error.ignore = true", function() {
  59. spyOn(Ext.global.console, 'warn');
  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. it("should not throw an error if handled by Ext.Error.handle", function() {
  71. spyOn(Ext.global.console, 'warn');
  72. var origHandle = Ext.Error.handle;
  73. Ext.Error.handle = function(err) {
  74. expect(err.msg).toEqual('foo');
  75. return true;
  76. }
  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.handle = origHandle;
  85. });
  86. });
  87. describe("passing an object with a msg property", function() {
  88. it("should throw an error with a msg property", function() {
  89. try {
  90. Ext.Error.raise({msg: 'foo'});
  91. }
  92. catch (err) {
  93. expect(err.msg).toEqual('foo');
  94. }
  95. });
  96. it("should log a warning to the console", function() {
  97. spyOn(Ext.global.console, 'error');
  98. try {
  99. Ext.Error.raise({msg: 'foo'});
  100. }
  101. catch (err) {}
  102. expect(Ext.global.console.error).toHaveBeenCalledWith('foo');
  103. });
  104. it("should log the error object to the console", function() {
  105. spyOn(Ext.global.console, 'dir').andCallFake(function(err){
  106. expect(err.msg).toEqual('foo');
  107. });
  108. try {
  109. Ext.Error.raise({msg: 'foo'});
  110. }
  111. catch (err) {}
  112. });
  113. it("should do nothing when Ext.Error.ignore = true", function() {
  114. spyOn(Ext.global.console, 'warn');
  115. Ext.Error.ignore = true;
  116. try {
  117. Ext.Error.raise({msg: 'foo'});
  118. }
  119. catch (err) {
  120. expect('Error should not have been caught').toBe(true);
  121. }
  122. expect(Ext.global.console.warn).not.toHaveBeenCalled();
  123. Ext.Error.ignore = false;
  124. });
  125. it("should not throw an error if handled by Ext.Error.handle", function() {
  126. spyOn(Ext.global.console, 'warn');
  127. var origHandle = Ext.Error.handle;
  128. Ext.Error.handle = function(err) {
  129. expect(err.msg).toEqual('foo');
  130. return true;
  131. }
  132. try {
  133. Ext.Error.raise({msg: 'foo'});
  134. }
  135. catch (err) {
  136. expect('Error should not have been caught').toBe(true);
  137. }
  138. expect(Ext.global.console.warn).not.toHaveBeenCalled();
  139. Ext.Error.handle = origHandle;
  140. });
  141. });
  142. describe("passing an object with custom metadata", function() {
  143. it("should throw an error with matching metadata", function() {
  144. try {
  145. Ext.Error.raise({
  146. msg: 'Custom error',
  147. data: {
  148. foo: 'bar'
  149. }
  150. });
  151. }
  152. catch (err) {
  153. expect(err.msg).toEqual('Custom error');
  154. expect(err.data).not.toBe(null);
  155. expect(err.data.foo).toEqual('bar');
  156. }
  157. });
  158. it("should log the complete metadata to the console", function() {
  159. spyOn(Ext.global.console, 'dir').andCallFake(function(err){
  160. expect(err.msg).toEqual('Custom error');
  161. expect(err.data).not.toBe(null);
  162. expect(err.data.foo).toEqual('bar');
  163. });
  164. try {
  165. Ext.Error.raise({
  166. msg: 'Custom error',
  167. data: {
  168. foo: 'bar'
  169. }
  170. });
  171. }
  172. catch (err) {}
  173. });
  174. });
  175. describe("originating from within a class defined by Ext", function() {
  176. Ext.define('CustomClass', {
  177. doSomething: function(o){
  178. Ext.Error.raise({
  179. msg: 'Custom error',
  180. data: o,
  181. foo: 'bar'
  182. });
  183. }
  184. });
  185. var customObj = Ext.create('CustomClass');
  186. it("should throw an error containing the source class and method", function() {
  187. try {
  188. customObj.doSomething({
  189. extraData: 'extra'
  190. });
  191. }
  192. catch (err) {
  193. expect(err.msg).toEqual('Custom error');
  194. expect(err.sourceClass).toEqual('CustomClass');
  195. expect(err.sourceMethod).toEqual('doSomething');
  196. }
  197. });
  198. it("should log the complete metadata to the console", function() {
  199. spyOn(Ext.global.console, 'dir').andCallFake(function(err){
  200. expect(err.msg).toEqual('Custom error');
  201. expect(err.sourceClass).toEqual('CustomClass');
  202. expect(err.sourceMethod).toEqual('doSomething');
  203. expect(err.data).not.toBe(null);
  204. expect(err.data.extraData).not.toBe(null);
  205. expect(err.data.extraData).toEqual('extra');
  206. expect(err.foo).toEqual('bar');
  207. });
  208. try {
  209. customObj.doSomething({
  210. extraData: 'extra'
  211. });
  212. }
  213. catch (err) {}
  214. });
  215. });
  216. });
  217. });