github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/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('Integration | Utility | exec-command-editor-xterm-adapter', function(hooks) {
    10    setupRenderingTest(hooks);
    11  
    12    test('it can wrap to a previous line while backspacing', async function(assert) {
    13      let done = assert.async();
    14  
    15      await render(hbs`
    16        <div id='terminal'></div>
    17      `);
    18  
    19      let terminal = new Terminal({ cols: 10 });
    20      terminal.open(document.getElementById('terminal'));
    21  
    22      terminal.write('/bin/long-command');
    23  
    24      new ExecCommandEditorXtermAdapter(
    25        terminal,
    26        command => {
    27          assert.equal(command, '/bin/long');
    28          done();
    29        },
    30        '/bin/long-command'
    31      );
    32  
    33      await terminal.simulateCommandDataEvent(KEYS.DELETE);
    34      await terminal.simulateCommandDataEvent(KEYS.DELETE);
    35      await terminal.simulateCommandDataEvent(KEYS.DELETE);
    36      await terminal.simulateCommandDataEvent(KEYS.DELETE);
    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  
    42      await settled();
    43  
    44      assert.equal(
    45        terminal.buffer.active
    46          .getLine(0)
    47          .translateToString()
    48          .trim(),
    49        '/bin/long'
    50      );
    51  
    52      await terminal.simulateCommandDataEvent(KEYS.ENTER);
    53    });
    54  
    55    test('it ignores arrow keys and unprintable characters other than ^U', async function(assert) {
    56      let done = assert.async();
    57  
    58      await render(hbs`
    59        <div id='terminal'></div>
    60      `);
    61  
    62      let terminal = new Terminal({ cols: 72 });
    63      terminal.open(document.getElementById('terminal'));
    64  
    65      terminal.write('/bin/bash');
    66  
    67      new ExecCommandEditorXtermAdapter(
    68        terminal,
    69        command => {
    70          assert.equal(command, '/bin/bash!');
    71          done();
    72        },
    73        '/bin/bash'
    74      );
    75  
    76      await terminal.simulateCommandDataEvent(KEYS.RIGHT_ARROW);
    77      await terminal.simulateCommandDataEvent(KEYS.RIGHT_ARROW);
    78      await terminal.simulateCommandDataEvent(KEYS.LEFT_ARROW);
    79      await terminal.simulateCommandDataEvent(KEYS.UP_ARROW);
    80      await terminal.simulateCommandDataEvent(KEYS.UP_ARROW);
    81      await terminal.simulateCommandDataEvent(KEYS.DOWN_ARROW);
    82      await terminal.simulateCommandDataEvent(KEYS.CONTROL_A);
    83      await terminal.simulateCommandDataEvent('!');
    84  
    85      await settled();
    86  
    87      assert.equal(terminal.buffer.active.cursorY, 0);
    88      assert.equal(terminal.buffer.active.cursorX, 10);
    89  
    90      assert.equal(
    91        terminal.buffer.active
    92          .getLine(0)
    93          .translateToString()
    94          .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      let done = assert.async();
   103  
   104      await render(hbs`
   105        <div id='terminal'></div>
   106      `);
   107  
   108      let terminal = new Terminal({ cols: 10 });
   109      terminal.open(document.getElementById('terminal'));
   110  
   111      terminal.write('to-delete');
   112  
   113      new ExecCommandEditorXtermAdapter(
   114        terminal,
   115        command => {
   116          assert.equal(command, '!');
   117          done();
   118        },
   119        'to-delete'
   120      );
   121  
   122      await terminal.simulateCommandDataEvent(KEYS.CONTROL_U);
   123  
   124      await settled();
   125  
   126      assert.equal(
   127        terminal.buffer.active
   128          .getLine(0)
   129          .translateToString()
   130          .trim(),
   131        ''
   132      );
   133  
   134      await terminal.simulateCommandDataEvent('!');
   135      await terminal.simulateCommandDataEvent(KEYS.ENTER);
   136    });
   137  });