github.com/hernad/nomad@v1.6.112/ui/tests/integration/components/streaming-file-test.js (about)

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