tests.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // ajax
  2. App.Com.ajax({
  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.Com.ajax({
  20. type: 'GET',
  21. url: '/test/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.Com.ajax({
  39. type: 'GET',
  40. url: '/test/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.Com.ajax({
  59. type: 'GET',
  60. url: '/test/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.Com.ajax({
  77. type: 'GET',
  78. url: '/test/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. // i18n
  95. test( "i18n", function() {
  96. // de
  97. App.i18n.set('de');
  98. var translated = App.i18n.translateContent('yes');
  99. equal( translated, 'ja', 'de - yes / ja translated correctly' );
  100. translated = App.i18n.translateContent('<test&now>//*äöüß');
  101. equal( translated, '&lt;test&amp;now&gt;//*äöüß', 'de - <test&now>//*äöüß' );
  102. var timestamp = App.i18n.translateTimestamp('2012-11-06T21:07:24Z');
  103. equal( timestamp, '06.11.2012 22:07', 'de - timestamp translated correctly' );
  104. // en
  105. App.i18n.set('en');
  106. translated = App.i18n.translateContent('yes');
  107. equal( translated, 'yes', 'en - yes translated correctly' );
  108. translated = App.i18n.translateContent('<test&now>');
  109. equal( translated, '&lt;test&amp;now&gt;', 'en - <test&now>' );
  110. timestamp = App.i18n.translateTimestamp('2012-11-06T21:07:24Z');
  111. equal( timestamp, '2012-11-06 22:07', 'en - timestamp translated correctly' );
  112. });
  113. // events
  114. test( "events simple", function() {
  115. // single bind
  116. App.Event.bind( 'test1', function(data) {
  117. ok( true, 'event received - single bind');
  118. equal( data.success, true, 'event received - data ok - single bind');
  119. });
  120. App.Event.bind( 'test2', function(data) {
  121. ok( false, 'should not be triggered - single bind');
  122. });
  123. App.Event.trigger( 'test1', { success: true } );
  124. App.Event.unbind( 'test1')
  125. App.Event.bind( 'test1', function(data) {
  126. ok( false, 'should not be triggered - single bind');
  127. });
  128. App.Event.unbind( 'test1');
  129. App.Event.trigger( 'test1', { success: true } );
  130. // multi bind
  131. App.Event.bind( 'test1-1 test1-2', function(data) {
  132. ok( true, 'event received - multi bind');
  133. equal( data.success, true, 'event received - data ok - multi bind');
  134. });
  135. App.Event.bind( 'test1-3', function(data) {
  136. ok( false, 'should not be triggered - multi bind');
  137. });
  138. App.Event.trigger( 'test1-2', { success: true } );
  139. App.Event.unbind( 'test1-1')
  140. App.Event.bind( 'test1-1', function(data) {
  141. ok( false, 'should not be triggered - multi bind');
  142. });
  143. App.Event.trigger( 'test1-2', { success: true } );
  144. });
  145. test( "events level", function() {
  146. // bind with level
  147. App.Event.bind( 'test3', function(data) {
  148. ok( false, 'should not be triggered!');
  149. }, 'test-level' );
  150. // unbind with level
  151. App.Event.unbindLevel( 'test-level' );
  152. // bind with level
  153. App.Event.bind( 'test3', function(data) {
  154. ok( true, 'event received');
  155. equal( data.success, true, 'event received - data ok - level bind');
  156. }, 'test-level' );
  157. App.Event.trigger( 'test3', { success: true} );
  158. });
  159. // local store
  160. test( "local store", function() {
  161. var tests = [
  162. 'some 123äöüßadajsdaiosjdiaoidj',
  163. { key: 123 },
  164. { key1: { key1: [1,2,3,4] }, key2: [1,2,'äöüß'] },
  165. ];
  166. // write/get
  167. App.Store.clear()
  168. _.each(tests, function(test) {
  169. App.Store.write( 'test1', test );
  170. var item = App.Store.get( 'test1' );
  171. deepEqual( test, item, 'write/get - compare stored and actual data' )
  172. });
  173. // undefined/get
  174. App.Store.clear()
  175. _.each(tests, function(test) {
  176. var item = App.Store.get( 'test1' );
  177. deepEqual( undefined, item, 'undefined/get - compare not existing data and actual data' )
  178. });
  179. // write/get/delete
  180. var tests = [
  181. { key: 'test1', value: 'some 123äöüßadajsdaiosjdiaoidj' },
  182. { key: 123, value: { a: 123, b: 'sdaad' } },
  183. { key: '123äöüß', value: { key1: [1,2,3,4] }, key2: [1,2,'äöüß'] },
  184. ];
  185. App.Store.clear()
  186. _.each(tests, function(test) {
  187. App.Store.write( test.key, test.value );
  188. });
  189. _.each(tests, function(test) {
  190. var item = App.Store.get( test.key );
  191. deepEqual( test.value, item, 'write/get/delete - compare stored and actual data' );
  192. App.Store.delete( test.key );
  193. item = App.Store.get( test.key );
  194. deepEqual( undefined, item, 'write/get/delete - compare deleted data' );
  195. });
  196. });
  197. // config
  198. test( "config", function() {
  199. // simple
  200. var tests = [
  201. { key: 'test1', value: 'some 123äöüßadajsdaiosjdiaoidj' },
  202. { key: 123, value: { a: 123, b: 'sdaad' } },
  203. { key: '123äöüß', value: { key1: [1,2,3,4] }, key2: [1,2,'äöüß'] },
  204. ];
  205. _.each(tests, function(test) {
  206. App.Config.set( test.key, test.value )
  207. });
  208. _.each(tests, function(test) {
  209. var item = App.Config.get( test.key )
  210. deepEqual( item, test.value, 'set/get tests' );
  211. });
  212. // group
  213. var test_groups = [
  214. { key: 'test2', value: [ 'some 123äöüßadajsdaiosjdiaoidj' ] },
  215. { key: 1234, value: { a: 123, b: 'sdaad' } },
  216. { key: '123äöüß', value: { key1: [1,2,3,4,5,6] }, key2: [1,2,'äöüß'] },
  217. ];
  218. var group = {};
  219. _.each(test_groups, function(test) {
  220. App.Config.set( test.key, test.value, 'group1' );
  221. group[test.key] = test.value
  222. });
  223. // verify whole group
  224. var item = App.Config.get( 'group1' );
  225. deepEqual( item, group, 'group - verify group hash');
  226. // verify each setting
  227. _.each(test_groups, function(test) {
  228. var item = App.Config.get( test.key, 'group1' );
  229. deepEqual( item, test.value, 'group set/get tests' );
  230. });
  231. });
  232. // auth
  233. App.Auth.login({
  234. data: {
  235. username: 'not_existing',
  236. password: 'not_existing'
  237. },
  238. success: function(data) {
  239. test( "auth - not existing user", function() {
  240. ok( false, 'ok')
  241. })
  242. },
  243. error: function() {
  244. test( "auth - not existing user", function() {
  245. ok( true, 'ok')
  246. authWithSession();
  247. })
  248. }
  249. });
  250. var authWithSession = function() {
  251. App.Auth.login({
  252. data: {
  253. username: 'nicole.braun@zammad.org',
  254. password: 'test'
  255. },
  256. success: function(data) {
  257. test( "auth - existing user", function() {
  258. ok( true, 'authenticated')
  259. var user = App.Session.get('login');
  260. equal( 'nicole.braun@zammad.org', user, 'session login')
  261. })
  262. },
  263. error: function() {
  264. test( "auth - existing user", function() {
  265. ok( false, 'not authenticated')
  266. })
  267. }
  268. });
  269. }