form_ticket_duplicate_detection.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. QUnit.test('form ticket duplicate detection', (assert) => {
  2. var done = assert.async(1)
  3. App.Config.set('ticket_duplicate_detection_title', 'foo')
  4. App.Config.set('ticket_duplicate_detection_body', 'bar')
  5. $('#forms').append('<hr><h1>form ticket duplicate detection</h1><form id="form1"></form>')
  6. var el = $('#form1')
  7. new App.ControllerForm({
  8. el: el,
  9. model: {
  10. configure_attributes: [
  11. { name: 'ticket_duplicate_detection', display: 'ticket_duplicate_detection', tag: 'ticket_duplicate_detection', null: true, label_class: 'hidden' }
  12. ]
  13. },
  14. });
  15. var payload = {
  16. count: 1,
  17. items: [
  18. [1, '27001', 'Welcome to Zammad!'],
  19. ],
  20. };
  21. var fieldElement = el.find('.duplicate-ticket-detection')
  22. assert.equal(typeof fieldElement.data('handleValue'), 'function', 'implements handleValue interface')
  23. assert.ok(fieldElement.hasClass('hide'), 'hides itself initially')
  24. fieldElement.data('handleValue')(payload)
  25. assert.notOk(fieldElement.hasClass('hide'), 'shows itself on payload with results')
  26. assert.equal(fieldElement.find('h4').text(), 'foo', 'displays configured title')
  27. assert.equal(fieldElement.find('p').text(), 'bar', 'displays configured message')
  28. assert.equal(fieldElement.find('a[href="#ticket/zoom/1"]').text().trim(), '27001', 'contains ticket link with ticket number')
  29. assert.ok(fieldElement.find('li:contains(Welcome to Zammad!)'), 'contains ticket title')
  30. payload = {
  31. count: 0,
  32. items: [],
  33. }
  34. fieldElement.data('handleValue')(payload)
  35. assert.ok(fieldElement.hasClass('hide'), 'hides itself on empty payload')
  36. payload = {
  37. count: 50,
  38. items: [],
  39. }
  40. fieldElement.data('handleValue')(payload)
  41. assert.notOk(fieldElement.hasClass('hide'), 'shows itself on payload with results but without tickets')
  42. assert.equal(fieldElement.find('h4').text(), 'foo', 'displays configured title')
  43. assert.equal(fieldElement.find('p').text(), 'bar', 'displays configured message')
  44. assert.notOk(fieldElement.find('ul').length, 'hides ticket list')
  45. const collapseDelay = 350
  46. // Combine all test examples in the same promise chain due to asynchronous behavior.
  47. return new Promise((resolve) => {
  48. payload = {
  49. count: 3,
  50. items: [
  51. [2, '27002', 'A Test Ticket'],
  52. [3, '27003', 'Another Test Ticket'],
  53. [4, '27004', 'Overflown Ticket'],
  54. ],
  55. }
  56. fieldElement.data('handleValue')(payload)
  57. setTimeout(() => { resolve() }, collapseDelay)
  58. })
  59. .then(() => new Promise((resolve) => {
  60. assert.ok(fieldElement.find('li:contains(A Test Ticket):visible').length, 'shows first ticket')
  61. assert.ok(fieldElement.find('li:contains(Another Test Ticket):visible').length, 'shows second ticket')
  62. assert.notOk(fieldElement.find('li:contains(Overflown Ticket):visible').length, 'hides third ticket')
  63. assert.equal(fieldElement.find('.btn').text(), 'See more', 'shows see more button')
  64. fieldElement.find('.btn').click()
  65. setTimeout(() => { resolve() }, collapseDelay)
  66. }))
  67. .then(() => new Promise((resolve) => {
  68. assert.ok(fieldElement.find('li:contains(Overflown Ticket):visible').length, 'shows third ticket')
  69. assert.equal(fieldElement.find('.btn').text(), 'See less', 'shows see less button')
  70. fieldElement.find('.btn').click()
  71. setTimeout(() => { resolve() }, collapseDelay)
  72. }))
  73. .then(() => {
  74. assert.notOk(fieldElement.find('li:contains(Overflown Ticket):visible').length, 'hides third ticket')
  75. assert.equal(fieldElement.find('.btn').text(), 'See more', 'shows see more button')
  76. })
  77. .finally(done)
  78. });