autocomplete.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. (function (root, factory) {
  2. define([
  3. "jasmine",
  4. "mock",
  5. "test-utils"
  6. ], factory);
  7. } (this, function (jasmine, mock, test_utils) {
  8. "use strict";
  9. const _ = converse.env._;
  10. const $iq = converse.env.$iq;
  11. const $msg = converse.env.$msg;
  12. const $pres = converse.env.$pres;
  13. const Strophe = converse.env.Strophe;
  14. const u = converse.env.utils;
  15. describe("The nickname autocomplete feature", function () {
  16. it("shows all autocompletion options when the user presses @",
  17. mock.initConverseWithPromises(
  18. null, ['rosterGroupsFetched'], {},
  19. function (done, _converse) {
  20. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'tom')
  21. .then(() => {
  22. const view = _converse.chatboxviews.get('lounge@localhost');
  23. ['dick', 'harry'].forEach((nick) => {
  24. _converse.connection._dataRecv(test_utils.createRequest(
  25. $pres({
  26. 'to': 'tom@localhost/resource',
  27. 'from': `lounge@localhost/${nick}`
  28. })
  29. .c('x', {xmlns: Strophe.NS.MUC_USER})
  30. .c('item', {
  31. 'affiliation': 'none',
  32. 'jid': `${nick}@localhost/resource`,
  33. 'role': 'participant'
  34. })));
  35. });
  36. // Test that pressing @ brings up all options
  37. const textarea = view.el.querySelector('textarea.chat-textarea');
  38. const at_event = {
  39. 'target': textarea,
  40. 'preventDefault': _.noop,
  41. 'stopPropagation': _.noop,
  42. 'keyCode': 50
  43. };
  44. view.keyPressed(at_event);
  45. textarea.value = '@';
  46. view.keyUp(at_event);
  47. expect(view.el.querySelectorAll('.suggestion-box__results li').length).toBe(3);
  48. expect(view.el.querySelector('.suggestion-box__results li[aria-selected="true"]').textContent).toBe('tom');
  49. expect(view.el.querySelector('.suggestion-box__results li:first-child').textContent).toBe('tom');
  50. expect(view.el.querySelector('.suggestion-box__results li:nth-child(2)').textContent).toBe('dick');
  51. expect(view.el.querySelector('.suggestion-box__results li:nth-child(3)').textContent).toBe('harry');
  52. done();
  53. }).catch(_.partial(console.error, _));
  54. }));
  55. it("autocompletes when the user presses tab",
  56. mock.initConverseWithPromises(
  57. null, ['rosterGroupsFetched'], {},
  58. function (done, _converse) {
  59. test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy')
  60. .then(() => {
  61. const view = _converse.chatboxviews.get('lounge@localhost');
  62. expect(view.model.occupants.length).toBe(1);
  63. let presence = $pres({
  64. 'to': 'dummy@localhost/resource',
  65. 'from': 'lounge@localhost/some1'
  66. })
  67. .c('x', {xmlns: Strophe.NS.MUC_USER})
  68. .c('item', {
  69. 'affiliation': 'none',
  70. 'jid': 'some1@localhost/resource',
  71. 'role': 'participant'
  72. });
  73. _converse.connection._dataRecv(test_utils.createRequest(presence));
  74. expect(view.model.occupants.length).toBe(2);
  75. const textarea = view.el.querySelector('textarea.chat-textarea');
  76. textarea.value = "hello som";
  77. // Press tab
  78. const tab_event = {
  79. 'target': textarea,
  80. 'preventDefault': _.noop,
  81. 'stopPropagation': _.noop,
  82. 'keyCode': 9
  83. }
  84. view.keyPressed(tab_event);
  85. view.keyUp(tab_event);
  86. expect(view.el.querySelector('.suggestion-box__results').hidden).toBeFalsy();
  87. expect(view.el.querySelectorAll('.suggestion-box__results li').length).toBe(1);
  88. expect(view.el.querySelector('.suggestion-box__results li').textContent).toBe('some1');
  89. const backspace_event = {
  90. 'target': textarea,
  91. 'preventDefault': _.noop,
  92. 'keyCode': 8
  93. }
  94. for (var i=0; i<3; i++) {
  95. // Press backspace 3 times to remove "som"
  96. view.keyPressed(backspace_event);
  97. textarea.value = textarea.value.slice(0, textarea.value.length-1)
  98. view.keyUp(backspace_event);
  99. }
  100. expect(view.el.querySelector('.suggestion-box__results').hidden).toBeTruthy();
  101. presence = $pres({
  102. 'to': 'dummy@localhost/resource',
  103. 'from': 'lounge@localhost/some2'
  104. })
  105. .c('x', {xmlns: Strophe.NS.MUC_USER})
  106. .c('item', {
  107. 'affiliation': 'none',
  108. 'jid': 'some2@localhost/resource',
  109. 'role': 'participant'
  110. });
  111. _converse.connection._dataRecv(test_utils.createRequest(presence));
  112. textarea.value = "hello s s";
  113. view.keyPressed(tab_event);
  114. view.keyUp(tab_event);
  115. expect(view.el.querySelector('.suggestion-box__results').hidden).toBeFalsy();
  116. expect(view.el.querySelectorAll('.suggestion-box__results li').length).toBe(2);
  117. const up_arrow_event = {
  118. 'target': textarea,
  119. 'preventDefault': () => (up_arrow_event.defaultPrevented = true),
  120. 'stopPropagation': _.noop,
  121. 'keyCode': 38
  122. }
  123. view.keyPressed(up_arrow_event);
  124. view.keyUp(up_arrow_event);
  125. expect(view.el.querySelectorAll('.suggestion-box__results li').length).toBe(2);
  126. expect(view.el.querySelector('.suggestion-box__results li[aria-selected="false"]').textContent).toBe('some1');
  127. expect(view.el.querySelector('.suggestion-box__results li[aria-selected="true"]').textContent).toBe('some2');
  128. view.keyPressed({
  129. 'target': textarea,
  130. 'preventDefault': _.noop,
  131. 'stopPropagation': _.noop,
  132. 'keyCode': 13 // Enter
  133. });
  134. expect(textarea.value).toBe('hello s @some2 ');
  135. // Test that pressing tab twice selects
  136. presence = $pres({
  137. 'to': 'dummy@localhost/resource',
  138. 'from': 'lounge@localhost/z3r0'
  139. })
  140. .c('x', {xmlns: Strophe.NS.MUC_USER})
  141. .c('item', {
  142. 'affiliation': 'none',
  143. 'jid': 'z3r0@localhost/resource',
  144. 'role': 'participant'
  145. });
  146. _converse.connection._dataRecv(test_utils.createRequest(presence));
  147. textarea.value = "hello z";
  148. view.keyPressed(tab_event);
  149. view.keyUp(tab_event);
  150. view.keyPressed(tab_event);
  151. view.keyUp(tab_event);
  152. expect(textarea.value).toBe('hello @z3r0 ');
  153. done();
  154. }).catch(_.partial(console.error, _));
  155. }));
  156. });
  157. }));