github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/ui/tests/integration/components/streaming-file-test.js (about) 1 import { run } from '@ember/runloop'; 2 import { find, render, 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 assert.expect(5); 49 50 const url = '/file/endpoint'; 51 const params = { path: 'hello/world.txt', offset: 0, limit: 50000 }; 52 this.setProperties({ 53 logger: makeLogger(url, params), 54 mode: 'head', 55 isStreaming: false, 56 }); 57 58 await render(commonTemplate); 59 60 const request = this.server.handledRequests[0]; 61 assert.equal(this.server.handledRequests.length, 1, 'One request made'); 62 assert.equal(request.url.split('?')[0], url, `URL is ${url}`); 63 assert.deepEqual( 64 request.queryParams, 65 stringifyValues(assign({ origin: 'start' }, params)), 66 'Query params are correct' 67 ); 68 assert.equal(find('[data-test-output]').textContent, 'Hello World'); 69 await componentA11yAudit(this.element, assert); 70 }); 71 72 test('when mode is `tail`, the logger signals tail', async function (assert) { 73 const url = '/file/endpoint'; 74 const params = { path: 'hello/world.txt', limit: 50000 }; 75 this.setProperties({ 76 logger: makeLogger(url, params), 77 mode: 'tail', 78 isStreaming: false, 79 }); 80 81 await render(commonTemplate); 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 render(commonTemplate); 108 109 const request = this.server.handledRequests[0]; 110 assert.equal(request.url.split('?')[0], url, `URL is ${url}`); 111 assert.equal(find('[data-test-output]').textContent, 'Hello World'); 112 }); 113 114 test('the ctrl+a/cmd+a shortcut selects only the text in the output window', async function (assert) { 115 const url = '/file/endpoint'; 116 const params = { path: 'hello/world.txt', offset: 0, limit: 50000 }; 117 this.setProperties({ 118 logger: makeLogger(url, params), 119 mode: 'head', 120 isStreaming: false, 121 }); 122 123 await render(hbs` 124 Extra text 125 <StreamingFile @logger={{logger}} @mode={{mode}} @isStreaming={{isStreaming}} /> 126 On either side 127 `); 128 129 // Windows and Linux shortcut 130 await triggerKeyEvent('[data-test-output]', 'keydown', A_KEY, { 131 ctrlKey: true, 132 }); 133 assert.equal( 134 window.getSelection().toString().trim(), 135 find('[data-test-output]').textContent.trim() 136 ); 137 138 window.getSelection().removeAllRanges(); 139 140 // MacOS shortcut 141 await triggerKeyEvent('[data-test-output]', 'keydown', A_KEY, { 142 metaKey: true, 143 }); 144 assert.equal( 145 window.getSelection().toString().trim(), 146 find('[data-test-output]').textContent.trim() 147 ); 148 }); 149 });