github.com/Ilhicas/nomad@v1.0.4-0.20210304152020-e86851182bc3/ui/tests/integration/components/streaming-file-test.js (about)

     1  import { run } from '@ember/runloop';
     2  import { find, settled, triggerKeyEvent } from '@ember/test-helpers';
     3  import { module, test } from 'qunit';
     4  import { setupRenderingTest } from 'ember-qunit';
     5  import hbs from 'htmlbars-inline-precompile';
     6  import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
     7  import Pretender from 'pretender';
     8  import { logEncode } from '../../../mirage/data/logs';
     9  import fetch from 'nomad-ui/utils/fetch';
    10  import Log from 'nomad-ui/utils/classes/log';
    11  
    12  const { assign } = Object;
    13  const A_KEY = 65;
    14  
    15  const stringifyValues = obj =>
    16    Object.keys(obj).reduce((newObj, key) => {
    17      newObj[key] = obj[key].toString();
    18      return newObj;
    19    }, {});
    20  
    21  const makeLogger = (url, params) =>
    22    Log.create({
    23      url,
    24      params,
    25      plainText: true,
    26      logFetch: url => fetch(url).then(res => res),
    27    });
    28  
    29  module('Integration | Component | streaming file', function(hooks) {
    30    setupRenderingTest(hooks);
    31  
    32    hooks.beforeEach(function() {
    33      this.server = new Pretender(function() {
    34        this.get('/file/endpoint', () => [200, {}, 'Hello World']);
    35        this.get('/file/stream', () => [200, {}, logEncode(['Hello World'], 0)]);
    36      });
    37    });
    38  
    39    hooks.afterEach(function() {
    40      this.server.shutdown();
    41    });
    42  
    43    const commonTemplate = hbs`
    44      <StreamingFile @logger={{logger}} @mode={{mode}} @isStreaming={{isStreaming}} />
    45    `;
    46  
    47    test('when mode is `head`, the logger signals head', async function(assert) {
    48      const url = '/file/endpoint';
    49      const params = { path: 'hello/world.txt', offset: 0, limit: 50000 };
    50      this.setProperties({
    51        logger: makeLogger(url, params),
    52        mode: 'head',
    53        isStreaming: false,
    54      });
    55  
    56      await this.render(commonTemplate);
    57      await settled();
    58  
    59      const request = this.server.handledRequests[0];
    60      assert.equal(this.server.handledRequests.length, 1, 'One request made');
    61      assert.equal(request.url.split('?')[0], url, `URL is ${url}`);
    62      assert.deepEqual(
    63        request.queryParams,
    64        stringifyValues(assign({ origin: 'start' }, params)),
    65        'Query params are correct'
    66      );
    67      assert.equal(find('[data-test-output]').textContent, 'Hello World');
    68      await componentA11yAudit(this.element, assert);
    69    });
    70  
    71    test('when mode is `tail`, the logger signals tail', async function(assert) {
    72      const url = '/file/endpoint';
    73      const params = { path: 'hello/world.txt', limit: 50000 };
    74      this.setProperties({
    75        logger: makeLogger(url, params),
    76        mode: 'tail',
    77        isStreaming: false,
    78      });
    79  
    80      await this.render(commonTemplate);
    81      await settled();
    82  
    83      const request = this.server.handledRequests[0];
    84      assert.equal(this.server.handledRequests.length, 1, 'One request made');
    85      assert.equal(request.url.split('?')[0], url, `URL is ${url}`);
    86      assert.deepEqual(
    87        request.queryParams,
    88        stringifyValues(assign({ origin: 'end', offset: 50000 }, params)),
    89        'Query params are correct'
    90      );
    91      assert.equal(find('[data-test-output]').textContent, 'Hello World');
    92    });
    93  
    94    test('when mode is `streaming` and `isStreaming` is true, streaming starts', async function(assert) {
    95      const url = '/file/stream';
    96      const params = { path: 'hello/world.txt', limit: 50000 };
    97      this.setProperties({
    98        logger: makeLogger(url, params),
    99        mode: 'streaming',
   100        isStreaming: true,
   101      });
   102  
   103      assert.ok(true);
   104  
   105      run.later(run, run.cancelTimers, 500);
   106  
   107      await this.render(commonTemplate);
   108      await settled();
   109  
   110      const request = this.server.handledRequests[0];
   111      assert.equal(request.url.split('?')[0], url, `URL is ${url}`);
   112      assert.equal(find('[data-test-output]').textContent, 'Hello World');
   113    });
   114  
   115    test('the ctrl+a/cmd+a shortcut selects only the text in the output window', async function(assert) {
   116      const url = '/file/endpoint';
   117      const params = { path: 'hello/world.txt', offset: 0, limit: 50000 };
   118      this.setProperties({
   119        logger: makeLogger(url, params),
   120        mode: 'head',
   121        isStreaming: false,
   122      });
   123  
   124      await this.render(hbs`
   125        Extra text
   126        <StreamingFile @logger={{logger}} @mode={{mode}} @isStreaming={{isStreaming}} />
   127        On either side
   128      `);
   129      await settled();
   130  
   131      // Windows and Linux shortcut
   132      await triggerKeyEvent('[data-test-output]', 'keydown', A_KEY, { ctrlKey: true });
   133      assert.equal(
   134        window
   135          .getSelection()
   136          .toString()
   137          .trim(),
   138        find('[data-test-output]').textContent.trim()
   139      );
   140  
   141      window.getSelection().removeAllRanges();
   142  
   143      // MacOS shortcut
   144      await triggerKeyEvent('[data-test-output]', 'keydown', A_KEY, { metaKey: true });
   145      assert.equal(
   146        window
   147          .getSelection()
   148          .toString()
   149          .trim(),
   150        find('[data-test-output]').textContent.trim()
   151      );
   152    });
   153  });