widget.spec.ts 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. // Copyright (c) Jupyter Development Team.
  2. // Distributed under the terms of the Modified BSD License.
  3. import { CodeEditor, CodeEditorWrapper } from '@jupyterlab/codeeditor';
  4. import { CodeMirrorEditor } from '@jupyterlab/codemirror';
  5. import {
  6. Completer,
  7. CompleterModel,
  8. CompletionHandler
  9. } from '@jupyterlab/completer';
  10. import { framePromise, sleep } from '@jupyterlab/testutils';
  11. import { Message, MessageLoop } from '@lumino/messaging';
  12. import { Panel, Widget } from '@lumino/widgets';
  13. import { simulate } from 'simulate-event';
  14. const TEST_ITEM_CLASS = 'jp-TestItem';
  15. const TEST_DOC_CLASS = 'jp-TestDoc';
  16. const ITEM_CLASS = 'jp-Completer-item';
  17. const DOC_PANEL_CLASS = 'jp-Completer-docpanel';
  18. const ACTIVE_CLASS = 'jp-mod-active';
  19. function createEditorWidget(): CodeEditorWrapper {
  20. const model = new CodeEditor.Model();
  21. const factory = (options: CodeEditor.IOptions) => {
  22. return new CodeMirrorEditor(options);
  23. };
  24. return new CodeEditorWrapper({ factory, model });
  25. }
  26. class CustomRenderer extends Completer.Renderer {
  27. createCompletionItemNode(
  28. item: CompletionHandler.ICompletionItem,
  29. orderedTypes: string[]
  30. ): HTMLLIElement {
  31. let li = super.createCompletionItemNode!(item, orderedTypes);
  32. li.classList.add(TEST_ITEM_CLASS);
  33. return li;
  34. }
  35. createItemNode(
  36. item: Completer.IItem,
  37. typeMap: Completer.TypeMap,
  38. orderedTypes: string[]
  39. ): HTMLLIElement {
  40. const li = super.createItemNode(item, typeMap, orderedTypes);
  41. li.classList.add(TEST_ITEM_CLASS);
  42. return li;
  43. }
  44. createDocumentationNode(
  45. item: CompletionHandler.ICompletionItem
  46. ): HTMLElement {
  47. const element = super.createDocumentationNode!(item);
  48. element.classList.add(TEST_DOC_CLASS);
  49. return element;
  50. }
  51. }
  52. class LogWidget extends Completer {
  53. events: string[] = [];
  54. methods: string[] = [];
  55. dispose(): void {
  56. super.dispose();
  57. this.events.length = 0;
  58. }
  59. handleEvent(event: Event): void {
  60. this.events.push(event.type);
  61. super.handleEvent(event);
  62. }
  63. protected onUpdateRequest(msg: Message): void {
  64. super.onUpdateRequest(msg);
  65. this.methods.push('onUpdateRequest');
  66. }
  67. }
  68. describe('completer/widget', () => {
  69. describe('Completer', () => {
  70. describe('#constructor()', () => {
  71. it('should create a completer widget', () => {
  72. const widget = new Completer({ editor: null });
  73. expect(widget).toBeInstanceOf(Completer);
  74. expect(Array.from(widget.node.classList)).toEqual(
  75. expect.arrayContaining(['jp-Completer'])
  76. );
  77. });
  78. it('should accept options with a model', () => {
  79. const options: Completer.IOptions = {
  80. editor: null,
  81. model: new CompleterModel()
  82. };
  83. const widget = new Completer(options);
  84. expect(widget).toBeInstanceOf(Completer);
  85. expect(widget.model).toBe(options.model);
  86. });
  87. it('should accept options with a renderer', () => {
  88. const options: Completer.IOptions = {
  89. editor: null,
  90. model: new CompleterModel(),
  91. renderer: new CustomRenderer()
  92. };
  93. options.model!.setOptions(['foo', 'bar']);
  94. const widget = new Completer(options);
  95. expect(widget).toBeInstanceOf(Completer);
  96. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  97. const items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  98. expect(items).toHaveLength(2);
  99. expect(Array.from(items[0].classList)).toEqual(
  100. expect.arrayContaining([TEST_ITEM_CLASS])
  101. );
  102. });
  103. it('should accept completion items with a renderer', () => {
  104. let options: Completer.IOptions = {
  105. editor: null,
  106. model: new CompleterModel(),
  107. renderer: new CustomRenderer()
  108. };
  109. options.model!.setCompletionItems!([
  110. { label: 'foo', documentation: 'foo does bar' },
  111. { label: 'bar' }
  112. ]);
  113. let widget = new Completer(options);
  114. expect(widget).toBeInstanceOf(Completer);
  115. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  116. let items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  117. expect(items).toHaveLength(2);
  118. expect(Array.from(items[0].classList)).toEqual(
  119. expect.arrayContaining([TEST_ITEM_CLASS])
  120. );
  121. let panel = widget.node.querySelector(`.${DOC_PANEL_CLASS}`)!;
  122. expect(panel.children).toHaveLength(1);
  123. expect(Array.from(panel.firstElementChild!.classList)).toEqual(
  124. expect.arrayContaining([TEST_DOC_CLASS])
  125. );
  126. });
  127. });
  128. describe('#selected', () => {
  129. it('should emit a signal when an item is selected', () => {
  130. const anchor = createEditorWidget();
  131. const options: Completer.IOptions = {
  132. editor: anchor.editor,
  133. model: new CompleterModel()
  134. };
  135. let value = '';
  136. const listener = (sender: any, selected: string) => {
  137. value = selected;
  138. };
  139. options.model!.setOptions(['foo', 'bar']);
  140. Widget.attach(anchor, document.body);
  141. const widget = new Completer(options);
  142. widget.selected.connect(listener);
  143. Widget.attach(widget, document.body);
  144. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  145. expect(value).toBe('');
  146. widget.selectActive();
  147. expect(value).toBe('foo');
  148. widget.dispose();
  149. anchor.dispose();
  150. });
  151. describe('#selected with completion items', () => {
  152. it('should emit the insert text if it is present', () => {
  153. let anchor = createEditorWidget();
  154. let options: Completer.IOptions = {
  155. editor: anchor.editor,
  156. model: new CompleterModel()
  157. };
  158. let value = '';
  159. let listener = (sender: any, selected: string) => {
  160. value = selected;
  161. };
  162. options.model!.setCompletionItems!([
  163. { label: 'foo', insertText: 'bar' },
  164. { label: 'baz' }
  165. ]);
  166. Widget.attach(anchor, document.body);
  167. let widget = new Completer(options);
  168. widget.selected.connect(listener);
  169. Widget.attach(widget, document.body);
  170. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  171. expect(value).toBe('');
  172. widget.selectActive();
  173. expect(value).toBe('bar');
  174. widget.dispose();
  175. anchor.dispose();
  176. });
  177. it('should emit the label if insert text is not present', () => {
  178. let anchor = createEditorWidget();
  179. let options: Completer.IOptions = {
  180. editor: anchor.editor,
  181. model: new CompleterModel()
  182. };
  183. let value = '';
  184. let listener = (sender: any, selected: string) => {
  185. value = selected;
  186. };
  187. options.model!.setCompletionItems!([
  188. { label: 'foo' },
  189. { label: 'baz' }
  190. ]);
  191. Widget.attach(anchor, document.body);
  192. let widget = new Completer(options);
  193. widget.selected.connect(listener);
  194. Widget.attach(widget, document.body);
  195. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  196. expect(value).toBe('');
  197. widget.selectActive();
  198. expect(value).toBe('foo');
  199. widget.dispose();
  200. anchor.dispose();
  201. });
  202. });
  203. });
  204. describe('#visibilityChanged', () => {
  205. it('should emit a signal when completer visibility changes', async () => {
  206. const panel = new Panel();
  207. const code = createEditorWidget();
  208. const editor = code.editor;
  209. const model = new CompleterModel();
  210. let called = false;
  211. editor.model.value.text = 'a';
  212. panel.node.style.position = 'absolute';
  213. panel.node.style.top = '0px';
  214. panel.node.style.left = '0px';
  215. panel.node.style.height = '1000px';
  216. code.node.style.height = '900px';
  217. panel.addWidget(code);
  218. Widget.attach(panel, document.body);
  219. panel.node.scrollTop = 0;
  220. document.body.scrollTop = 0;
  221. const position = code.editor.getPositionAt(1)!;
  222. editor.setCursorPosition(position);
  223. const request: Completer.ITextState = {
  224. column: position.column,
  225. lineHeight: editor.lineHeight,
  226. charWidth: editor.charWidth,
  227. line: position.line,
  228. text: 'a'
  229. };
  230. model.original = request;
  231. model.cursor = { start: 0, end: 1 };
  232. model.setOptions(['abc', 'abd', 'abe', 'abi']);
  233. const widget = new Completer({ model, editor: code.editor });
  234. widget.hide();
  235. expect(called).toBe(false);
  236. widget.visibilityChanged.connect(() => {
  237. called = true;
  238. });
  239. Widget.attach(widget, document.body);
  240. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  241. await framePromise();
  242. expect(called).toBe(true);
  243. widget.dispose();
  244. code.dispose();
  245. panel.dispose();
  246. });
  247. it('should emit a signal when completion items completer visibility changes', async () => {
  248. let panel = new Panel();
  249. let code = createEditorWidget();
  250. let editor = code.editor;
  251. let model = new CompleterModel();
  252. let called = false;
  253. editor.model.value.text = 'a';
  254. panel.node.style.position = 'absolute';
  255. panel.node.style.top = '0px';
  256. panel.node.style.left = '0px';
  257. panel.node.style.height = '1000px';
  258. code.node.style.height = '900px';
  259. panel.addWidget(code);
  260. Widget.attach(panel, document.body);
  261. panel.node.scrollTop = 0;
  262. document.body.scrollTop = 0;
  263. let position = code.editor.getPositionAt(1)!;
  264. editor.setCursorPosition(position);
  265. let request: Completer.ITextState = {
  266. column: position.column,
  267. lineHeight: editor.lineHeight,
  268. charWidth: editor.charWidth,
  269. line: position.line,
  270. text: 'a'
  271. };
  272. model.original = request;
  273. model.cursor = { start: 0, end: 1 };
  274. model.setCompletionItems!([
  275. { label: 'abc' },
  276. { label: 'abd' },
  277. { label: 'abe' },
  278. { label: 'abi' }
  279. ]);
  280. let widget = new Completer({ model, editor: code.editor });
  281. widget.hide();
  282. expect(called).toBe(false);
  283. widget.visibilityChanged.connect(() => {
  284. called = true;
  285. });
  286. Widget.attach(widget, document.body);
  287. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  288. await framePromise();
  289. expect(called).toBe(true);
  290. widget.dispose();
  291. code.dispose();
  292. panel.dispose();
  293. });
  294. });
  295. describe('#model', () => {
  296. it('should default to null', () => {
  297. const widget = new Completer({ editor: null });
  298. expect(widget.model).toBeNull();
  299. });
  300. it('should be settable', () => {
  301. const widget = new Completer({ editor: null });
  302. expect(widget.model).toBeNull();
  303. widget.model = new CompleterModel();
  304. expect(widget.model).toBeInstanceOf(CompleterModel);
  305. });
  306. it('should be safe to set multiple times', () => {
  307. const model = new CompleterModel();
  308. const widget = new Completer({ editor: null });
  309. widget.model = model;
  310. widget.model = model;
  311. expect(widget.model).toBe(model);
  312. });
  313. it('should be safe to reset', () => {
  314. const model = new CompleterModel();
  315. const widget = new Completer({
  316. editor: null,
  317. model: new CompleterModel()
  318. });
  319. expect(widget.model).not.toBe(model);
  320. widget.model = model;
  321. expect(widget.model).toBe(model);
  322. });
  323. });
  324. describe('#editor', () => {
  325. it('should default to null', () => {
  326. const widget = new Completer({ editor: null });
  327. expect(widget.editor).toBeNull();
  328. });
  329. it('should be settable', () => {
  330. const anchor = createEditorWidget();
  331. const widget = new Completer({ editor: null });
  332. expect(widget.editor).toBeNull();
  333. widget.editor = anchor.editor;
  334. expect(widget.editor).toBeTruthy();
  335. });
  336. });
  337. describe('#dispose()', () => {
  338. it('should dispose of the resources held by the widget', () => {
  339. const widget = new Completer({ editor: null });
  340. widget.dispose();
  341. expect(widget.isDisposed).toBe(true);
  342. });
  343. it('should be safe to call multiple times', () => {
  344. const widget = new Completer({ editor: null });
  345. widget.dispose();
  346. widget.dispose();
  347. expect(widget.isDisposed).toBe(true);
  348. });
  349. });
  350. describe('#reset()', () => {
  351. it('should reset the completer widget', () => {
  352. const anchor = createEditorWidget();
  353. const model = new CompleterModel();
  354. const options: Completer.IOptions = {
  355. editor: anchor.editor,
  356. model
  357. };
  358. model.setOptions(['foo', 'bar'], { foo: 'instance', bar: 'function' });
  359. Widget.attach(anchor, document.body);
  360. const widget = new Completer(options);
  361. Widget.attach(widget, document.body);
  362. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  363. expect(widget.isHidden).toBe(false);
  364. expect(model.options).toBeTruthy();
  365. widget.reset();
  366. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  367. expect(widget.isHidden).toBe(true);
  368. expect(model.options().next()).toBeUndefined();
  369. widget.dispose();
  370. anchor.dispose();
  371. });
  372. it('should reset the completer widget and its completion items', () => {
  373. let anchor = createEditorWidget();
  374. let model = new CompleterModel();
  375. let options: Completer.IOptions = {
  376. editor: anchor.editor,
  377. model
  378. };
  379. model.setCompletionItems!([{ label: 'foo' }, { label: 'bar' }]);
  380. Widget.attach(anchor, document.body);
  381. let widget = new Completer(options);
  382. Widget.attach(widget, document.body);
  383. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  384. expect(widget.isHidden).toBe(false);
  385. expect(model.completionItems!()).toEqual([
  386. { label: 'foo' },
  387. { label: 'bar' }
  388. ]);
  389. widget.reset();
  390. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  391. expect(widget.isHidden).toBe(true);
  392. expect(model.completionItems!()).toEqual([]);
  393. widget.dispose();
  394. anchor.dispose();
  395. });
  396. });
  397. describe('#handleEvent()', () => {
  398. it('should handle document keydown, mousedown, and scroll events', () => {
  399. const anchor = createEditorWidget();
  400. const widget = new LogWidget({ editor: anchor.editor });
  401. Widget.attach(anchor, document.body);
  402. Widget.attach(widget, document.body);
  403. ['keydown', 'mousedown', 'scroll'].forEach(type => {
  404. simulate(document.body, type);
  405. expect(widget.events).toEqual(expect.arrayContaining([type]));
  406. });
  407. widget.dispose();
  408. anchor.dispose();
  409. });
  410. describe('keydown', () => {
  411. it('should reset if keydown is outside anchor', () => {
  412. const model = new CompleterModel();
  413. const anchor = createEditorWidget();
  414. const options: Completer.IOptions = {
  415. editor: anchor.editor,
  416. model
  417. };
  418. model.setOptions(['foo', 'bar'], {
  419. foo: 'instance',
  420. bar: 'function'
  421. });
  422. Widget.attach(anchor, document.body);
  423. const widget = new Completer(options);
  424. Widget.attach(widget, document.body);
  425. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  426. expect(widget.isHidden).toBe(false);
  427. expect(model.options).toBeTruthy();
  428. simulate(document.body, 'keydown', { keyCode: 70 }); // F
  429. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  430. expect(widget.isHidden).toBe(true);
  431. expect(model.options().next()).toBeUndefined();
  432. widget.dispose();
  433. anchor.dispose();
  434. });
  435. it('should reset completion items if keydown is outside anchor', () => {
  436. let model = new CompleterModel();
  437. let anchor = createEditorWidget();
  438. let options: Completer.IOptions = {
  439. editor: anchor.editor,
  440. model
  441. };
  442. model.setCompletionItems!([{ label: 'foo' }, { label: 'bar' }]);
  443. Widget.attach(anchor, document.body);
  444. let widget = new Completer(options);
  445. Widget.attach(widget, document.body);
  446. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  447. expect(widget.isHidden).toBe(false);
  448. expect(model.completionItems!()).toEqual([
  449. { label: 'foo' },
  450. { label: 'bar' }
  451. ]);
  452. simulate(document.body, 'keydown', { keyCode: 70 }); // F
  453. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  454. expect(widget.isHidden).toBe(true);
  455. expect(model.completionItems!()).toEqual([]);
  456. widget.dispose();
  457. anchor.dispose();
  458. });
  459. it('should select the item below and wrap to top past last (arrow keys)', () => {
  460. const anchor = createEditorWidget();
  461. const model = new CompleterModel();
  462. const options: Completer.IOptions = {
  463. editor: anchor.editor,
  464. model
  465. };
  466. model.setOptions(['foo', 'bar', 'baz'], {
  467. foo: 'instance',
  468. bar: 'function'
  469. });
  470. Widget.attach(anchor, document.body);
  471. const widget = new Completer(options);
  472. const target = document.createElement('div');
  473. anchor.node.appendChild(target);
  474. Widget.attach(widget, document.body);
  475. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  476. const items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  477. expect(Array.from(items[0].classList)).toEqual(
  478. expect.arrayContaining([ACTIVE_CLASS])
  479. );
  480. expect(Array.from(items[1].classList)).toEqual(
  481. expect.not.arrayContaining([ACTIVE_CLASS])
  482. );
  483. expect(Array.from(items[2].classList)).toEqual(
  484. expect.not.arrayContaining([ACTIVE_CLASS])
  485. );
  486. simulate(target, 'keydown', { keyCode: 40 }); // Down
  487. expect(Array.from(items[0].classList)).toEqual(
  488. expect.not.arrayContaining([ACTIVE_CLASS])
  489. );
  490. expect(Array.from(items[1].classList)).toEqual(
  491. expect.arrayContaining([ACTIVE_CLASS])
  492. );
  493. expect(Array.from(items[2].classList)).toEqual(
  494. expect.not.arrayContaining([ACTIVE_CLASS])
  495. );
  496. simulate(target, 'keydown', { keyCode: 40 }); // Down
  497. expect(Array.from(items[0].classList)).toEqual(
  498. expect.not.arrayContaining([ACTIVE_CLASS])
  499. );
  500. expect(Array.from(items[1].classList)).toEqual(
  501. expect.not.arrayContaining([ACTIVE_CLASS])
  502. );
  503. expect(Array.from(items[2].classList)).toEqual(
  504. expect.arrayContaining([ACTIVE_CLASS])
  505. );
  506. simulate(target, 'keydown', { keyCode: 40 }); // Down
  507. expect(Array.from(items[0].classList)).toEqual(
  508. expect.arrayContaining([ACTIVE_CLASS])
  509. );
  510. expect(Array.from(items[1].classList)).toEqual(
  511. expect.not.arrayContaining([ACTIVE_CLASS])
  512. );
  513. expect(Array.from(items[2].classList)).toEqual(
  514. expect.not.arrayContaining([ACTIVE_CLASS])
  515. );
  516. widget.dispose();
  517. anchor.dispose();
  518. });
  519. it('should select the item below and wrap to top past last (tab)', () => {
  520. const anchor = createEditorWidget();
  521. const model = new CompleterModel();
  522. const options: Completer.IOptions = {
  523. editor: anchor.editor,
  524. model
  525. };
  526. model.setOptions(['foo', 'bar', 'baz'], {
  527. foo: 'instance',
  528. bar: 'function'
  529. });
  530. Widget.attach(anchor, document.body);
  531. const widget = new Completer(options);
  532. const target = document.createElement('div');
  533. anchor.node.appendChild(target);
  534. Widget.attach(widget, document.body);
  535. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  536. const items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  537. expect(Array.from(items[0].classList)).toEqual(
  538. expect.arrayContaining([ACTIVE_CLASS])
  539. );
  540. expect(Array.from(items[1].classList)).toEqual(
  541. expect.not.arrayContaining([ACTIVE_CLASS])
  542. );
  543. expect(Array.from(items[2].classList)).toEqual(
  544. expect.not.arrayContaining([ACTIVE_CLASS])
  545. );
  546. simulate(target, 'keydown', { keyCode: 9 }); // Tab
  547. expect(Array.from(items[0].classList)).toEqual(
  548. expect.not.arrayContaining([ACTIVE_CLASS])
  549. );
  550. expect(Array.from(items[1].classList)).toEqual(
  551. expect.arrayContaining([ACTIVE_CLASS])
  552. );
  553. expect(Array.from(items[2].classList)).toEqual(
  554. expect.not.arrayContaining([ACTIVE_CLASS])
  555. );
  556. simulate(target, 'keydown', { keyCode: 9 }); // Tab
  557. expect(Array.from(items[0].classList)).toEqual(
  558. expect.not.arrayContaining([ACTIVE_CLASS])
  559. );
  560. expect(Array.from(items[1].classList)).toEqual(
  561. expect.not.arrayContaining([ACTIVE_CLASS])
  562. );
  563. expect(Array.from(items[2].classList)).toEqual(
  564. expect.arrayContaining([ACTIVE_CLASS])
  565. );
  566. simulate(target, 'keydown', { keyCode: 9 }); // Tab
  567. expect(Array.from(items[0].classList)).toEqual(
  568. expect.arrayContaining([ACTIVE_CLASS])
  569. );
  570. expect(Array.from(items[1].classList)).toEqual(
  571. expect.not.arrayContaining([ACTIVE_CLASS])
  572. );
  573. expect(Array.from(items[2].classList)).toEqual(
  574. expect.not.arrayContaining([ACTIVE_CLASS])
  575. );
  576. widget.dispose();
  577. anchor.dispose();
  578. });
  579. it('should select the completion item below and wrap to top past last (arrow keys)', () => {
  580. let anchor = createEditorWidget();
  581. let model = new CompleterModel();
  582. let options: Completer.IOptions = {
  583. editor: anchor.editor,
  584. model
  585. };
  586. model.setCompletionItems!([
  587. { label: 'foo' },
  588. { label: 'bar' },
  589. { label: 'baz' }
  590. ]);
  591. Widget.attach(anchor, document.body);
  592. let widget = new Completer(options);
  593. let target = document.createElement('div');
  594. anchor.node.appendChild(target);
  595. Widget.attach(widget, document.body);
  596. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  597. let items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  598. expect(Array.from(items[0].classList)).toEqual(
  599. expect.arrayContaining([ACTIVE_CLASS])
  600. );
  601. expect(Array.from(items[1].classList)).toEqual(
  602. expect.not.arrayContaining([ACTIVE_CLASS])
  603. );
  604. expect(Array.from(items[2].classList)).toEqual(
  605. expect.not.arrayContaining([ACTIVE_CLASS])
  606. );
  607. simulate(target, 'keydown', { keyCode: 40 }); // Down
  608. expect(Array.from(items[0].classList)).toEqual(
  609. expect.not.arrayContaining([ACTIVE_CLASS])
  610. );
  611. expect(Array.from(items[1].classList)).toEqual(
  612. expect.arrayContaining([ACTIVE_CLASS])
  613. );
  614. expect(Array.from(items[2].classList)).toEqual(
  615. expect.not.arrayContaining([ACTIVE_CLASS])
  616. );
  617. simulate(target, 'keydown', { keyCode: 40 }); // Down
  618. expect(Array.from(items[0].classList)).toEqual(
  619. expect.not.arrayContaining([ACTIVE_CLASS])
  620. );
  621. expect(Array.from(items[1].classList)).toEqual(
  622. expect.not.arrayContaining([ACTIVE_CLASS])
  623. );
  624. expect(Array.from(items[2].classList)).toEqual(
  625. expect.arrayContaining([ACTIVE_CLASS])
  626. );
  627. simulate(target, 'keydown', { keyCode: 40 }); // Down
  628. expect(Array.from(items[0].classList)).toEqual(
  629. expect.arrayContaining([ACTIVE_CLASS])
  630. );
  631. expect(Array.from(items[1].classList)).toEqual(
  632. expect.not.arrayContaining([ACTIVE_CLASS])
  633. );
  634. expect(Array.from(items[2].classList)).toEqual(
  635. expect.not.arrayContaining([ACTIVE_CLASS])
  636. );
  637. widget.dispose();
  638. anchor.dispose();
  639. });
  640. it('should select the completion item below and wrap to top past last (tab)', () => {
  641. let anchor = createEditorWidget();
  642. let model = new CompleterModel();
  643. let options: Completer.IOptions = {
  644. editor: anchor.editor,
  645. model
  646. };
  647. model.setCompletionItems!([
  648. { label: 'foo' },
  649. { label: 'bar' },
  650. { label: 'baz' }
  651. ]);
  652. Widget.attach(anchor, document.body);
  653. let widget = new Completer(options);
  654. let target = document.createElement('div');
  655. anchor.node.appendChild(target);
  656. Widget.attach(widget, document.body);
  657. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  658. let items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  659. expect(Array.from(items[0].classList)).toEqual(
  660. expect.arrayContaining([ACTIVE_CLASS])
  661. );
  662. expect(Array.from(items[1].classList)).toEqual(
  663. expect.not.arrayContaining([ACTIVE_CLASS])
  664. );
  665. expect(Array.from(items[2].classList)).toEqual(
  666. expect.not.arrayContaining([ACTIVE_CLASS])
  667. );
  668. simulate(target, 'keydown', { keyCode: 9 }); // Tab
  669. expect(Array.from(items[0].classList)).toEqual(
  670. expect.not.arrayContaining([ACTIVE_CLASS])
  671. );
  672. expect(Array.from(items[1].classList)).toEqual(
  673. expect.arrayContaining([ACTIVE_CLASS])
  674. );
  675. expect(Array.from(items[2].classList)).toEqual(
  676. expect.not.arrayContaining([ACTIVE_CLASS])
  677. );
  678. simulate(target, 'keydown', { keyCode: 9 }); // Tab
  679. expect(Array.from(items[0].classList)).toEqual(
  680. expect.not.arrayContaining([ACTIVE_CLASS])
  681. );
  682. expect(Array.from(items[1].classList)).toEqual(
  683. expect.not.arrayContaining([ACTIVE_CLASS])
  684. );
  685. expect(Array.from(items[2].classList)).toEqual(
  686. expect.arrayContaining([ACTIVE_CLASS])
  687. );
  688. simulate(target, 'keydown', { keyCode: 9 }); // Tab
  689. expect(Array.from(items[0].classList)).toEqual(
  690. expect.arrayContaining([ACTIVE_CLASS])
  691. );
  692. expect(Array.from(items[1].classList)).toEqual(
  693. expect.not.arrayContaining([ACTIVE_CLASS])
  694. );
  695. expect(Array.from(items[2].classList)).toEqual(
  696. expect.not.arrayContaining([ACTIVE_CLASS])
  697. );
  698. widget.dispose();
  699. anchor.dispose();
  700. });
  701. it('should select the item above and wrap to bottom past first (arrow keys)', () => {
  702. const anchor = createEditorWidget();
  703. const model = new CompleterModel();
  704. const options: Completer.IOptions = {
  705. editor: anchor.editor,
  706. model
  707. };
  708. model.setOptions(['foo', 'bar', 'baz'], {
  709. foo: 'instance',
  710. bar: 'function'
  711. });
  712. Widget.attach(anchor, document.body);
  713. const widget = new Completer(options);
  714. Widget.attach(widget, document.body);
  715. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  716. const items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  717. expect(Array.from(items[0].classList)).toEqual(
  718. expect.arrayContaining([ACTIVE_CLASS])
  719. );
  720. expect(Array.from(items[1].classList)).toEqual(
  721. expect.not.arrayContaining([ACTIVE_CLASS])
  722. );
  723. expect(Array.from(items[2].classList)).toEqual(
  724. expect.not.arrayContaining([ACTIVE_CLASS])
  725. );
  726. simulate(anchor.node, 'keydown', { keyCode: 40 }); // Down
  727. expect(Array.from(items[0].classList)).toEqual(
  728. expect.not.arrayContaining([ACTIVE_CLASS])
  729. );
  730. expect(Array.from(items[1].classList)).toEqual(
  731. expect.arrayContaining([ACTIVE_CLASS])
  732. );
  733. expect(Array.from(items[2].classList)).toEqual(
  734. expect.not.arrayContaining([ACTIVE_CLASS])
  735. );
  736. simulate(anchor.node, 'keydown', { keyCode: 40 }); // Down
  737. expect(Array.from(items[0].classList)).toEqual(
  738. expect.not.arrayContaining([ACTIVE_CLASS])
  739. );
  740. expect(Array.from(items[1].classList)).toEqual(
  741. expect.not.arrayContaining([ACTIVE_CLASS])
  742. );
  743. expect(Array.from(items[2].classList)).toEqual(
  744. expect.arrayContaining([ACTIVE_CLASS])
  745. );
  746. simulate(anchor.node, 'keydown', { keyCode: 38 }); // Up
  747. expect(Array.from(items[0].classList)).toEqual(
  748. expect.not.arrayContaining([ACTIVE_CLASS])
  749. );
  750. expect(Array.from(items[1].classList)).toEqual(
  751. expect.arrayContaining([ACTIVE_CLASS])
  752. );
  753. expect(Array.from(items[2].classList)).toEqual(
  754. expect.not.arrayContaining([ACTIVE_CLASS])
  755. );
  756. simulate(anchor.node, 'keydown', { keyCode: 38 }); // Up
  757. expect(Array.from(items[0].classList)).toEqual(
  758. expect.arrayContaining([ACTIVE_CLASS])
  759. );
  760. expect(Array.from(items[1].classList)).toEqual(
  761. expect.not.arrayContaining([ACTIVE_CLASS])
  762. );
  763. expect(Array.from(items[2].classList)).toEqual(
  764. expect.not.arrayContaining([ACTIVE_CLASS])
  765. );
  766. simulate(anchor.node, 'keydown', { keyCode: 38 }); // Up
  767. expect(Array.from(items[0].classList)).toEqual(
  768. expect.not.arrayContaining([ACTIVE_CLASS])
  769. );
  770. expect(Array.from(items[1].classList)).toEqual(
  771. expect.not.arrayContaining([ACTIVE_CLASS])
  772. );
  773. expect(Array.from(items[2].classList)).toEqual(
  774. expect.arrayContaining([ACTIVE_CLASS])
  775. );
  776. widget.dispose();
  777. anchor.dispose();
  778. });
  779. it('should select the item above and wrap to bottom past first (tab)', () => {
  780. const anchor = createEditorWidget();
  781. const model = new CompleterModel();
  782. const options: Completer.IOptions = {
  783. editor: anchor.editor,
  784. model
  785. };
  786. model.setOptions(['foo', 'bar', 'baz'], {
  787. foo: 'instance',
  788. bar: 'function'
  789. });
  790. Widget.attach(anchor, document.body);
  791. const widget = new Completer(options);
  792. Widget.attach(widget, document.body);
  793. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  794. const items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  795. expect(Array.from(items[0].classList)).toEqual(
  796. expect.arrayContaining([ACTIVE_CLASS])
  797. );
  798. expect(Array.from(items[1].classList)).toEqual(
  799. expect.not.arrayContaining([ACTIVE_CLASS])
  800. );
  801. expect(Array.from(items[2].classList)).toEqual(
  802. expect.not.arrayContaining([ACTIVE_CLASS])
  803. );
  804. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab
  805. expect(Array.from(items[0].classList)).toEqual(
  806. expect.not.arrayContaining([ACTIVE_CLASS])
  807. );
  808. expect(Array.from(items[1].classList)).toEqual(
  809. expect.arrayContaining([ACTIVE_CLASS])
  810. );
  811. expect(Array.from(items[2].classList)).toEqual(
  812. expect.not.arrayContaining([ACTIVE_CLASS])
  813. );
  814. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab
  815. expect(Array.from(items[0].classList)).toEqual(
  816. expect.not.arrayContaining([ACTIVE_CLASS])
  817. );
  818. expect(Array.from(items[1].classList)).toEqual(
  819. expect.not.arrayContaining([ACTIVE_CLASS])
  820. );
  821. expect(Array.from(items[2].classList)).toEqual(
  822. expect.arrayContaining([ACTIVE_CLASS])
  823. );
  824. simulate(anchor.node, 'keydown', { keyCode: 9, shiftKey: true }); // Shift + Tab
  825. expect(Array.from(items[0].classList)).toEqual(
  826. expect.not.arrayContaining([ACTIVE_CLASS])
  827. );
  828. expect(Array.from(items[1].classList)).toEqual(
  829. expect.arrayContaining([ACTIVE_CLASS])
  830. );
  831. expect(Array.from(items[2].classList)).toEqual(
  832. expect.not.arrayContaining([ACTIVE_CLASS])
  833. );
  834. simulate(anchor.node, 'keydown', { keyCode: 9, shiftKey: true }); // Shift + Tab
  835. expect(Array.from(items[0].classList)).toEqual(
  836. expect.arrayContaining([ACTIVE_CLASS])
  837. );
  838. expect(Array.from(items[1].classList)).toEqual(
  839. expect.not.arrayContaining([ACTIVE_CLASS])
  840. );
  841. expect(Array.from(items[2].classList)).toEqual(
  842. expect.not.arrayContaining([ACTIVE_CLASS])
  843. );
  844. simulate(anchor.node, 'keydown', { keyCode: 9, shiftKey: true }); // Shift + Tab
  845. expect(Array.from(items[0].classList)).toEqual(
  846. expect.not.arrayContaining([ACTIVE_CLASS])
  847. );
  848. expect(Array.from(items[1].classList)).toEqual(
  849. expect.not.arrayContaining([ACTIVE_CLASS])
  850. );
  851. expect(Array.from(items[2].classList)).toEqual(
  852. expect.arrayContaining([ACTIVE_CLASS])
  853. );
  854. widget.dispose();
  855. anchor.dispose();
  856. });
  857. it('should select the completion item above and wrap to top past first (arrow keys)', () => {
  858. let anchor = createEditorWidget();
  859. let model = new CompleterModel();
  860. let options: Completer.IOptions = {
  861. editor: anchor.editor,
  862. model
  863. };
  864. model.setCompletionItems!([
  865. { label: 'foo' },
  866. { label: 'bar' },
  867. { label: 'baz' }
  868. ]);
  869. Widget.attach(anchor, document.body);
  870. let widget = new Completer(options);
  871. Widget.attach(widget, document.body);
  872. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  873. let items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  874. expect(Array.from(items[0].classList)).toEqual(
  875. expect.arrayContaining([ACTIVE_CLASS])
  876. );
  877. expect(Array.from(items[1].classList)).toEqual(
  878. expect.not.arrayContaining([ACTIVE_CLASS])
  879. );
  880. expect(Array.from(items[2].classList)).toEqual(
  881. expect.not.arrayContaining([ACTIVE_CLASS])
  882. );
  883. simulate(anchor.node, 'keydown', { keyCode: 40 }); // Down
  884. expect(Array.from(items[0].classList)).toEqual(
  885. expect.not.arrayContaining([ACTIVE_CLASS])
  886. );
  887. expect(Array.from(items[1].classList)).toEqual(
  888. expect.arrayContaining([ACTIVE_CLASS])
  889. );
  890. expect(Array.from(items[2].classList)).toEqual(
  891. expect.not.arrayContaining([ACTIVE_CLASS])
  892. );
  893. simulate(anchor.node, 'keydown', { keyCode: 40 }); // Down
  894. expect(Array.from(items[0].classList)).toEqual(
  895. expect.not.arrayContaining([ACTIVE_CLASS])
  896. );
  897. expect(Array.from(items[1].classList)).toEqual(
  898. expect.not.arrayContaining([ACTIVE_CLASS])
  899. );
  900. expect(Array.from(items[2].classList)).toEqual(
  901. expect.arrayContaining([ACTIVE_CLASS])
  902. );
  903. simulate(anchor.node, 'keydown', { keyCode: 38 }); // Up
  904. expect(Array.from(items[0].classList)).toEqual(
  905. expect.not.arrayContaining([ACTIVE_CLASS])
  906. );
  907. expect(Array.from(items[1].classList)).toEqual(
  908. expect.arrayContaining([ACTIVE_CLASS])
  909. );
  910. expect(Array.from(items[2].classList)).toEqual(
  911. expect.not.arrayContaining([ACTIVE_CLASS])
  912. );
  913. simulate(anchor.node, 'keydown', { keyCode: 38 }); // Up
  914. expect(Array.from(items[0].classList)).toEqual(
  915. expect.arrayContaining([ACTIVE_CLASS])
  916. );
  917. expect(Array.from(items[1].classList)).toEqual(
  918. expect.not.arrayContaining([ACTIVE_CLASS])
  919. );
  920. expect(Array.from(items[2].classList)).toEqual(
  921. expect.not.arrayContaining([ACTIVE_CLASS])
  922. );
  923. simulate(anchor.node, 'keydown', { keyCode: 38 }); // Up
  924. expect(Array.from(items[0].classList)).toEqual(
  925. expect.not.arrayContaining([ACTIVE_CLASS])
  926. );
  927. expect(Array.from(items[1].classList)).toEqual(
  928. expect.not.arrayContaining([ACTIVE_CLASS])
  929. );
  930. expect(Array.from(items[2].classList)).toEqual(
  931. expect.arrayContaining([ACTIVE_CLASS])
  932. );
  933. widget.dispose();
  934. anchor.dispose();
  935. });
  936. it('should select the completion item above and wrap to top past first (tab)', () => {
  937. let anchor = createEditorWidget();
  938. let model = new CompleterModel();
  939. let options: Completer.IOptions = {
  940. editor: anchor.editor,
  941. model
  942. };
  943. model.setCompletionItems!([
  944. { label: 'foo' },
  945. { label: 'bar' },
  946. { label: 'baz' }
  947. ]);
  948. Widget.attach(anchor, document.body);
  949. let widget = new Completer(options);
  950. Widget.attach(widget, document.body);
  951. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  952. let items = widget.node.querySelectorAll(`.${ITEM_CLASS}`);
  953. expect(Array.from(items[0].classList)).toEqual(
  954. expect.arrayContaining([ACTIVE_CLASS])
  955. );
  956. expect(Array.from(items[1].classList)).toEqual(
  957. expect.not.arrayContaining([ACTIVE_CLASS])
  958. );
  959. expect(Array.from(items[2].classList)).toEqual(
  960. expect.not.arrayContaining([ACTIVE_CLASS])
  961. );
  962. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab
  963. expect(Array.from(items[0].classList)).toEqual(
  964. expect.not.arrayContaining([ACTIVE_CLASS])
  965. );
  966. expect(Array.from(items[1].classList)).toEqual(
  967. expect.arrayContaining([ACTIVE_CLASS])
  968. );
  969. expect(Array.from(items[2].classList)).toEqual(
  970. expect.not.arrayContaining([ACTIVE_CLASS])
  971. );
  972. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab
  973. expect(Array.from(items[0].classList)).toEqual(
  974. expect.not.arrayContaining([ACTIVE_CLASS])
  975. );
  976. expect(Array.from(items[1].classList)).toEqual(
  977. expect.not.arrayContaining([ACTIVE_CLASS])
  978. );
  979. expect(Array.from(items[2].classList)).toEqual(
  980. expect.arrayContaining([ACTIVE_CLASS])
  981. );
  982. simulate(anchor.node, 'keydown', { keyCode: 9, shiftKey: true }); // Shift + Tab
  983. expect(Array.from(items[0].classList)).toEqual(
  984. expect.not.arrayContaining([ACTIVE_CLASS])
  985. );
  986. expect(Array.from(items[1].classList)).toEqual(
  987. expect.arrayContaining([ACTIVE_CLASS])
  988. );
  989. expect(Array.from(items[2].classList)).toEqual(
  990. expect.not.arrayContaining([ACTIVE_CLASS])
  991. );
  992. simulate(anchor.node, 'keydown', { keyCode: 9, shiftKey: true }); // Shift + Tab
  993. expect(Array.from(items[0].classList)).toEqual(
  994. expect.arrayContaining([ACTIVE_CLASS])
  995. );
  996. expect(Array.from(items[1].classList)).toEqual(
  997. expect.not.arrayContaining([ACTIVE_CLASS])
  998. );
  999. expect(Array.from(items[2].classList)).toEqual(
  1000. expect.not.arrayContaining([ACTIVE_CLASS])
  1001. );
  1002. simulate(anchor.node, 'keydown', { keyCode: 9, shiftKey: true }); // Shift + Tab
  1003. expect(Array.from(items[0].classList)).toEqual(
  1004. expect.not.arrayContaining([ACTIVE_CLASS])
  1005. );
  1006. expect(Array.from(items[1].classList)).toEqual(
  1007. expect.not.arrayContaining([ACTIVE_CLASS])
  1008. );
  1009. expect(Array.from(items[2].classList)).toEqual(
  1010. expect.arrayContaining([ACTIVE_CLASS])
  1011. );
  1012. widget.dispose();
  1013. anchor.dispose();
  1014. });
  1015. it('should mark common subset on start and complete that subset on tab', async () => {
  1016. const anchor = createEditorWidget();
  1017. const model = new CompleterModel();
  1018. const options: Completer.IOptions = {
  1019. editor: anchor.editor,
  1020. model
  1021. };
  1022. let value = '';
  1023. const listener = (sender: any, selected: string) => {
  1024. value = selected;
  1025. };
  1026. model.setOptions(['fo', 'foo', 'foo', 'fooo'], {
  1027. foo: 'instance',
  1028. bar: 'function'
  1029. });
  1030. Widget.attach(anchor, document.body);
  1031. const widget = new Completer(options);
  1032. widget.selected.connect(listener);
  1033. Widget.attach(widget, document.body);
  1034. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1035. await framePromise();
  1036. const marked = widget.node.querySelectorAll(`.${ITEM_CLASS} mark`);
  1037. expect(Object.keys(value)).toHaveLength(0);
  1038. expect(marked).toHaveLength(4);
  1039. expect(marked[0].textContent).toBe('fo');
  1040. expect(marked[1].textContent).toBe('fo');
  1041. expect(marked[2].textContent).toBe('fo');
  1042. expect(marked[3].textContent).toBe('fo');
  1043. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab key
  1044. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1045. expect(value).toBe('fo');
  1046. widget.dispose();
  1047. anchor.dispose();
  1048. });
  1049. it('should mark common subset of completion items on start and complete that subset on tab', async () => {
  1050. let anchor = createEditorWidget();
  1051. let model = new CompleterModel();
  1052. let options: Completer.IOptions = {
  1053. editor: anchor.editor,
  1054. model
  1055. };
  1056. let value = '';
  1057. let listener = (sender: any, selected: string) => {
  1058. value = selected;
  1059. };
  1060. model.setCompletionItems!([
  1061. { label: 'fo' },
  1062. { label: 'foo' },
  1063. { label: 'foo' },
  1064. { label: 'fooo' }
  1065. ]);
  1066. Widget.attach(anchor, document.body);
  1067. let widget = new Completer(options);
  1068. widget.selected.connect(listener);
  1069. Widget.attach(widget, document.body);
  1070. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1071. await framePromise();
  1072. let marked = widget.node.querySelectorAll(`.${ITEM_CLASS} mark`);
  1073. expect(value).toHaveLength(0);
  1074. expect(marked).toHaveLength(4);
  1075. expect(marked[0].textContent).toBe('fo');
  1076. expect(marked[1].textContent).toBe('fo');
  1077. expect(marked[2].textContent).toBe('fo');
  1078. expect(marked[3].textContent).toBe('fo');
  1079. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab key
  1080. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1081. expect(value).toBe('fo');
  1082. widget.dispose();
  1083. anchor.dispose();
  1084. });
  1085. });
  1086. it('should insert single completion item on tab', async () => {
  1087. const anchor = createEditorWidget();
  1088. const model = new CompleterModel();
  1089. const options: Completer.IOptions = {
  1090. editor: anchor.editor,
  1091. model
  1092. };
  1093. let value = '';
  1094. const listener = (sender: any, selected: string) => {
  1095. value = selected;
  1096. };
  1097. model.setCompletionItems!([{ label: 'foo' }]);
  1098. Widget.attach(anchor, document.body);
  1099. const widget = new Completer(options);
  1100. widget.selected.connect(listener);
  1101. Widget.attach(widget, document.body);
  1102. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1103. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab key
  1104. expect(value).toBe('foo');
  1105. widget.dispose();
  1106. anchor.dispose();
  1107. });
  1108. it('should insert single completion item insertText on tab', async () => {
  1109. const anchor = createEditorWidget();
  1110. const model = new CompleterModel();
  1111. const options: Completer.IOptions = {
  1112. editor: anchor.editor,
  1113. model
  1114. };
  1115. let value = '';
  1116. const listener = (sender: any, selected: string) => {
  1117. value = selected;
  1118. };
  1119. model.setCompletionItems!([{ label: 'foo', insertText: 'bar' }]);
  1120. Widget.attach(anchor, document.body);
  1121. const widget = new Completer(options);
  1122. widget.selected.connect(listener);
  1123. Widget.attach(widget, document.body);
  1124. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1125. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab key
  1126. expect(value).toBe('bar');
  1127. widget.dispose();
  1128. anchor.dispose();
  1129. });
  1130. describe('mousedown', () => {
  1131. it('should trigger a selected signal on mouse down', () => {
  1132. const anchor = createEditorWidget();
  1133. const model = new CompleterModel();
  1134. const options: Completer.IOptions = {
  1135. editor: anchor.editor,
  1136. model
  1137. };
  1138. let value = '';
  1139. const listener = (sender: any, selected: string) => {
  1140. value = selected;
  1141. };
  1142. model.setOptions(['foo', 'bar', 'baz'], {
  1143. foo: 'instance',
  1144. bar: 'function'
  1145. });
  1146. model.query = 'b';
  1147. Widget.attach(anchor, document.body);
  1148. const widget = new Completer(options);
  1149. widget.selected.connect(listener);
  1150. Widget.attach(widget, document.body);
  1151. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1152. const item = widget.node.querySelectorAll(`.${ITEM_CLASS} mark`)[1];
  1153. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab key
  1154. expect(model.query).toBe('ba');
  1155. simulate(item, 'mousedown');
  1156. expect(value).toBe('baz');
  1157. widget.dispose();
  1158. anchor.dispose();
  1159. });
  1160. it('should trigger a selected signal on mouse down of completion item', () => {
  1161. let anchor = createEditorWidget();
  1162. let model = new CompleterModel();
  1163. let options: Completer.IOptions = {
  1164. editor: anchor.editor,
  1165. model
  1166. };
  1167. let value = '';
  1168. let listener = (sender: any, selected: string) => {
  1169. value = selected;
  1170. };
  1171. model.setCompletionItems!([
  1172. { label: 'foo' },
  1173. { label: 'bar' },
  1174. { label: 'baz' }
  1175. ]);
  1176. model.query = 'b';
  1177. Widget.attach(anchor, document.body);
  1178. let widget = new Completer(options);
  1179. widget.selected.connect(listener);
  1180. Widget.attach(widget, document.body);
  1181. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1182. let item = widget.node.querySelectorAll(`.${ITEM_CLASS} mark`)[1];
  1183. simulate(anchor.node, 'keydown', { keyCode: 9 }); // Tab key
  1184. expect(model.query).toBe('ba');
  1185. simulate(item, 'mousedown');
  1186. expect(value).toBe('baz');
  1187. widget.dispose();
  1188. anchor.dispose();
  1189. });
  1190. it('should ignore nonstandard mouse clicks (e.g., right click)', () => {
  1191. const anchor = createEditorWidget();
  1192. const model = new CompleterModel();
  1193. const options: Completer.IOptions = {
  1194. editor: anchor.editor,
  1195. model
  1196. };
  1197. let value = '';
  1198. const listener = (sender: any, selected: string) => {
  1199. value = selected;
  1200. };
  1201. model.setOptions(['foo', 'bar']);
  1202. Widget.attach(anchor, document.body);
  1203. const widget = new Completer(options);
  1204. widget.selected.connect(listener);
  1205. Widget.attach(widget, document.body);
  1206. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1207. expect(value).toBe('');
  1208. simulate(widget.node, 'mousedown', { button: 1 });
  1209. expect(value).toBe('');
  1210. widget.dispose();
  1211. anchor.dispose();
  1212. });
  1213. it('should ignore nonstandard mouse clicks (e.g., right click) on completion item', () => {
  1214. let anchor = createEditorWidget();
  1215. let model = new CompleterModel();
  1216. let options: Completer.IOptions = {
  1217. editor: anchor.editor,
  1218. model
  1219. };
  1220. let value = '';
  1221. let listener = (sender: any, selected: string) => {
  1222. value = selected;
  1223. };
  1224. model.setCompletionItems!([{ label: 'foo' }, { label: 'bar' }]);
  1225. Widget.attach(anchor, document.body);
  1226. let widget = new Completer(options);
  1227. widget.selected.connect(listener);
  1228. Widget.attach(widget, document.body);
  1229. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1230. expect(value).toBe('');
  1231. simulate(widget.node, 'mousedown', { button: 1 });
  1232. expect(value).toBe('');
  1233. widget.dispose();
  1234. anchor.dispose();
  1235. });
  1236. it('should ignore a mouse down that misses an item', () => {
  1237. const anchor = createEditorWidget();
  1238. const model = new CompleterModel();
  1239. const options: Completer.IOptions = {
  1240. editor: anchor.editor,
  1241. model
  1242. };
  1243. let value = '';
  1244. const listener = (sender: any, selected: string) => {
  1245. value = selected;
  1246. };
  1247. model.setOptions(['foo', 'bar']);
  1248. Widget.attach(anchor, document.body);
  1249. const widget = new Completer(options);
  1250. widget.selected.connect(listener);
  1251. Widget.attach(widget, document.body);
  1252. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1253. expect(value).toBe('');
  1254. simulate(widget.node, 'mousedown');
  1255. expect(value).toBe('');
  1256. widget.dispose();
  1257. anchor.dispose();
  1258. });
  1259. it('should ignore a mouse down that misses a completion item', () => {
  1260. let anchor = createEditorWidget();
  1261. let model = new CompleterModel();
  1262. let options: Completer.IOptions = {
  1263. editor: anchor.editor,
  1264. model
  1265. };
  1266. let value = '';
  1267. let listener = (sender: any, selected: string) => {
  1268. value = selected;
  1269. };
  1270. model.setCompletionItems!([{ label: 'foo' }, { label: 'bar' }]);
  1271. Widget.attach(anchor, document.body);
  1272. let widget = new Completer(options);
  1273. widget.selected.connect(listener);
  1274. Widget.attach(widget, document.body);
  1275. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1276. expect(value).toBe('');
  1277. simulate(widget.node, 'mousedown');
  1278. expect(value).toBe('');
  1279. widget.dispose();
  1280. anchor.dispose();
  1281. });
  1282. it('should hide widget if mouse down misses it', () => {
  1283. const anchor = createEditorWidget();
  1284. const model = new CompleterModel();
  1285. const options: Completer.IOptions = {
  1286. editor: anchor.editor,
  1287. model
  1288. };
  1289. const listener = (sender: any, selected: string) => {
  1290. // no op
  1291. };
  1292. model.setOptions(['foo', 'bar']);
  1293. Widget.attach(anchor, document.body);
  1294. const widget = new Completer(options);
  1295. widget.selected.connect(listener);
  1296. Widget.attach(widget, document.body);
  1297. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1298. expect(widget.isHidden).toBe(false);
  1299. simulate(anchor.node, 'mousedown');
  1300. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1301. expect(widget.isHidden).toBe(true);
  1302. widget.dispose();
  1303. anchor.dispose();
  1304. });
  1305. it('should hide completion items widget if mouse down misses it', () => {
  1306. let anchor = createEditorWidget();
  1307. let model = new CompleterModel();
  1308. let options: Completer.IOptions = {
  1309. editor: anchor.editor,
  1310. model
  1311. };
  1312. let listener = (sender: any, selected: string) => {
  1313. // no op
  1314. };
  1315. model.setCompletionItems!([{ label: 'foo' }, { label: 'bar' }]);
  1316. Widget.attach(anchor, document.body);
  1317. let widget = new Completer(options);
  1318. widget.selected.connect(listener);
  1319. Widget.attach(widget, document.body);
  1320. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1321. expect(widget.isHidden).toBe(false);
  1322. simulate(anchor.node, 'mousedown');
  1323. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1324. expect(widget.isHidden).toBe(true);
  1325. widget.dispose();
  1326. anchor.dispose();
  1327. });
  1328. });
  1329. describe('scroll', () => {
  1330. it.skip('should position itself according to the anchor', async () => {
  1331. const panel = new Panel();
  1332. const code = createEditorWidget();
  1333. const editor = code.editor;
  1334. const model = new CompleterModel();
  1335. const text = '\n\n\n\n\n\na';
  1336. code.node.style.height = '5000px';
  1337. code.node.style.width = '400px';
  1338. code.node.style.background = 'yellow';
  1339. editor.model.value.text = text;
  1340. panel.node.style.background = 'red';
  1341. panel.node.style.height = '2000px';
  1342. panel.node.style.width = '500px';
  1343. panel.node.style.maxHeight = '500px';
  1344. panel.node.style.overflow = 'auto';
  1345. panel.node.style.position = 'absolute';
  1346. panel.node.style.top = '0px';
  1347. panel.node.style.left = '0px';
  1348. panel.node.scrollTop = 10;
  1349. panel.addWidget(code);
  1350. Widget.attach(panel, document.body);
  1351. editor.refresh();
  1352. const position = code.editor.getPositionAt(text.length)!;
  1353. const coords = code.editor.getCoordinateForPosition(position);
  1354. editor.setCursorPosition(position);
  1355. const request: Completer.ITextState = {
  1356. column: position.column,
  1357. lineHeight: editor.lineHeight,
  1358. charWidth: editor.charWidth,
  1359. line: position.line,
  1360. text: 'a'
  1361. };
  1362. model.original = request;
  1363. model.cursor = { start: text.length - 1, end: text.length };
  1364. model.setOptions(['abc', 'abd', 'abe', 'abi']);
  1365. const widget = new Completer({ model, editor: code.editor });
  1366. Widget.attach(widget, document.body);
  1367. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1368. simulate(document.body, 'scroll');
  1369. // Because the scroll handling is asynchronous, this test uses a large
  1370. // timeout (500ms) to guarantee the scroll handling has finished.
  1371. await sleep(500);
  1372. const top = parseInt(window.getComputedStyle(widget.node).top, 10);
  1373. const bottom = Math.floor(coords.bottom);
  1374. expect(top + panel.node.scrollTop).toBe(bottom);
  1375. widget.dispose();
  1376. code.dispose();
  1377. panel.dispose();
  1378. });
  1379. });
  1380. });
  1381. describe('#onUpdateRequest()', () => {
  1382. it('should emit a selection if there is only one match', () => {
  1383. const anchor = createEditorWidget();
  1384. const model = new CompleterModel();
  1385. const coords = { left: 0, right: 0, top: 100, bottom: 120 };
  1386. const request: Completer.ITextState = {
  1387. column: 0,
  1388. lineHeight: 0,
  1389. charWidth: 0,
  1390. line: 0,
  1391. coords: coords as CodeEditor.ICoordinate,
  1392. text: 'f'
  1393. };
  1394. let value = '';
  1395. const options: Completer.IOptions = {
  1396. editor: anchor.editor,
  1397. model
  1398. };
  1399. const listener = (sender: any, selected: string) => {
  1400. value = selected;
  1401. };
  1402. Widget.attach(anchor, document.body);
  1403. model.original = request;
  1404. model.setOptions(['foo']);
  1405. const widget = new Completer(options);
  1406. widget.selected.connect(listener);
  1407. Widget.attach(widget, document.body);
  1408. expect(value).toBe('');
  1409. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1410. expect(value).toBe('foo');
  1411. widget.dispose();
  1412. anchor.dispose();
  1413. });
  1414. it('should do nothing if a model does not exist', () => {
  1415. const widget = new LogWidget({ editor: null });
  1416. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1417. expect(widget.methods).toEqual(
  1418. expect.arrayContaining(['onUpdateRequest'])
  1419. );
  1420. });
  1421. it('should un-hide widget if multiple options are available', () => {
  1422. const anchor = createEditorWidget();
  1423. const model = new CompleterModel();
  1424. const coords = { left: 0, right: 0, top: 100, bottom: 120 };
  1425. const request: Completer.ITextState = {
  1426. column: 0,
  1427. lineHeight: 0,
  1428. charWidth: 0,
  1429. line: 0,
  1430. coords: coords as CodeEditor.ICoordinate,
  1431. text: 'f'
  1432. };
  1433. const options: Completer.IOptions = {
  1434. editor: anchor.editor,
  1435. model
  1436. };
  1437. Widget.attach(anchor, document.body);
  1438. model.original = request;
  1439. model.setOptions(['foo', 'bar', 'baz']);
  1440. const widget = new Completer(options);
  1441. widget.hide();
  1442. expect(widget.isHidden).toBe(true);
  1443. Widget.attach(widget, document.body);
  1444. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1445. expect(widget.isVisible).toBe(true);
  1446. widget.dispose();
  1447. anchor.dispose();
  1448. });
  1449. it('should un-hide widget if multiple completion items are available', () => {
  1450. let anchor = createEditorWidget();
  1451. let model = new CompleterModel();
  1452. let coords = { left: 0, right: 0, top: 100, bottom: 120 };
  1453. let request: Completer.ITextState = {
  1454. column: 0,
  1455. lineHeight: 0,
  1456. charWidth: 0,
  1457. line: 0,
  1458. coords: coords as CodeEditor.ICoordinate,
  1459. text: 'f'
  1460. };
  1461. let options: Completer.IOptions = {
  1462. editor: anchor.editor,
  1463. model
  1464. };
  1465. Widget.attach(anchor, document.body);
  1466. model.original = request;
  1467. model.setCompletionItems!([
  1468. { label: 'foo' },
  1469. { label: 'bar' },
  1470. { label: 'baz' }
  1471. ]);
  1472. let widget = new Completer(options);
  1473. widget.hide();
  1474. expect(widget.isHidden).toBe(true);
  1475. Widget.attach(widget, document.body);
  1476. MessageLoop.sendMessage(widget, Widget.Msg.UpdateRequest);
  1477. expect(widget.isVisible).toBe(true);
  1478. widget.dispose();
  1479. anchor.dispose();
  1480. });
  1481. });
  1482. });
  1483. });