PageRenderTime 47ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/web-app/client/ext/packages/sencha-core/test/specs/lang/Error.js

https://github.com/mxrguspxrt/klassifikaator
JavaScript | 282 lines | 249 code | 31 blank | 2 comment | 0 complexity | bdc07e39793c4a6ad9e0bbd792144197 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. var customObj;
  178. beforeEach(function() {
  179. Ext.define('spec.CustomClass', {
  180. doSomething: function(o){
  181. Ext.Error.raise({
  182. msg: 'Custom error',
  183. data: o,
  184. foo: 'bar'
  185. });
  186. }
  187. });
  188. customObj = Ext.create('spec.CustomClass');
  189. });
  190. afterEach(function() {
  191. Ext.undefine('spec.CustomClass');
  192. });
  193. it("should throw an error containing the source class and method", function() {
  194. var error;
  195. try {
  196. customObj.doSomething({
  197. extraData: 'extra'
  198. });
  199. }
  200. catch (err) {
  201. error = err;
  202. }
  203. expect(error.msg).toEqual('Custom error');
  204. expect(error.sourceClass).toEqual('spec.CustomClass');
  205. expect(error.sourceMethod).toEqual('doSomething');
  206. expect(error.toString()).toBe('spec.CustomClass.doSomething(): Custom error');
  207. });
  208. it("should log the complete metadata to the console", function() {
  209. spyOn(Ext.global.console, 'dir').andCallFake(function(err){
  210. expect(err.msg).toEqual('Custom error');
  211. expect(err.sourceClass).toEqual('spec.CustomClass');
  212. expect(err.sourceMethod).toEqual('doSomething');
  213. expect(err.data).not.toBe(null);
  214. expect(err.data.extraData).not.toBe(null);
  215. expect(err.data.extraData).toEqual('extra');
  216. expect(err.foo).toEqual('bar');
  217. });
  218. try {
  219. customObj.doSomething({
  220. extraData: 'extra'
  221. });
  222. }
  223. catch (err) {
  224. }
  225. });
  226. });
  227. });
  228. describe("Throwing an an Ext.Error directly intantiated", function() {
  229. describe("Passing an string as constructor argument", function() {
  230. it("should contain a msg property with the given string as value", function() {
  231. expect(function() {
  232. throw new Ext.Error("expected message");
  233. }).toRaiseExtError("expected message");
  234. });
  235. });
  236. });
  237. xdescribe("Ext.deprecated", function() {
  238. // failing only on CI
  239. it("should return a function that raises an error with the given suggestion", function() {
  240. Ext.ClassManager.enableNamespaceParseCache = false;
  241. Ext.define("spec.MyClass", {
  242. deprecatedMethod : Ext.deprecated("use another function")
  243. });
  244. expect(function() {
  245. new spec.ThisClassContainsADeprecatedMethod().deprecatedMethod();
  246. }).toThrow('The method "spec.MyClass.deprecatedMethod" has been removed. use another function');
  247. Ext.undefine('spec.MyClass');
  248. Ext.ClassManager.enableNamespaceParseCache = true;
  249. });
  250. });
  251. });