John Cowen cb0c5309c9 UI: Add EventSource ready for implementing blocking queries (#5070)
- Maintain http headers as JSON-API meta for all API requests (#4946)
- Add EventSource ready for implementing blocking queries
- EventSource project implementation to enable blocking queries for service and node listings (#5267)
- Add setting to enable/disable blocking queries (#5352)
2019-05-01 18:22:06 +00:00

66 lines
1.8 KiB
JavaScript

import domEventSourceCallable, { defaultRunner } from 'consul-ui/utils/dom/event-source/callable';
import { module } from 'qunit';
import test from 'ember-sinon-qunit/test-support/test';
module('Unit | Utility | dom/event-source/callable');
const createEventTarget = function() {
return class {
addEventListener() {}
removeEventListener() {}
dispatchEvent() {}
};
};
const createPromise = function() {
class PromiseMock {
then(cb) {
cb();
return this;
}
catch(cb) {
cb({ message: 'error' });
return this;
}
}
PromiseMock.resolve = function() {
return new PromiseMock();
};
return PromiseMock;
};
test('it creates an EventSource class implementing EventTarget', function(assert) {
const EventTarget = createEventTarget();
const EventSource = domEventSourceCallable(EventTarget, createPromise());
assert.ok(EventSource instanceof Function);
const source = new EventSource();
assert.ok(source instanceof EventTarget);
});
test('the default runner loops and can be closed', function(assert) {
assert.expect(12); // 10 not closed, 1 to close and the final call count
let count = 0;
const isClosed = function() {
count++;
assert.ok(true);
return count === 11;
};
const configuration = {};
const then = this.stub().callsArg(0);
const target = {
source: function(configuration) {
return {
then: then,
};
},
};
defaultRunner(target, configuration, isClosed);
assert.ok(then.callCount == 10);
});
test('it calls the defaultRunner', function(assert) {
const Promise = createPromise();
const EventTarget = createEventTarget();
const run = this.stub();
const EventSource = domEventSourceCallable(EventTarget, Promise, run);
const source = new EventSource();
assert.ok(run.calledOnce);
assert.equal(source.readyState, 2);
});