form_code_editor.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. QUnit.test('form code editor', (assert) => {
  2. var done = assert.async(1)
  3. $('#forms').append('<hr><h1>form code editor</h1><form id="form1"></form>')
  4. var el = $('#form1')
  5. new App.ControllerForm({
  6. el: el,
  7. model: {
  8. configure_attributes: [
  9. { name: 'code', display: 'Code', tag: 'code_editor', null: false, default: '[]' }
  10. ]
  11. },
  12. });
  13. var params = App.ControllerForm.params(el)
  14. var test_params = {
  15. code: '[]'
  16. }
  17. assert.deepEqual(params, test_params, 'default param check')
  18. var initDelay = 350
  19. var updateDelay = 1000
  20. setTimeout(() => {
  21. var editor = el.find('.CodeMirror-code')
  22. // Combine all test examples in the same promise chain due to asynchronous behavior.
  23. new Promise((resolve) => {
  24. syn.click(editor[0]).type('[delete][delete]')
  25. setTimeout(() => { resolve() }, updateDelay)
  26. })
  27. .then(() => {
  28. var params = App.ControllerForm.params(el)
  29. var test_params = {
  30. code: ''
  31. }
  32. assert.deepEqual(params, test_params, 'code editor supports empty value')
  33. })
  34. .then(() => new Promise((resolve) => {
  35. syn.click(editor[0]).type('{}')
  36. setTimeout(() => { resolve() }, updateDelay)
  37. }))
  38. .then(() => {
  39. var params = App.ControllerForm.params(el)
  40. var test_params = {
  41. code: '{}'
  42. }
  43. assert.deepEqual(params, test_params, 'code editor value was updated')
  44. })
  45. .then(() => new Promise((resolve) => {
  46. App.Auth.login({
  47. data: {
  48. username: 'admin@example.com',
  49. password: 'test',
  50. },
  51. success: resolve,
  52. error: resolve
  53. })
  54. }))
  55. .then(() => new Promise((resolve) => {
  56. syn.click(editor[0]).type('[left][enter]"id[right]:[space]"::')
  57. setTimeout(() => { resolve() }, updateDelay)
  58. }))
  59. .then(() => {
  60. assert.ok($('.CodeMirror-hints'), 'shows full replacements list triggered by ::')
  61. })
  62. .then(() => new Promise((resolve) => {
  63. syn.click(editor[0]).type('ticket.id[enter]')
  64. setTimeout(() => { resolve() }, updateDelay)
  65. }))
  66. .then(() => {
  67. assert.notOk($('.CodeMirror-hints').length, 'hides replacements list after choosing')
  68. })
  69. .then(() => new Promise((resolve) => {
  70. syn.click(editor[0]).type('[right],[enter]"title[right]:[space]"#{')
  71. setTimeout(() => { resolve() }, updateDelay)
  72. }))
  73. .then(() => {
  74. assert.ok($('.CodeMirror-hints'), 'shows full replacements list triggered by #{')
  75. })
  76. .then(() => new Promise((resolve) => {
  77. syn.click(editor[0]).type('ticket.titl[enter]')
  78. setTimeout(() => { resolve() }, updateDelay)
  79. }))
  80. .then(() => {
  81. assert.notOk($('.CodeMirror-hints').length, 'hides replacements list after choosing')
  82. })
  83. .then(() => new Promise((resolve) => {
  84. syn.click(editor[0]).type('[right],[enter]"escalation[right]:[space]"#{')
  85. setTimeout(() => { resolve() }, updateDelay)
  86. }))
  87. .then(() => {
  88. assert.ok($('.CodeMirror-hints'), 'shows full replacements list triggered by #{')
  89. })
  90. .then(() => new Promise((resolve) => {
  91. syn.click(editor[0]).type('ticket.escalation_at')
  92. setTimeout(() => { resolve() }, updateDelay)
  93. }))
  94. .then(() => {
  95. assert.notOk($('.CodeMirror-hints').length, 'hides replacements list after only a single match remains')
  96. })
  97. .then(() => {
  98. var params = App.ControllerForm.params(el)
  99. var test_params = {
  100. code: '{\r\n "id": "#{ticket.id}",\r\n "title": "#{ticket.title}",\r\n "escalation": "#{ticket.escalation_at}"\r\n}',
  101. }
  102. assert.deepEqual(params, test_params, 'code editor value contains replacements')
  103. })
  104. .finally(done)
  105. }, initDelay)
  106. });