core.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466
  1. // ajax
  2. App.Ajax.request({
  3. type: 'GET',
  4. url: '/assets/tests/ajax-test.json',
  5. success: function (data) {
  6. test( "ajax get 200", function() {
  7. ok( true, "File found!" );
  8. equal( data.success, true, "content parsable and ok!" );
  9. equal( data.success2, undefined, "content parsable and ok!" );
  10. });
  11. },
  12. error: function (data) {
  13. test( "ajax", function() {
  14. ok( false, "Failed!" );
  15. });
  16. }
  17. });
  18. // ajax queueing
  19. App.Ajax.request({
  20. type: 'GET',
  21. url: '/tests/wait/2',
  22. queue: true,
  23. success: function (data) {
  24. test( "ajax - queue - ajax get 200 1/2", function() {
  25. // check queue
  26. ok( !window.testAjax, 'ajax - queue - check queue' );
  27. window.testAjax = true;
  28. equal( data.success, true, "ajax - queue - content parsable and ok!" );
  29. equal( data.success2, undefined, "ajax - queue - content parsable and ok!" );
  30. });
  31. },
  32. error: function (data) {
  33. test( "ajax", function() {
  34. ok( false, "Failed!" );
  35. });
  36. }
  37. });
  38. App.Ajax.request({
  39. type: 'GET',
  40. url: '/tests/wait/1',
  41. queue: true,
  42. success: function (data) {
  43. test( "ajax - queue - ajax get 200 2/2", function() {
  44. // check queue
  45. ok( window.testAjax, 'ajax - queue - check queue' )
  46. window.testAjax = undefined;
  47. equal( data.success, true, "content parsable and ok!" );
  48. equal( data.success2, undefined, "content parsable and ok!" );
  49. });
  50. },
  51. error: function (data) {
  52. test( "ajax", function() {
  53. ok( false, "Failed!" );
  54. });
  55. }
  56. });
  57. // ajax parallel
  58. App.Ajax.request({
  59. type: 'GET',
  60. url: '/tests/wait/2',
  61. success: function (data) {
  62. test( "ajax - parallel - ajax get 200 1/2", function() {
  63. // check queue
  64. ok( window.testAjaxQ, 'ajax - parallel - check queue' );
  65. window.testAjaxQ = undefined;
  66. equal( data.success, true, "ajax - parallel - content parsable and ok!" );
  67. equal( data.success2, undefined, "ajax - parallel - content parsable and ok!" );
  68. });
  69. },
  70. error: function (data) {
  71. test( "ajax", function() {
  72. ok( false, "Failed!" );
  73. });
  74. }
  75. });
  76. App.Ajax.request({
  77. type: 'GET',
  78. url: '/tests/wait/1',
  79. success: function (data) {
  80. test( "ajax - parallel - ajax get 200 2/2", function() {
  81. // check queue
  82. ok( !window.testAjaxQ, 'ajax - parallel - check queue' )
  83. window.testAjaxQ = true;
  84. equal( data.success, true, "content parsable and ok!" );
  85. equal( data.success2, undefined, "content parsable and ok!" );
  86. });
  87. },
  88. error: function (data) {
  89. test( "ajax", function() {
  90. ok( false, "Failed!" );
  91. });
  92. }
  93. });
  94. // delay
  95. window.testDelay1 = false
  96. App.Delay.set( function() {
  97. test( "delay - test 1 - 1/3 - should not be executed, will be reset by next set()", function() {
  98. // check
  99. ok( false, 'delay - test 1 - 1/3 - should not be executed, will be reset by next set()' );
  100. window.testDelay1 = true;
  101. });
  102. },
  103. 1000,
  104. 'delay-test1',
  105. 'level'
  106. );
  107. App.Delay.set( function() {
  108. test( "delay - test 1 - 2/3", function() {
  109. // check
  110. ok( !window.testDelay1, 'delay - test 1 - 2/3' );
  111. window.testDelay1 = 1;
  112. });
  113. },
  114. 2000,
  115. 'delay-test1',
  116. 'level'
  117. );
  118. App.Delay.set( function() {
  119. test( "delay - test 1 - 2/3", function() {
  120. // check
  121. ok( window.testDelay1, 'delay - test 1 - 2/3' );
  122. window.testDelay1 = false;
  123. });
  124. },
  125. 3000,
  126. 'delay-test1-verify',
  127. 'level'
  128. );
  129. App.Delay.set( function() {
  130. test( "delay - test 2 - 1/3", function() {
  131. // check
  132. ok( !window.testDelay2, 'delay - test 2 - 1/3' );
  133. window.testDelay2 = 1;
  134. });
  135. },
  136. 2000
  137. );
  138. App.Delay.set( function() {
  139. test( "delay - test 2 - 2/3", function() {
  140. // check
  141. ok( !window.testDelay2, 'delay - test 2 - 2/3' );
  142. });
  143. },
  144. 1000
  145. );
  146. App.Delay.set( function() {
  147. test( "delay - test 2 - 3/3", function() {
  148. // check
  149. ok( window.testDelay2, 'delay - test 2 - 3/3' );
  150. });
  151. },
  152. 3000
  153. );
  154. window.testDelay3 = 1;
  155. App.Delay.set( function() {
  156. test( "delay - test 3 - 1/1", function() {
  157. // check
  158. ok( false, 'delay - test 3 - 1/1' );
  159. });
  160. },
  161. 1000,
  162. 'delay3'
  163. );
  164. App.Delay.clear('delay3')
  165. App.Delay.set( function() {
  166. test( "delay - test 4 - 1/1", function() {
  167. // check
  168. ok( false, 'delay - test 4 - 1/1' );
  169. });
  170. },
  171. 1000,
  172. undefined,
  173. 'Page'
  174. );
  175. App.Delay.clearLevel('Page')
  176. // interval 1
  177. window.testInterval1 = 1
  178. App.Interval.set( function() {
  179. window.testInterval1 += 1;
  180. },
  181. 500,
  182. 'interval-test1'
  183. );
  184. App.Delay.set( function() {
  185. test( "interval - test 1 - 1/1", function() {
  186. // check
  187. equal( window.testInterval1, 6, 'interval - test 1' );
  188. App.Interval.clear('interval-test1')
  189. });
  190. },
  191. 2400
  192. );
  193. App.Delay.set( function() {
  194. test( "interval - test 1 - 1/1", function() {
  195. // check
  196. equal( window.testInterval1, 6, 'interval - test after clear' );
  197. });
  198. },
  199. 3500
  200. );
  201. // interval 2
  202. window.testInterval2 = 1
  203. App.Interval.set( function() {
  204. window.testInterval2 += 1;
  205. },
  206. 500,
  207. undefined,
  208. 'page'
  209. );
  210. App.Delay.set( function() {
  211. test( "interval - test 2 - 1/1", function() {
  212. // check
  213. equal( window.testInterval2, 6, 'interval - test 2' );
  214. App.Interval.clearLevel('page')
  215. });
  216. },
  217. 2400
  218. );
  219. App.Delay.set( function() {
  220. test( "interval - test 2 - 1/1", function() {
  221. // check
  222. equal( window.testInterval2, 6, 'interval - test 2 - after clear' );
  223. });
  224. },
  225. 3500
  226. );
  227. // i18n
  228. test( "i18n", function() {
  229. // de
  230. App.i18n.set('de');
  231. var translated = App.i18n.translateContent('yes');
  232. equal( translated, 'ja', 'de - yes / ja translated correctly' );
  233. translated = App.i18n.translateContent('<test&now>//*äöüß');
  234. equal( translated, '&lt;test&amp;now&gt;//*äöüß', 'de - <test&now>//*äöüß' );
  235. var timestamp = App.i18n.translateTimestamp('2012-11-06T21:07:24Z');
  236. equal( timestamp, '06.11.2012 22:07', 'de - timestamp translated correctly' );
  237. // en
  238. App.i18n.set('en');
  239. translated = App.i18n.translateContent('yes');
  240. equal( translated, 'yes', 'en - yes translated correctly' );
  241. translated = App.i18n.translateContent('<test&now>');
  242. equal( translated, '&lt;test&amp;now&gt;', 'en - <test&now>' );
  243. timestamp = App.i18n.translateTimestamp('2012-11-06T21:07:24Z');
  244. equal( timestamp, '2012-11-06 22:07', 'en - timestamp translated correctly' );
  245. });
  246. // events
  247. test( "events simple", function() {
  248. // single bind
  249. App.Event.bind( 'test1', function(data) {
  250. ok( true, 'event received - single bind');
  251. equal( data.success, true, 'event received - data ok - single bind');
  252. });
  253. App.Event.bind( 'test2', function(data) {
  254. ok( false, 'should not be triggered - single bind');
  255. });
  256. App.Event.trigger( 'test1', { success: true } );
  257. App.Event.unbind( 'test1')
  258. App.Event.bind( 'test1', function(data) {
  259. ok( false, 'should not be triggered - single bind');
  260. });
  261. App.Event.unbind( 'test1');
  262. App.Event.trigger( 'test1', { success: true } );
  263. // multi bind
  264. App.Event.bind( 'test1-1 test1-2', function(data) {
  265. ok( true, 'event received - multi bind');
  266. equal( data.success, true, 'event received - data ok - multi bind');
  267. });
  268. App.Event.bind( 'test1-3', function(data) {
  269. ok( false, 'should not be triggered - multi bind');
  270. });
  271. App.Event.trigger( 'test1-2', { success: true } );
  272. App.Event.unbind( 'test1-1')
  273. App.Event.bind( 'test1-1', function(data) {
  274. ok( false, 'should not be triggered - multi bind');
  275. });
  276. App.Event.trigger( 'test1-2', { success: true } );
  277. });
  278. test( "events level", function() {
  279. // bind with level
  280. App.Event.bind( 'test3', function(data) {
  281. ok( false, 'should not be triggered!');
  282. }, 'test-level' );
  283. // unbind with level
  284. App.Event.unbindLevel( 'test-level' );
  285. // bind with level
  286. App.Event.bind( 'test3', function(data) {
  287. ok( true, 'event received');
  288. equal( data.success, true, 'event received - data ok - level bind');
  289. }, 'test-level' );
  290. App.Event.trigger( 'test3', { success: true} );
  291. });
  292. // local store
  293. test( "local store", function() {
  294. var tests = [
  295. 'some 123äöüßadajsdaiosjdiaoidj',
  296. { key: 123 },
  297. { key1: { key1: [1,2,3,4] }, key2: [1,2,'äöüß'] },
  298. ];
  299. // write/get
  300. App.Store.clear()
  301. _.each(tests, function(test) {
  302. App.Store.write( 'test1', test );
  303. var item = App.Store.get( 'test1' );
  304. deepEqual( test, item, 'write/get - compare stored and actual data' )
  305. });
  306. // undefined/get
  307. App.Store.clear()
  308. _.each(tests, function(test) {
  309. var item = App.Store.get( 'test1' );
  310. deepEqual( undefined, item, 'undefined/get - compare not existing data and actual data' )
  311. });
  312. // write/get/delete
  313. var tests = [
  314. { key: 'test1', value: 'some 123äöüßadajsdaiosjdiaoidj' },
  315. { key: 123, value: { a: 123, b: 'sdaad' } },
  316. { key: '123äöüß', value: { key1: [1,2,3,4] }, key2: [1,2,'äöüß'] },
  317. ];
  318. App.Store.clear()
  319. _.each(tests, function(test) {
  320. App.Store.write( test.key, test.value );
  321. });
  322. _.each(tests, function(test) {
  323. var item = App.Store.get( test.key );
  324. deepEqual( test.value, item, 'write/get/delete - compare stored and actual data' );
  325. App.Store.delete( test.key );
  326. item = App.Store.get( test.key );
  327. deepEqual( undefined, item, 'write/get/delete - compare deleted data' );
  328. });
  329. });
  330. // config
  331. test( "config", function() {
  332. // simple
  333. var tests = [
  334. { key: 'test1', value: 'some 123äöüßadajsdaiosjdiaoidj' },
  335. { key: 123, value: { a: 123, b: 'sdaad' } },
  336. { key: '123äöüß', value: { key1: [1,2,3,4] }, key2: [1,2,'äöüß'] },
  337. ];
  338. _.each(tests, function(test) {
  339. App.Config.set( test.key, test.value )
  340. });
  341. _.each(tests, function(test) {
  342. var item = App.Config.get( test.key )
  343. deepEqual( item, test.value, 'set/get tests' );
  344. });
  345. // group
  346. var test_groups = [
  347. { key: 'test2', value: [ 'some 123äöüßadajsdaiosjdiaoidj' ] },
  348. { key: 1234, value: { a: 123, b: 'sdaad' } },
  349. { key: '123äöüß', value: { key1: [1,2,3,4,5,6] }, key2: [1,2,'äöüß'] },
  350. ];
  351. var group = {};
  352. _.each(test_groups, function(test) {
  353. App.Config.set( test.key, test.value, 'group1' );
  354. group[test.key] = test.value
  355. });
  356. // verify whole group
  357. var item = App.Config.get( 'group1' );
  358. deepEqual( item, group, 'group - verify group hash');
  359. // verify each setting
  360. _.each(test_groups, function(test) {
  361. var item = App.Config.get( test.key, 'group1' );
  362. deepEqual( item, test.value, 'group set/get tests' );
  363. });
  364. });
  365. // auth
  366. App.Auth.login({
  367. data: {
  368. username: 'not_existing',
  369. password: 'not_existing'
  370. },
  371. success: function(data) {
  372. test( "auth - not existing user", function() {
  373. ok( false, 'ok')
  374. })
  375. },
  376. error: function() {
  377. test( "auth - not existing user", function() {
  378. ok( true, 'ok')
  379. authWithSession();
  380. })
  381. }
  382. });
  383. var authWithSession = function() {
  384. App.Auth.login({
  385. data: {
  386. username: 'nicole.braun@zammad.org',
  387. password: 'test'
  388. },
  389. success: function(data) {
  390. test( "auth - existing user", function() {
  391. ok( true, 'authenticated')
  392. var user = App.Session.get('login');
  393. equal( 'nicole.braun@zammad.org', user, 'session login')
  394. })
  395. },
  396. error: function() {
  397. test( "auth - existing user", function() {
  398. ok( false, 'not authenticated')
  399. })
  400. }
  401. });
  402. }