2019-07-31 00:20:44 +00:00
|
|
|
import { run } from '@ember/runloop';
|
2021-12-28 15:42:35 +00:00
|
|
|
import { find, render, triggerKeyEvent } from '@ember/test-helpers';
|
2019-07-31 00:20:44 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2020-08-25 15:56:02 +00:00
|
|
|
import { componentA11yAudit } from 'nomad-ui/tests/helpers/a11y-audit';
|
2019-07-31 00:20:44 +00:00
|
|
|
import Pretender from 'pretender';
|
2020-08-25 15:56:02 +00:00
|
|
|
import { logEncode } from '../../../mirage/data/logs';
|
2019-07-31 00:20:44 +00:00
|
|
|
import fetch from 'nomad-ui/utils/fetch';
|
|
|
|
import Log from 'nomad-ui/utils/classes/log';
|
|
|
|
|
|
|
|
const { assign } = Object;
|
2020-06-16 19:51:52 +00:00
|
|
|
const A_KEY = 65;
|
2019-07-31 00:20:44 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
const stringifyValues = (obj) =>
|
2019-07-31 00:20:44 +00:00
|
|
|
Object.keys(obj).reduce((newObj, key) => {
|
|
|
|
newObj[key] = obj[key].toString();
|
|
|
|
return newObj;
|
|
|
|
}, {});
|
|
|
|
|
|
|
|
const makeLogger = (url, params) =>
|
|
|
|
Log.create({
|
|
|
|
url,
|
|
|
|
params,
|
|
|
|
plainText: true,
|
2021-12-28 14:45:20 +00:00
|
|
|
logFetch: (url) => fetch(url).then((res) => res),
|
2019-07-31 00:20:44 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
module('Integration | Component | streaming file', function (hooks) {
|
2019-07-31 00:20:44 +00:00
|
|
|
setupRenderingTest(hooks);
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.beforeEach(function () {
|
|
|
|
this.server = new Pretender(function () {
|
2019-07-31 00:20:44 +00:00
|
|
|
this.get('/file/endpoint', () => [200, {}, 'Hello World']);
|
|
|
|
this.get('/file/stream', () => [200, {}, logEncode(['Hello World'], 0)]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
hooks.afterEach(function () {
|
2019-07-31 00:20:44 +00:00
|
|
|
this.server.shutdown();
|
|
|
|
});
|
|
|
|
|
|
|
|
const commonTemplate = hbs`
|
2020-06-01 19:03:56 +00:00
|
|
|
<StreamingFile @logger={{logger}} @mode={{mode}} @isStreaming={{isStreaming}} />
|
2019-07-31 00:20:44 +00:00
|
|
|
`;
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('when mode is `head`, the logger signals head', async function (assert) {
|
2021-12-28 19:30:38 +00:00
|
|
|
assert.expect(5);
|
|
|
|
|
2019-07-31 00:20:44 +00:00
|
|
|
const url = '/file/endpoint';
|
|
|
|
const params = { path: 'hello/world.txt', offset: 0, limit: 50000 };
|
|
|
|
this.setProperties({
|
|
|
|
logger: makeLogger(url, params),
|
|
|
|
mode: 'head',
|
|
|
|
isStreaming: false,
|
|
|
|
});
|
|
|
|
|
2021-12-28 15:42:35 +00:00
|
|
|
await render(commonTemplate);
|
2019-07-31 00:20:44 +00:00
|
|
|
|
|
|
|
const request = this.server.handledRequests[0];
|
|
|
|
assert.equal(this.server.handledRequests.length, 1, 'One request made');
|
|
|
|
assert.equal(request.url.split('?')[0], url, `URL is ${url}`);
|
|
|
|
assert.deepEqual(
|
|
|
|
request.queryParams,
|
|
|
|
stringifyValues(assign({ origin: 'start' }, params)),
|
|
|
|
'Query params are correct'
|
|
|
|
);
|
|
|
|
assert.equal(find('[data-test-output]').textContent, 'Hello World');
|
2020-08-25 15:56:02 +00:00
|
|
|
await componentA11yAudit(this.element, assert);
|
2019-07-31 00:20:44 +00:00
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('when mode is `tail`, the logger signals tail', async function (assert) {
|
2019-07-31 00:20:44 +00:00
|
|
|
const url = '/file/endpoint';
|
|
|
|
const params = { path: 'hello/world.txt', limit: 50000 };
|
|
|
|
this.setProperties({
|
|
|
|
logger: makeLogger(url, params),
|
|
|
|
mode: 'tail',
|
|
|
|
isStreaming: false,
|
|
|
|
});
|
|
|
|
|
2021-12-28 15:42:35 +00:00
|
|
|
await render(commonTemplate);
|
2019-07-31 00:20:44 +00:00
|
|
|
|
|
|
|
const request = this.server.handledRequests[0];
|
|
|
|
assert.equal(this.server.handledRequests.length, 1, 'One request made');
|
|
|
|
assert.equal(request.url.split('?')[0], url, `URL is ${url}`);
|
|
|
|
assert.deepEqual(
|
|
|
|
request.queryParams,
|
|
|
|
stringifyValues(assign({ origin: 'end', offset: 50000 }, params)),
|
|
|
|
'Query params are correct'
|
|
|
|
);
|
|
|
|
assert.equal(find('[data-test-output]').textContent, 'Hello World');
|
|
|
|
});
|
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('when mode is `streaming` and `isStreaming` is true, streaming starts', async function (assert) {
|
2019-07-31 00:20:44 +00:00
|
|
|
const url = '/file/stream';
|
|
|
|
const params = { path: 'hello/world.txt', limit: 50000 };
|
|
|
|
this.setProperties({
|
|
|
|
logger: makeLogger(url, params),
|
|
|
|
mode: 'streaming',
|
|
|
|
isStreaming: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.ok(true);
|
|
|
|
|
|
|
|
run.later(run, run.cancelTimers, 500);
|
|
|
|
|
2021-12-28 15:42:35 +00:00
|
|
|
await render(commonTemplate);
|
2019-07-31 00:20:44 +00:00
|
|
|
|
|
|
|
const request = this.server.handledRequests[0];
|
|
|
|
assert.equal(request.url.split('?')[0], url, `URL is ${url}`);
|
|
|
|
assert.equal(find('[data-test-output]').textContent, 'Hello World');
|
|
|
|
});
|
2020-06-16 19:51:52 +00:00
|
|
|
|
2021-12-28 14:45:20 +00:00
|
|
|
test('the ctrl+a/cmd+a shortcut selects only the text in the output window', async function (assert) {
|
2020-06-16 19:51:52 +00:00
|
|
|
const url = '/file/endpoint';
|
|
|
|
const params = { path: 'hello/world.txt', offset: 0, limit: 50000 };
|
|
|
|
this.setProperties({
|
|
|
|
logger: makeLogger(url, params),
|
|
|
|
mode: 'head',
|
|
|
|
isStreaming: false,
|
|
|
|
});
|
|
|
|
|
2021-12-28 15:42:35 +00:00
|
|
|
await render(hbs`
|
2020-06-16 19:51:52 +00:00
|
|
|
Extra text
|
|
|
|
<StreamingFile @logger={{logger}} @mode={{mode}} @isStreaming={{isStreaming}} />
|
|
|
|
On either side
|
|
|
|
`);
|
|
|
|
|
|
|
|
// Windows and Linux shortcut
|
2021-12-28 16:08:12 +00:00
|
|
|
await triggerKeyEvent('[data-test-output]', 'keydown', A_KEY, {
|
|
|
|
ctrlKey: true,
|
|
|
|
});
|
2020-06-16 19:51:52 +00:00
|
|
|
assert.equal(
|
2021-12-28 14:45:20 +00:00
|
|
|
window.getSelection().toString().trim(),
|
2020-06-16 19:51:52 +00:00
|
|
|
find('[data-test-output]').textContent.trim()
|
|
|
|
);
|
|
|
|
|
|
|
|
window.getSelection().removeAllRanges();
|
|
|
|
|
|
|
|
// MacOS shortcut
|
2021-12-28 16:08:12 +00:00
|
|
|
await triggerKeyEvent('[data-test-output]', 'keydown', A_KEY, {
|
|
|
|
metaKey: true,
|
|
|
|
});
|
2020-06-16 19:51:52 +00:00
|
|
|
assert.equal(
|
2021-12-28 14:45:20 +00:00
|
|
|
window.getSelection().toString().trim(),
|
2020-06-16 19:51:52 +00:00
|
|
|
find('[data-test-output]').textContent.trim()
|
|
|
|
);
|
|
|
|
});
|
2019-07-31 00:20:44 +00:00
|
|
|
});
|