mirror of
https://github.com/status-im/consul.git
synced 2025-02-26 04:15:25 +00:00
* Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Adding explicit MPL license for sub-package This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository. * Updating the license from MPL to Business Source License Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at <Blog URL>, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl. * add missing license headers * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 * Update copyright file headers to BUSL-1.1 --------- Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
109 lines
3.4 KiB
JavaScript
109 lines
3.4 KiB
JavaScript
/**
|
|
* Copyright (c) HashiCorp, Inc.
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
import domEventSourceCallable from 'consul-ui/utils/dom/event-source/callable';
|
|
import EventTarget from 'consul-ui/utils/dom/event-target/rsvp';
|
|
|
|
import { module, test, skip } from 'qunit';
|
|
import { setupTest } from 'ember-qunit';
|
|
import sinon from 'sinon';
|
|
|
|
module('Integration | Utility | dom/event-source/callable', function (hooks) {
|
|
setupTest(hooks);
|
|
test('it dispatches messages', function (assert) {
|
|
assert.expect(1);
|
|
const EventSource = domEventSourceCallable(EventTarget);
|
|
const listener = sinon.stub();
|
|
const source = new EventSource(
|
|
function (configuration) {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
this.dispatchEvent({
|
|
type: 'message',
|
|
data: null,
|
|
});
|
|
resolve();
|
|
}, configuration.milliseconds);
|
|
});
|
|
},
|
|
{
|
|
milliseconds: 100,
|
|
}
|
|
);
|
|
source.addEventListener('message', function () {
|
|
listener();
|
|
});
|
|
return new Promise(function (resolve) {
|
|
setTimeout(function () {
|
|
source.close();
|
|
assert.equal(listener.callCount, 5);
|
|
resolve();
|
|
}, 550);
|
|
});
|
|
});
|
|
// TODO: rsvp timing seems to have completely changed
|
|
// this test tests an API that is not used within the code
|
|
// (using an EventSource with no callable)
|
|
// so we'll come back here to investigate
|
|
skip('it dispatches a single open event and closes when called with no callable', function (assert) {
|
|
assert.expect(4);
|
|
const EventSource = domEventSourceCallable(EventTarget, Promise);
|
|
const listener = sinon.stub();
|
|
const source = new EventSource();
|
|
source.addEventListener('open', function (e) {
|
|
assert.deepEqual(e.target, this);
|
|
assert.equal(e.target.readyState, 1);
|
|
listener();
|
|
});
|
|
return Promise.resolve().then(function () {
|
|
assert.ok(listener.calledOnce);
|
|
assert.equal(source.readyState, 2);
|
|
});
|
|
});
|
|
test('it dispatches a single open event, and calls the specified callable that can dispatch an event', function (assert) {
|
|
assert.expect(1);
|
|
const EventSource = domEventSourceCallable(EventTarget);
|
|
const listener = sinon.stub();
|
|
const source = new EventSource(function () {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
this.dispatchEvent({
|
|
type: 'message',
|
|
data: {},
|
|
});
|
|
this.close();
|
|
}, 190);
|
|
});
|
|
});
|
|
source.addEventListener('open', function () {
|
|
// open is called first
|
|
listener();
|
|
});
|
|
return new Promise(function (resolve) {
|
|
source.addEventListener('message', function () {
|
|
// message is called second
|
|
assert.ok(listener.calledOnce);
|
|
resolve();
|
|
});
|
|
});
|
|
});
|
|
test("it can be closed before the first tick, and therefore doesn't run", function (assert) {
|
|
assert.expect(4);
|
|
const EventSource = domEventSourceCallable(EventTarget);
|
|
const listener = sinon.stub();
|
|
const source = new EventSource();
|
|
assert.equal(source.readyState, 0);
|
|
source.close();
|
|
assert.equal(source.readyState, 2);
|
|
source.addEventListener('open', function (e) {
|
|
listener();
|
|
});
|
|
return Promise.resolve().then(function () {
|
|
assert.notOk(listener.called);
|
|
assert.equal(source.readyState, 2);
|
|
});
|
|
});
|
|
});
|