open-nomad/ui/tests/integration/util/exec-command-editor-xterm-adapter-test.js
Buck Doyle 222b36ced3
UI: use lazy-loading for Xterm.js (#7964)
This updates Xterm.js to 4.6.0, which includes support for reverse-wraparound
mode, so we no longer need to use a vendored dependency, which closes #7461.
The interface for accessing the buffer that’s used for test assertions changed.

With the dependency now accessed conventionally, we can have it load only when
it’s needed by an exec popup window, which closes #7516. That saves us
≈60kb compressed in the dependency bundle!
2020-05-26 09:56:25 -05:00

138 lines
3.7 KiB
JavaScript

import ExecCommandEditorXtermAdapter from 'nomad-ui/utils/classes/exec-command-editor-xterm-adapter';
import { setupRenderingTest } from 'ember-qunit';
import { module, test } from 'qunit';
import { render, settled } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import { Terminal } from 'xterm';
import KEYS from 'nomad-ui/utils/keys';
module('Integration | Utility | exec-command-editor-xterm-adapter', function(hooks) {
setupRenderingTest(hooks);
test('it can wrap to a previous line while backspacing', async function(assert) {
let done = assert.async();
await render(hbs`
<div id='terminal'></div>
`);
let terminal = new Terminal({ cols: 10 });
terminal.open(document.getElementById('terminal'));
terminal.write('/bin/long-command');
new ExecCommandEditorXtermAdapter(
terminal,
command => {
assert.equal(command, '/bin/long');
done();
},
'/bin/long-command'
);
await terminal.simulateCommandDataEvent(KEYS.DELETE);
await terminal.simulateCommandDataEvent(KEYS.DELETE);
await terminal.simulateCommandDataEvent(KEYS.DELETE);
await terminal.simulateCommandDataEvent(KEYS.DELETE);
await terminal.simulateCommandDataEvent(KEYS.DELETE);
await terminal.simulateCommandDataEvent(KEYS.DELETE);
await terminal.simulateCommandDataEvent(KEYS.DELETE);
await terminal.simulateCommandDataEvent(KEYS.DELETE);
await settled();
assert.equal(
terminal.buffer.active
.getLine(0)
.translateToString()
.trim(),
'/bin/long'
);
await terminal.simulateCommandDataEvent(KEYS.ENTER);
});
test('it ignores arrow keys and unprintable characters other than ^U', async function(assert) {
let done = assert.async();
await render(hbs`
<div id='terminal'></div>
`);
let terminal = new Terminal({ cols: 72 });
terminal.open(document.getElementById('terminal'));
terminal.write('/bin/bash');
new ExecCommandEditorXtermAdapter(
terminal,
command => {
assert.equal(command, '/bin/bash!');
done();
},
'/bin/bash'
);
await terminal.simulateCommandDataEvent(KEYS.RIGHT_ARROW);
await terminal.simulateCommandDataEvent(KEYS.RIGHT_ARROW);
await terminal.simulateCommandDataEvent(KEYS.LEFT_ARROW);
await terminal.simulateCommandDataEvent(KEYS.UP_ARROW);
await terminal.simulateCommandDataEvent(KEYS.UP_ARROW);
await terminal.simulateCommandDataEvent(KEYS.DOWN_ARROW);
await terminal.simulateCommandDataEvent(KEYS.CONTROL_A);
await terminal.simulateCommandDataEvent('!');
await settled();
assert.equal(terminal.buffer.active.cursorY, 0);
assert.equal(terminal.buffer.active.cursorX, 10);
assert.equal(
terminal.buffer.active
.getLine(0)
.translateToString()
.trim(),
'/bin/bash!'
);
await terminal.simulateCommandDataEvent(KEYS.ENTER);
});
test('it supports typing ^U to delete the entire command', async function(assert) {
let done = assert.async();
await render(hbs`
<div id='terminal'></div>
`);
let terminal = new Terminal({ cols: 10 });
terminal.open(document.getElementById('terminal'));
terminal.write('to-delete');
new ExecCommandEditorXtermAdapter(
terminal,
command => {
assert.equal(command, '!');
done();
},
'to-delete'
);
await terminal.simulateCommandDataEvent(KEYS.CONTROL_U);
await settled();
assert.equal(
terminal.buffer.active
.getLine(0)
.translateToString()
.trim(),
''
);
await terminal.simulateCommandDataEvent('!');
await terminal.simulateCommandDataEvent(KEYS.ENTER);
});
});