form_autocompletion_ajax.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. function check_ajax_field(field, value, count, callback, assert, waitTotal = 30000, wait = 0) {
  2. $elementInput = $('[name="' + field + '"].js-shadow + .js-input')
  3. if ($elementInput.val() != value) {
  4. $elementInput.trigger('focus').val(value).trigger('input')
  5. }
  6. var $element = $('[name="' + field + '"]').closest('.searchableSelect').find('.js-optionsList')
  7. var entries = $element.find('li:not(.is-hidden)').length
  8. var match = entries == count
  9. if (match || wait >= waitTotal) {
  10. assert.equal(entries, count, 'search result found for email address ' + value)
  11. callback()
  12. return
  13. }
  14. wait += 100
  15. if (wait % 3000 == 0) {
  16. assert.ok(true, 'check_ajax_field for ' + field + ' waiting ' + wait)
  17. }
  18. setTimeout(function() {
  19. check_ajax_field(field, value, count, callback, assert, waitTotal, wait)
  20. }, 100)
  21. }
  22. QUnit.test( "autocompletion_ajax check", assert => {
  23. var done = assert.async(1)
  24. $('#forms').append('<hr><h1>autocompletion_ajax check</h1><form id="form1"></form>')
  25. var el = $('#form1')
  26. new App.ControllerForm({
  27. el: el,
  28. model: {
  29. configure_attributes: [
  30. {
  31. name: 'autocompletion_ajax1',
  32. display: 'AutocompletionAjax1',
  33. tag: 'autocompletion_ajax',
  34. relation: 'User'
  35. },
  36. ]
  37. },
  38. autofocus: true
  39. })
  40. new Promise( (resolve, reject) => {
  41. App.Auth.login({
  42. data: {
  43. username: 'admin@example.com',
  44. password: 'test',
  45. },
  46. success: resolve,
  47. error: resolve
  48. });
  49. })
  50. .then( function() {
  51. return new Promise( (resolve, reject) => {
  52. assert.notEqual(App.Session.get(), undefined, 'User is logged in so the api requests will work')
  53. check_ajax_field('autocompletion_ajax1', 'admin@example.com', 1, resolve, assert)
  54. })
  55. })
  56. .then( function() {
  57. return new Promise( (resolve, reject) => {
  58. check_ajax_field('autocompletion_ajax1', 'xxx@example.com', 0, resolve, assert)
  59. })
  60. })
  61. .finally(done)
  62. })