open-nomad/ui/tests/unit/utils/stream-logger-test.js
Buck Doyle 07831ab455
Update to v4 of Ember Power Select (#10226)
This closes #10146.

Because of cibernox/ember-power-select#1203, which documents
the current impossibility of attaching test selectors to a
PowerSelect invocation, this uses test selectors on parent
containers instead, occasionally adding wrappers when needed.
I chose to leave the existing test selectors in the hopes that
we can return to using them eventually, but I could easily
remove them if it seems like extra noise now.

Presumably for the same reason, @class no longer works, so
this adjusts the scoping of global search CSS to preserve the style
of the search control.

I also included an update to the latest version of
ember-test-selectors, since we were far behind and I tried
that before finding the aforelinked issue.

Finally, this replaces ember-cli-uglify with ember-cli-terser to address
production build failures as described at ember-cli/ember-cli#9290.
2021-03-26 08:55:12 -05:00

97 lines
2.2 KiB
JavaScript

import { module, test } from 'qunit';
import { Promise } from 'rsvp';
import sinon from 'sinon';
import StreamLogger from 'nomad-ui/utils/classes/stream-logger';
module('Unit | Util | StreamLogger', function() {
test('when a StreamLogger is stopped before the poll request responds, the request is immediately canceled upon completion', async function(assert) {
const fetchMock = new FetchMock();
const fetch = fetchMock.request();
const logger = StreamLogger.create({
logFetch: () => fetch,
});
logger.start();
await logger.stop();
assert.notOk(logger.poll.isRunning);
assert.equal(fetchMock.reader.cancel.callCount, 0);
fetchMock.closeRequest();
await fetch;
assert.equal(fetchMock.reader.cancel.callCount, 1);
});
test('when the streaming request sends the done flag, the poll task completes', async function(assert) {
const fetchMock = new FetchMock();
const fetch = fetchMock.request();
const logger = StreamLogger.create({
logFetch: () => fetch,
});
logger.start();
assert.ok(logger.poll.isRunning);
assert.equal(fetchMock.reader.readSpy.callCount, 0);
fetchMock.closeRequest();
await fetch;
assert.notOk(logger.poll.isRunning);
assert.equal(fetchMock.reader.readSpy.callCount, 1);
});
});
class FetchMock {
constructor() {
this._closeRequest = null;
this.reader = new ReadableStreamMock();
this.response = new FetchResponseMock(this.reader);
}
request() {
if (this._closeRequest) {
throw new Error('Can only call FetchMock.request once');
}
return new Promise(resolve => {
this._closeRequest = resolve;
});
}
closeRequest() {
if (this._closeRequest) {
this._closeRequest(this.response);
} else {
throw new Error('Must call FetchMock.request() before FetchMock.closeRequest');
}
}
}
class FetchResponseMock {
constructor(reader) {
this.reader = reader;
this.body = {
getReader() {
return reader;
},
};
}
}
class ReadableStreamMock {
constructor() {
this.cancel = sinon.spy();
this.readSpy = sinon.spy();
}
read() {
this.readSpy();
return new Promise(resolve => {
resolve({ value: new ArrayBuffer(0), done: true });
});
}
}