github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/integration/util/exec-command-editor-xterm-adapter-test.js (about) 1 import ExecCommandEditorXtermAdapter from 'nomad-ui/utils/classes/exec-command-editor-xterm-adapter'; 2 import { setupRenderingTest } from 'ember-qunit'; 3 import { module, test } from 'qunit'; 4 import { render, settled } from '@ember/test-helpers'; 5 import hbs from 'htmlbars-inline-precompile'; 6 import { Terminal } from 'xterm'; 7 import KEYS from 'nomad-ui/utils/keys'; 8 9 module( 10 'Integration | Utility | exec-command-editor-xterm-adapter', 11 function (hooks) { 12 setupRenderingTest(hooks); 13 14 test('it can wrap to a previous line while backspacing', async function (assert) { 15 assert.expect(2); 16 17 let done = assert.async(); 18 19 await render(hbs` 20 <div id='terminal'></div> 21 `); 22 23 let terminal = new Terminal({ cols: 10 }); 24 terminal.open(document.getElementById('terminal')); 25 26 terminal.write('/bin/long-command'); 27 28 new ExecCommandEditorXtermAdapter( 29 terminal, 30 (command) => { 31 assert.equal(command, '/bin/long'); 32 done(); 33 }, 34 '/bin/long-command' 35 ); 36 37 await terminal.simulateCommandDataEvent(KEYS.DELETE); 38 await terminal.simulateCommandDataEvent(KEYS.DELETE); 39 await terminal.simulateCommandDataEvent(KEYS.DELETE); 40 await terminal.simulateCommandDataEvent(KEYS.DELETE); 41 await terminal.simulateCommandDataEvent(KEYS.DELETE); 42 await terminal.simulateCommandDataEvent(KEYS.DELETE); 43 await terminal.simulateCommandDataEvent(KEYS.DELETE); 44 await terminal.simulateCommandDataEvent(KEYS.DELETE); 45 46 await settled(); 47 48 assert.equal( 49 terminal.buffer.active.getLine(0).translateToString().trim(), 50 '/bin/long' 51 ); 52 53 await terminal.simulateCommandDataEvent(KEYS.ENTER); 54 }); 55 56 test('it ignores arrow keys and unprintable characters other than ^U', async function (assert) { 57 assert.expect(4); 58 59 let done = assert.async(); 60 61 await render(hbs` 62 <div id='terminal'></div> 63 `); 64 65 let terminal = new Terminal({ cols: 72 }); 66 terminal.open(document.getElementById('terminal')); 67 68 terminal.write('/bin/bash'); 69 70 new ExecCommandEditorXtermAdapter( 71 terminal, 72 (command) => { 73 assert.equal(command, '/bin/bash!'); 74 done(); 75 }, 76 '/bin/bash' 77 ); 78 79 await terminal.simulateCommandDataEvent(KEYS.RIGHT_ARROW); 80 await terminal.simulateCommandDataEvent(KEYS.RIGHT_ARROW); 81 await terminal.simulateCommandDataEvent(KEYS.LEFT_ARROW); 82 await terminal.simulateCommandDataEvent(KEYS.UP_ARROW); 83 await terminal.simulateCommandDataEvent(KEYS.UP_ARROW); 84 await terminal.simulateCommandDataEvent(KEYS.DOWN_ARROW); 85 await terminal.simulateCommandDataEvent(KEYS.CONTROL_A); 86 await terminal.simulateCommandDataEvent('!'); 87 88 await settled(); 89 90 assert.equal(terminal.buffer.active.cursorY, 0); 91 assert.equal(terminal.buffer.active.cursorX, 10); 92 93 assert.equal( 94 terminal.buffer.active.getLine(0).translateToString().trim(), 95 '/bin/bash!' 96 ); 97 98 await terminal.simulateCommandDataEvent(KEYS.ENTER); 99 }); 100 101 test('it supports typing ^U to delete the entire command', async function (assert) { 102 assert.expect(2); 103 104 let done = assert.async(); 105 106 await render(hbs` 107 <div id='terminal'></div> 108 `); 109 110 let terminal = new Terminal({ cols: 10 }); 111 terminal.open(document.getElementById('terminal')); 112 113 terminal.write('to-delete'); 114 115 new ExecCommandEditorXtermAdapter( 116 terminal, 117 (command) => { 118 assert.equal(command, '!'); 119 done(); 120 }, 121 'to-delete' 122 ); 123 124 await terminal.simulateCommandDataEvent(KEYS.CONTROL_U); 125 126 await settled(); 127 128 assert.equal( 129 terminal.buffer.active.getLine(0).translateToString().trim(), 130 '' 131 ); 132 133 await terminal.simulateCommandDataEvent('!'); 134 await terminal.simulateCommandDataEvent(KEYS.ENTER); 135 }); 136 } 137 );