form_tokenfield.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. QUnit.module('form tokenfield')
  2. QUnit.test('initial value', (assert) => {
  3. var done = assert.async(1)
  4. $('#forms').append('<hr><h1>form tokenfield #1</h1><form id="form1"></form>')
  5. var el = $('#form1')
  6. new App.ControllerForm({
  7. el: el,
  8. model: {
  9. configure_attributes: [
  10. { name: '{json}tokenfield', display: 'Tokens', tag: 'tokenfield', null: false, value: ['foo', 'bar'] }
  11. ]
  12. },
  13. });
  14. var params = App.ControllerForm.params(el)
  15. var test_params = {
  16. tokenfield: ['foo', 'bar']
  17. }
  18. assert.deepEqual(params, test_params, 'initial param check')
  19. var initDelay = 750
  20. setTimeout(() => {
  21. assert.equal(el.find('.token-label').eq(0).text(), 'foo', 'first token label')
  22. assert.equal(el.find('.token-label').eq(1).text(), 'bar', 'second token label')
  23. done()
  24. }, initDelay)
  25. })
  26. QUnit.test('default value', (assert) => {
  27. $('#forms').append('<hr><h1>form tokenfield #2</h1><form id="form2"></form>')
  28. el = $('#form2')
  29. new App.ControllerForm({
  30. el: el,
  31. model: {
  32. configure_attributes: [
  33. { name: '{json}tokenfield', display: 'Tokens', tag: 'tokenfield', null: false, default: [] }
  34. ]
  35. },
  36. });
  37. params = App.ControllerForm.params(el)
  38. test_params = {
  39. tokenfield: []
  40. }
  41. assert.deepEqual(params, test_params, 'default param check')
  42. })
  43. QUnit.test('value update', (assert) => {
  44. var done = assert.async(1)
  45. $('#forms').append('<hr><h1>form tokenfield #3</h1><form id="form3"></form>')
  46. el = $('#form3')
  47. new App.ControllerForm({
  48. el: el,
  49. model: {
  50. configure_attributes: [
  51. { name: '{json}tokenfield', display: 'Tokens', tag: 'tokenfield', null: false, default: [] }
  52. ]
  53. },
  54. });
  55. params = App.ControllerForm.params(el)
  56. test_params = {
  57. tokenfield: []
  58. }
  59. assert.deepEqual(params, test_params, 'default param check')
  60. var initDelay = 750
  61. var updateDelay = 500
  62. setTimeout(() => {
  63. var tokenInput = el.find('.token-input')
  64. // Combine all test examples in the same promise chain due to asynchronous behavior.
  65. new Promise((resolve) => {
  66. syn.click(tokenInput[0]).type('one[tab]')
  67. setTimeout(() => { resolve() }, updateDelay)
  68. })
  69. .then(() => {
  70. var params = App.ControllerForm.params(el)
  71. var test_params = {
  72. tokenfield: ['one']
  73. }
  74. assert.deepEqual(params, test_params, 'value updated after token created (1)')
  75. })
  76. .then(() => new Promise((resolve) => {
  77. syn.click(tokenInput[0]).type('two[tab]')
  78. setTimeout(() => { resolve() }, updateDelay)
  79. }))
  80. .then(() => {
  81. var params = App.ControllerForm.params(el)
  82. var test_params = {
  83. tokenfield: ['one', 'two']
  84. }
  85. assert.deepEqual(params, test_params, 'value updated after token created (2)')
  86. })
  87. .then(() => new Promise((resolve) => {
  88. syn.click(tokenInput[0]).type('three[tab]')
  89. setTimeout(() => { resolve() }, updateDelay)
  90. }))
  91. .then(() => {
  92. var params = App.ControllerForm.params(el)
  93. var test_params = {
  94. tokenfield: ['one', 'two', 'three']
  95. }
  96. assert.deepEqual(params, test_params, 'value updated after token created (3)')
  97. })
  98. .then(() => new Promise((resolve) => {
  99. var firstToken = el.find('.token').eq(0)
  100. syn.click(firstToken.find('.close')[0])
  101. setTimeout(() => { resolve() }, updateDelay)
  102. }))
  103. .then(() => {
  104. var params = App.ControllerForm.params(el)
  105. var test_params = {
  106. tokenfield: ['two', 'three']
  107. }
  108. assert.deepEqual(params, test_params, 'value updated after token removed (1)')
  109. })
  110. .then(() => new Promise((resolve) => {
  111. var secondToken = el.find('.token').eq(0)
  112. syn.dblclick(secondToken.find('.token-label')[0])
  113. setTimeout(() => { resolve() }, updateDelay)
  114. }))
  115. .then(() => {
  116. var params = App.ControllerForm.params(el)
  117. var test_params = {
  118. tokenfield: ['three']
  119. }
  120. assert.deepEqual(params, test_params, 'value updated after token being edited (2)')
  121. })
  122. .then(() => new Promise((resolve) => {
  123. syn.type(tokenInput[0], 'twoedit[tab]')
  124. setTimeout(() => { resolve() }, updateDelay)
  125. }))
  126. .then(() => {
  127. var params = App.ControllerForm.params(el)
  128. var test_params = {
  129. tokenfield: ['twoedit', 'three']
  130. }
  131. assert.deepEqual(params, test_params, 'value updated after token edited (2)')
  132. })
  133. .finally(done)
  134. }, initDelay)
  135. });
  136. QUnit.test('compatibility layer for value type (#4709)', (assert) => {
  137. $('#forms').append('<hr><h1>form tokenfield #4</h1><form id="form4"></form>')
  138. var el = $('#form4')
  139. new App.ControllerForm({
  140. el: el,
  141. model: {
  142. configure_attributes: [
  143. { name: '{json}tokenfield', display: 'Tokens', tag: 'tokenfield', null: false, value: 'foobar' }
  144. ]
  145. },
  146. });
  147. var params = App.ControllerForm.params(el)
  148. var test_params = {
  149. tokenfield: ['foobar']
  150. }
  151. assert.deepEqual(params, test_params, 'migrated value type')
  152. })