core.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. QUnit.test( "ajax get 200", assert => {
  2. App.Config.set('api_path', '/api/v1')
  3. var done = assert.async(1)
  4. new Promise( (resolve, reject) => {
  5. App.Ajax.request({
  6. type: 'GET',
  7. url: '/assets/tests/ajax-test.json',
  8. success: resolve,
  9. error: reject
  10. });
  11. }).then( function(data) {
  12. assert.ok( true, "File found!")
  13. assert.equal(data.success, true, "content parsable and assert.ok!")
  14. assert.equal(data.success2, undefined, "content parsable and assert.ok!")
  15. }, function(data) {
  16. assert.ok( false, "Failed!")
  17. })
  18. .finally(done)
  19. });
  20. QUnit.test( "ajax - queue - ajax get 200 1/2", assert => {
  21. var done = assert.async(1)
  22. new Promise( (resolve, reject) => {
  23. App.Ajax.request({
  24. type: 'GET',
  25. url: '/tests/wait/2',
  26. queue: true,
  27. success: resolve,
  28. error: reject
  29. });
  30. }).then( function(data) {
  31. assert.ok( !window.testAjax, 'ajax - queue - check queue')
  32. window.testAjax = true;
  33. assert.equal(data.success, true, "ajax - queue - content parsable and assert.ok!")
  34. assert.equal(data.success2, undefined, "ajax - queue - content parsable and assert.ok!")
  35. }, function(data) {
  36. assert.ok( false, "Failed!")
  37. })
  38. .finally(done)
  39. });
  40. QUnit.test( "ajax - queue - ajax get 200 2/2", assert => {
  41. var done = assert.async(1)
  42. new Promise( (resolve, reject) => {
  43. App.Ajax.request({
  44. type: 'GET',
  45. url: '/tests/wait/1',
  46. queue: true,
  47. success: resolve,
  48. error: reject
  49. });
  50. }).then( function(data) {
  51. // check queue
  52. assert.ok( window.testAjax, 'ajax - queue - check queue')
  53. window.testAjax = undefined;
  54. assert.equal(data.success, true, "content parsable and assert.ok!")
  55. assert.equal(data.success2, undefined, "content parsable and assert.ok!")
  56. }, function(data) {
  57. assert.ok( false, "Failed!")
  58. })
  59. .finally(done)
  60. });
  61. QUnit.test( "ajax - parallel - ajax get 200", assert => {
  62. var done = assert.async(2)
  63. new Promise( (resolve, reject) => {
  64. App.Ajax.request({
  65. type: 'GET',
  66. url: '/tests/wait/3',
  67. success: resolve,
  68. error: reject
  69. });
  70. new Promise( (resolve, reject) => {
  71. App.Ajax.request({
  72. type: 'GET',
  73. url: '/tests/wait/1',
  74. success: resolve,
  75. error: reject
  76. });
  77. }).then( function(data) {
  78. // check queue
  79. assert.ok( !window.testAjaxQ, 'ajax - parallel - check queue')
  80. window.testAjaxQ = true;
  81. assert.equal(data.success, true, "content parsable and assert.ok!")
  82. assert.equal(data.success2, undefined, "content parsable and assert.ok!")
  83. }, function(data) {
  84. assert.ok( false, "Failed!")
  85. })
  86. .finally(done)
  87. }).then( function(data) {
  88. // check queue
  89. assert.ok( window.testAjaxQ, 'ajax - parallel - check queue')
  90. window.testAjaxQ = undefined;
  91. assert.equal(data.success, true, "ajax - parallel - content parsable and assert.ok!")
  92. assert.equal(data.success2, undefined, "ajax - parallel - content parsable and assert.ok!")
  93. done()
  94. }, function(data) {
  95. assert.ok( false, "Failed!")
  96. })
  97. });
  98. QUnit.test('delay - test', assert => {
  99. var done = assert.async(1)
  100. window.testDelay1 = false
  101. new Promise( (resolve, reject) => {
  102. App.Delay.set(resolve, 1000, 'delay-test1', 'level');
  103. new Promise( (resolve, reject) => {
  104. App.Delay.set(resolve, 2000, 'delay-test1', 'level');
  105. }).then( function() {
  106. assert.ok(!window.testDelay1, 'delay - 1/2')
  107. window.testDelay1 = 1;
  108. })
  109. new Promise( (resolve, reject) => {
  110. App.Delay.set(resolve, 3000, 'delay-test1-verify', 'level');
  111. }).then( function() {
  112. assert.ok(window.testDelay1, 'delay - 2/2')
  113. window.testDelay1 = false;
  114. })
  115. .finally(done)
  116. }).then( function() {
  117. assert.ok(false, 'delay - 1/2 - FAILED - should not be executed, will be reset by next set()')
  118. window.testDelay1 = true;
  119. })
  120. });
  121. QUnit.test('delay - test 2', assert => {
  122. var done = assert.async(1)
  123. new Promise( (resolve, reject) => {
  124. App.Delay.set(resolve, 2000);
  125. new Promise( (resolve, reject) => {
  126. App.Delay.set(resolve, 1000);
  127. }).then( function() {
  128. assert.ok(!window.testDelay2, 'delay - test 2 - 1/3')
  129. })
  130. new Promise( (resolve, reject) => {
  131. App.Delay.set(resolve, 3000);
  132. }).then( function() {
  133. assert.ok(window.testDelay2, 'delay - test 2 - 3/3')
  134. })
  135. .finally(done)
  136. }).then( function() {
  137. assert.ok(!window.testDelay2, 'delay - test 2 - 2/3')
  138. window.testDelay2 = 1;
  139. })
  140. });
  141. QUnit.test('delay - test 3', assert => {
  142. var done = assert.async(1)
  143. new Promise( (resolve, reject) => {
  144. App.Delay.set(resolve, 1000, 'delay3');
  145. App.Delay.clear('delay3')
  146. assert.ok(true, 'delay - test 3 - 1/1')
  147. done()
  148. }).then( function() {
  149. assert.ok(false, 'delay - test 3 - 1/1 - FAILED')
  150. })
  151. });
  152. QUnit.test('delay - test 4', assert => {
  153. var done = assert.async(1)
  154. new Promise( (resolve, reject) => {
  155. App.Delay.set(resolve, 1000, undefined, 'Page');
  156. App.Delay.clearLevel('Page')
  157. assert.ok(true, 'delay - test 4 - 1/1')
  158. done()
  159. }).then( function() {
  160. assert.ok(false, 'delay - test 4 - 1/1 - FAILED')
  161. })
  162. });
  163. QUnit.test('interval - test 1', assert => {
  164. var done = assert.async(1)
  165. window.testInterval1 = 1
  166. App.Interval.set(function() {
  167. window.testInterval1 += 1;
  168. },
  169. 100,
  170. 'interval-test1'
  171. );
  172. new Promise( (resolve, reject) => {
  173. App.Delay.set(resolve, 1000);
  174. new Promise( (resolve, reject) => {
  175. App.Delay.set(resolve, 2000);
  176. }).then( function() {
  177. assert.equal(window.testInterval1, window.testInterval1Backup, 'interval - did not change after clear interval')
  178. })
  179. .finally(done)
  180. }).then( function() {
  181. assert.notEqual(window.testInterval1, 1, 'interval - interval moved up')
  182. App.Interval.clear('interval-test1')
  183. window.testInterval1Backup = window.testInterval1;
  184. })
  185. })
  186. QUnit.test('interval - test 2', assert => {
  187. var done = assert.async(1)
  188. window.testInterval1 = 1
  189. App.Interval.set(function() {
  190. window.testInterval1 += 1;
  191. },
  192. 100,
  193. undefined,
  194. 'someLevel'
  195. );
  196. new Promise( (resolve, reject) => {
  197. App.Delay.set(resolve, 1000);
  198. new Promise( (resolve, reject) => {
  199. App.Delay.set(resolve, 2000);
  200. }).then( function() {
  201. assert.equal(window.testInterval1, window.testInterval1Backup, 'interval - did not change after clear interval')
  202. })
  203. .finally(done)
  204. }).then( function() {
  205. assert.notEqual(window.testInterval1, 1, 'interval - interval moved up')
  206. App.Interval.clearLevel('someLevel')
  207. window.testInterval1Backup = window.testInterval1;
  208. })
  209. })
  210. // events
  211. QUnit.test('events simple', assert => {
  212. // single bind
  213. App.Event.bind('test1', function(data) {
  214. assert.ok(true, 'event received - single bind')
  215. assert.equal(data.success, true, 'event received - data assert.ok - single bind')
  216. });
  217. App.Event.bind('test2', function(data) {
  218. assert.ok(false, 'should not be triggered - single bind')
  219. });
  220. App.Event.trigger('test1', { success: true })
  221. App.Event.unbind('test1')
  222. App.Event.bind('test1', function(data) {
  223. assert.ok(false, 'should not be triggered - single bind')
  224. });
  225. App.Event.unbind('test1')
  226. App.Event.trigger('test1', { success: true })
  227. // multi bind
  228. App.Event.bind('test1-1 test1-2', function(data) {
  229. assert.ok(true, 'event received - multi bind')
  230. assert.equal(data.success, true, 'event received - data assert.ok - multi bind')
  231. });
  232. App.Event.bind('test1-3', function(data) {
  233. assert.ok(false, 'should not be triggered - multi bind')
  234. });
  235. App.Event.trigger('test1-2', { success: true })
  236. App.Event.unbind('test1-1')
  237. App.Event.bind('test1-1', function(data) {
  238. assert.ok(false, 'should not be triggered - multi bind')
  239. });
  240. App.Event.trigger('test1-2', { success: true })
  241. });
  242. QUnit.test('events level', assert => {
  243. // bind with level
  244. App.Event.bind('test3', function(data) {
  245. assert.ok(false, 'should not be triggered!')
  246. }, 'test-level')
  247. // unbind with level
  248. App.Event.unbindLevel( 'test-level')
  249. // bind with level
  250. App.Event.bind('test3', function(data) {
  251. assert.ok(true, 'event received')
  252. assert.equal(data.success, true, 'event received - data assert.ok - level bind')
  253. }, 'test-level')
  254. App.Event.trigger('test3', { success: true})
  255. });
  256. // session store
  257. QUnit.test('session store', assert => {
  258. var tests = [
  259. 'some 123äöüßadajsdaiosjdiaoidj',
  260. { key: 123 },
  261. { key1: { key1: [1,2,3,4] }, key2: [1,2,'äöüß'] },
  262. ];
  263. // write/get
  264. App.SessionStorage.clear()
  265. _.each(tests, function(test) {
  266. App.SessionStorage.set('test1', test)
  267. var item = App.SessionStorage.get('test1')
  268. assert.deepEqual(test, item, 'write/get - compare stored and actual data')
  269. });
  270. // undefined/get
  271. App.SessionStorage.clear()
  272. _.each(tests, function(test) {
  273. var item = App.SessionStorage.get('test1')
  274. assert.deepEqual(undefined, item, 'undefined/get - compare not existing data and actual data')
  275. });
  276. // write/get/delete
  277. var tests = [
  278. { key: 'test1', value: 'some 123äöüßadajsdaiosjdiaoidj' },
  279. { key: 123, value: { a: 123, b: 'sdaad' } },
  280. { key: '123äöüß', value: { key1: [1,2,3,4] }, key2: [1,2,'äöüß'] },
  281. ];
  282. App.SessionStorage.clear()
  283. _.each(tests, function(test) {
  284. App.SessionStorage.set(test.key, test.value)
  285. });
  286. _.each(tests, function(test) {
  287. var item = App.SessionStorage.get(test.key)
  288. assert.deepEqual(test.value, item, 'write/get/delete - compare stored and actual data')
  289. App.SessionStorage.delete( test.key)
  290. item = App.SessionStorage.get(test.key)
  291. assert.deepEqual(undefined, item, 'write/get/delete - compare deleted data')
  292. });
  293. });
  294. // config
  295. QUnit.test('config', assert => {
  296. // simple
  297. var tests = [
  298. { key: 'test1', value: 'some 123äöüßadajsdaiosjdiaoidj' },
  299. { key: 123, value: { a: 123, b: 'sdaad' } },
  300. { key: '123äöüß', value: { key1: [1,2,3,4] }, key2: [1,2,'äöüß'] },
  301. ];
  302. _.each(tests, function(test) {
  303. App.Config.set(test.key, test.value )
  304. });
  305. _.each(tests, function(test) {
  306. var item = App.Config.get(test.key )
  307. assert.deepEqual(item, test.value, 'set/get tests')
  308. });
  309. // group
  310. var test_groups = [
  311. { key: 'test2', value: [ 'some 123äöüßadajsdaiosjdiaoidj' ] },
  312. { key: 1234, value: { a: 123, b: 'sdaad' } },
  313. { key: '123äöüß', value: { key1: [1,2,3,4,5,6] }, key2: [1,2,'äöüß'] },
  314. ];
  315. var group = {};
  316. _.each(test_groups, function(test) {
  317. App.Config.set(test.key, test.value, 'group1')
  318. group[test.key] = test.value
  319. });
  320. // verify whole group
  321. var item = App.Config.get('group1')
  322. assert.deepEqual(item, group, 'group - verify group hash')
  323. // verify each setting
  324. _.each(test_groups, function(test) {
  325. var item = App.Config.get(test.key, 'group1')
  326. assert.deepEqual(item, test.value, 'group set/get tests')
  327. });
  328. });
  329. // clone
  330. QUnit.test('clone', assert => {
  331. // simple
  332. var tests = [
  333. {
  334. key1: 123,
  335. key2: 1234
  336. },
  337. {
  338. key1: 123,
  339. key2: '1234'
  340. },
  341. [1,2,4,5,6],
  342. 'some string äöü',
  343. {
  344. key1: 123,
  345. key2: null,
  346. },
  347. {
  348. key1: 123,
  349. key2: undefined,
  350. },
  351. {
  352. key1: 123,
  353. key2: undefined,
  354. key3: {
  355. keya: 'some',
  356. keyb: undefined,
  357. },
  358. key4: ['a', 'b', null, false, true, undefined],
  359. },
  360. undefined,
  361. false,
  362. true,
  363. null,
  364. {
  365. key1: 123,
  366. key2: undefined,
  367. key3: {
  368. keya: 'some',
  369. keyb: undefined,
  370. },
  371. key4: ['a', 'b', {
  372. a: 123,
  373. b: [1,5,7,8,1213,1231321]
  374. }],
  375. },
  376. ];
  377. _.each(tests, function(test) {
  378. var item = clone( test )
  379. assert.deepEqual(item, test, 'clone')
  380. });
  381. // complex test
  382. var source = [
  383. { name: 'some name' },
  384. { name: 'some name2' },
  385. { fn: function() { return 'test' } },
  386. ]
  387. var reference = [
  388. { name: 'some name' },
  389. { name: 'some name2' },
  390. { fn: undefined },
  391. ]
  392. var result = clone( source )
  393. // modify source later, should not have any result
  394. source[0].name = 'some new name'
  395. assert.deepEqual(result, reference, 'clone')
  396. // full test
  397. var source = [
  398. { name: 'some name' },
  399. { name: 'some name2' },
  400. { fn: function a() { return 'test' } },
  401. ]
  402. var reference = [
  403. { name: 'some name' },
  404. { name: 'some name2' },
  405. { fn: function a() { return 'test' } },
  406. ]
  407. var result = clone( source, true )
  408. // modify source later, should not have any result
  409. source[0].name = 'some new name'
  410. source[2].fn = 'some new name'
  411. assert.deepEqual(result[0], reference[0], 'clone full')
  412. assert.deepEqual(result[1], reference[1], 'clone full')
  413. assert.equal(typeof reference[2].fn, 'function')
  414. assert.equal(typeof result[2].fn, 'function')
  415. assert.equal(reference[2].fn(), 'test')
  416. assert.equal(result[2].fn(), 'test')
  417. });
  418. // diff
  419. QUnit.test('difference', assert => {
  420. // simple
  421. var object1 = {
  422. key1: 123,
  423. key2: 1234
  424. }
  425. var object2 = {
  426. key1: 123,
  427. key2: 1235
  428. }
  429. var result = {
  430. key2: 1235
  431. }
  432. var item = difference(object1, object2)
  433. assert.deepEqual(item, result)
  434. object1 = {
  435. key1: 123,
  436. key2: 123
  437. }
  438. object2 = {
  439. key1: 123,
  440. key2: 123
  441. }
  442. result = {}
  443. item = difference(object1, object2)
  444. assert.deepEqual(item, result)
  445. object1 = {
  446. key1: 123,
  447. key2: [1,3,5]
  448. }
  449. object2 = {
  450. key1: 123,
  451. key2: 123
  452. }
  453. result = {
  454. key2: 123
  455. }
  456. item = difference(object1, object2)
  457. assert.deepEqual(item, result)
  458. object1 = {
  459. key1: 123,
  460. key2: [1,3,5]
  461. }
  462. object2 = {
  463. key1: 123,
  464. }
  465. result = {
  466. key2: undefined
  467. }
  468. item = difference(object1, object2)
  469. assert.deepEqual(item, result)
  470. object1 = {
  471. key1: 123,
  472. }
  473. object2 = {
  474. key1: 123,
  475. key2: 124
  476. }
  477. result = {
  478. key2: 124
  479. }
  480. item = difference(object1, object2)
  481. assert.deepEqual(item, result)
  482. object1 = {
  483. customer_id: 1,
  484. organization_id: 2,
  485. }
  486. object2 = {
  487. customer_id: 1,
  488. organization_id: null,
  489. }
  490. result = {
  491. organization_id: null,
  492. }
  493. item = difference(object1, object2)
  494. assert.deepEqual(item, result)
  495. object1 = {
  496. customer_id: 1,
  497. organization_id: null,
  498. }
  499. object2 = {
  500. customer_id: 1,
  501. organization_id: 2,
  502. }
  503. result = {
  504. organization_id: 2,
  505. }
  506. item = difference(object1, object2)
  507. assert.deepEqual(item, result)
  508. object1 = {
  509. customer_id: 1,
  510. preferences: { resolved: true },
  511. }
  512. object2 = {
  513. customer_id: 1,
  514. preferences: {},
  515. }
  516. result = {
  517. preferences: { resolved: undefined }
  518. }
  519. item = difference(object1, object2)
  520. assert.deepEqual(item, result)
  521. object1 = {
  522. customer_id: 1,
  523. }
  524. object2 = {
  525. customer_id: 1,
  526. preferences: { resolved: true },
  527. }
  528. result = {
  529. preferences: { resolved: true }
  530. }
  531. item = difference(object1, object2)
  532. assert.deepEqual(item, result)
  533. object1 = {
  534. customer_id: 1,
  535. preferences: {},
  536. }
  537. object2 = {
  538. customer_id: 1,
  539. preferences: { resolved: true },
  540. }
  541. result = {
  542. preferences: { resolved: true }
  543. }
  544. item = difference(object1, object2)
  545. assert.deepEqual(item, result)
  546. object1 = {
  547. customer_id: 1,
  548. preferences: { resolved: false },
  549. }
  550. object2 = {
  551. customer_id: 1,
  552. preferences: { resolved: true },
  553. }
  554. result = {
  555. preferences: { resolved: true }
  556. }
  557. item = difference(object1, object2)
  558. assert.deepEqual(item, result)
  559. object1 = {
  560. customer_id: 1,
  561. preferences: { resolved: true },
  562. }
  563. object2 = {
  564. customer_id: 1,
  565. preferences: { resolved: true },
  566. }
  567. result = {}
  568. item = difference(object1, object2)
  569. assert.deepEqual(item, result)
  570. item = difference({}, undefined)
  571. assert.deepEqual(item, {})
  572. item = difference(undefined, {})
  573. assert.deepEqual(item, {})
  574. try {
  575. a = { a: { a: 1 } }
  576. b = { b: { b: a } }
  577. a.a.a = b
  578. item = difference(a, a)
  579. assert.deepEqual(item, {})
  580. } catch(error) {
  581. assert.deepEqual(false, true, 'recursion failed: ' + error)
  582. }
  583. });
  584. QUnit.test('auth - not existing user', assert => {
  585. var done = assert.async(1)
  586. new Promise( (resolve, reject) => {
  587. App.Auth.login({
  588. data: {
  589. username: 'not_existing',
  590. password: 'not_existing',
  591. },
  592. success: resolve,
  593. error: reject
  594. });
  595. }).then( function(data) {
  596. assert.ok(false, 'ok')
  597. }, function() {
  598. assert.ok(true, 'ok')
  599. })
  600. .finally(done)
  601. })
  602. QUnit.test('auth - existing user', assert => {
  603. App.Config.set('api_path', '/api/v1')
  604. var done = assert.async(1)
  605. new Promise( (resolve, reject) => {
  606. App.Auth.login({
  607. data: {
  608. username: 'admin@example.com',
  609. password: 'test',
  610. },
  611. success: resolve,
  612. error: reject
  613. });
  614. }).then( function(data) {
  615. assert.ok(true, 'authenticated')
  616. var user = App.Session.get('login')
  617. assert.equal('admin@example.com', user, 'session login')
  618. }, function() {
  619. assert.ok(false, 'failed')
  620. })
  621. .finally(done)
  622. })