PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/assets/extjs/src/core/test/unit/spec/lang/Error.js

https://bitbucket.org/nkusumah/indoquran
JavaScript | 277 lines | 245 code | 30 blank | 2 comment | 0 complexity | 628ebf25d9ef16fe6ccc309dffc8da16 MD5 | raw file
  1. describe("Ext.Error", function() {
  2. var global;
  3. beforeEach(function() {
  4. global = Ext.global;
  5. // mock the console to avoid logging to the real console during the tests
  6. Ext.global = {
  7. console: {
  8. dir: function(s) {
  9. return s;
  10. },
  11. log: function(s) {
  12. return s;
  13. },
  14. error: function(s) {
  15. return s;
  16. },
  17. warn: function(s) {
  18. return s;
  19. }
  20. }
  21. };
  22. });
  23. afterEach(function() {
  24. Ext.global = global;
  25. });
  26. describe("raising an error via Ext.Error.raise", function() {
  27. describe("passing a string", function() {
  28. it("should throw an error with a msg property", function() {
  29. var error;
  30. try {
  31. Ext.Error.raise('foo');
  32. }
  33. catch (err) {
  34. error = err;
  35. }
  36. expect(error.msg).toEqual('foo');
  37. });
  38. it("should log an error to the console", function() {
  39. spyOn(Ext.global.console, 'error');
  40. try {
  41. Ext.Error.raise('foo');
  42. }
  43. catch (err) {}
  44. expect(Ext.global.console.error).toHaveBeenCalledWith('[E] foo');
  45. });
  46. it("should log the error object to the console", function() {
  47. spyOn(Ext.global.console, 'dir').andCallFake(function(err){
  48. expect(err.msg).toEqual('foo');
  49. });
  50. try {
  51. Ext.Error.raise('foo');
  52. }
  53. catch (err) {}
  54. });
  55. it("should do nothing when Ext.Error.ignore = true", function() {
  56. spyOn(Ext.global.console, 'warn');
  57. Ext.Error.ignore = true;
  58. try {
  59. Ext.Error.raise('foo');
  60. }
  61. catch (err) {
  62. expect('Error should not have been caught').toBe(true);
  63. }
  64. expect(Ext.global.console.warn).not.toHaveBeenCalled();
  65. Ext.Error.ignore = false;
  66. });
  67. it("should not throw an error if handled by Ext.Error.handle", function() {
  68. spyOn(Ext.global.console, 'warn');
  69. var origHandle = Ext.Error.handle;
  70. Ext.Error.handle = function(err) {
  71. expect(err.msg).toEqual('foo');
  72. return true;
  73. }
  74. try {
  75. Ext.Error.raise('foo');
  76. }
  77. catch (err) {
  78. expect('Error should not have been caught').toBe(true);
  79. }
  80. expect(Ext.global.console.warn).not.toHaveBeenCalled();
  81. Ext.Error.handle = origHandle;
  82. });
  83. });
  84. describe("passing an object with a msg property", function() {
  85. it("should throw an error with a msg property", function() {
  86. var error;
  87. try {
  88. Ext.Error.raise({msg: 'foo'});
  89. }
  90. catch (err) {
  91. error = err;
  92. }
  93. expect(error.msg).toEqual('foo');
  94. });
  95. it("should log an error to the console", function() {
  96. spyOn(Ext.global.console, 'error');
  97. try {
  98. Ext.Error.raise({msg: 'foo'});
  99. }
  100. catch (err) {}
  101. expect(Ext.global.console.error).toHaveBeenCalledWith('[E] foo');
  102. });
  103. it("should log the error object to the console", function() {
  104. spyOn(Ext.global.console, 'dir').andCallFake(function(err){
  105. expect(err.msg).toEqual('foo');
  106. });
  107. try {
  108. Ext.Error.raise({msg: 'foo'});
  109. }
  110. catch (err) {}
  111. });
  112. it("should do nothing when Ext.Error.ignore = true", function() {
  113. spyOn(Ext.global.console, 'warn');
  114. Ext.Error.ignore = true;
  115. try {
  116. Ext.Error.raise({msg: 'foo'});
  117. }
  118. catch (err) {
  119. expect('Error should not have been caught').toBe(true);
  120. }
  121. expect(Ext.global.console.warn).not.toHaveBeenCalled();
  122. Ext.Error.ignore = false;
  123. });
  124. it("should not throw an error if handled by Ext.Error.handle", function() {
  125. spyOn(Ext.global.console, 'warn');
  126. var origHandle = Ext.Error.handle;
  127. Ext.Error.handle = function(err) {
  128. expect(err.msg).toEqual('foo');
  129. return true;
  130. }
  131. try {
  132. Ext.Error.raise({msg: 'foo'});
  133. }
  134. catch (err) {
  135. expect('Error should not have been caught').toBe(true);
  136. }
  137. expect(Ext.global.console.warn).not.toHaveBeenCalled();
  138. Ext.Error.handle = origHandle;
  139. });
  140. });
  141. describe("passing an object with custom metadata", function() {
  142. it("should throw an error with matching metadata", function() {
  143. var error;
  144. try {
  145. Ext.Error.raise({
  146. msg: 'Custom error',
  147. data: {
  148. foo: 'bar'
  149. }
  150. });
  151. }
  152. catch (err) {
  153. error = err;
  154. }
  155. expect(error.msg).toEqual('Custom error');
  156. expect(error.data).not.toBe(null);
  157. expect(error.data.foo).toEqual('bar');
  158. });
  159. it("should log the complete metadata to the console", function() {
  160. spyOn(Ext.global.console, 'dir').andCallFake(function(err){
  161. expect(err.msg).toEqual('Custom error');
  162. expect(err.data).not.toBe(null);
  163. expect(err.data.foo).toEqual('bar');
  164. });
  165. try {
  166. Ext.Error.raise({
  167. msg: 'Custom error',
  168. data: {
  169. foo: 'bar'
  170. }
  171. });
  172. }
  173. catch (err) {}
  174. });
  175. });
  176. describe("originating from within a class defined by Ext", function() {
  177. Ext.define('CustomClass', {
  178. doSomething: function(o){
  179. Ext.Error.raise({
  180. msg: 'Custom error',
  181. data: o,
  182. foo: 'bar'
  183. });
  184. }
  185. });
  186. var customObj = Ext.create('CustomClass');
  187. it("should throw an error containing the source class and method", function() {
  188. var error;
  189. try {
  190. customObj.doSomething({
  191. extraData: 'extra'
  192. });
  193. }
  194. catch (err) {
  195. error = err;
  196. }
  197. expect(error.msg).toEqual('Custom error');
  198. expect(error.sourceClass).toEqual('CustomClass');
  199. expect(error.sourceMethod).toEqual('doSomething');
  200. expect(error.toString()).toBe('CustomClass.doSomething(): Custom error');
  201. });
  202. it("should log the complete metadata to the console", function() {
  203. spyOn(Ext.global.console, 'dir').andCallFake(function(err){
  204. expect(err.msg).toEqual('Custom error');
  205. expect(err.sourceClass).toEqual('CustomClass');
  206. expect(err.sourceMethod).toEqual('doSomething');
  207. expect(err.data).not.toBe(null);
  208. expect(err.data.extraData).not.toBe(null);
  209. expect(err.data.extraData).toEqual('extra');
  210. expect(err.foo).toEqual('bar');
  211. });
  212. try {
  213. customObj.doSomething({
  214. extraData: 'extra'
  215. });
  216. }
  217. catch (err) {
  218. }
  219. });
  220. });
  221. });
  222. describe("Throwing an an Ext.Error directly intantiated", function() {
  223. describe("Passing an string as constructor argument", function() {
  224. it("should contain a msg property with the given string as value", function() {
  225. expect(function() {
  226. throw new Ext.Error("expected message");
  227. }).toRaiseExtError("expected message");
  228. });
  229. });
  230. });
  231. xdescribe("Ext.deprecated", function() {
  232. // failing only on CI
  233. it("should return a function that raises an error with the given suggestion", function() {
  234. Ext.ClassManager.enableNamespaceParseCache = false;
  235. Ext.define("Test.ThisClassContainsADeprecatedMethod", {
  236. deprecatedMethod : Ext.deprecated("use another function")
  237. });
  238. expect(function() {
  239. new Test.ThisClassContainsADeprecatedMethod().deprecatedMethod();
  240. }).toThrow('The method "Test.ThisClassContainsADeprecatedMethod.deprecatedMethod" has been removed. use another function');
  241. try {
  242. delete Test;
  243. } catch(e) { }
  244. Ext.ClassManager.enableNamespaceParseCache = true;
  245. });
  246. });
  247. });