hashicorp-copywrite[bot] 5fb9df1640
[COMPLIANCE] License changes (#18443)
* 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>
2023-08-11 09:12:13 -04:00

155 lines
4.9 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
import domEventSourceCache from 'consul-ui/utils/dom/event-source/cache';
import { module, test } from 'qunit';
import sinon from 'sinon';
module('Unit | Utility | dom/event-source/cache', function () {
const createEventSource = function () {
return class {
constructor(cb) {
this.source = cb;
this.source.apply(this, arguments);
}
addEventListener() {}
removeEventListener() {}
dispatchEvent() {}
close() {}
};
};
const createPromise = function (
resolve = (result) => result,
reject = (result = { message: 'error' }) => result
) {
class PromiseMock {
constructor(cb = function () {}) {
cb(resolve);
}
then(cb) {
setTimeout(() => cb.bind(this)(resolve()), 0);
return this;
}
catch(cb) {
setTimeout(() => cb.bind(this)(reject()), 0);
return this;
}
}
PromiseMock.resolve = function (result) {
return new PromiseMock(function (resolve) {
resolve(result);
});
};
PromiseMock.reject = function () {
return new PromiseMock();
};
return PromiseMock;
};
test('it returns a function', function (assert) {
const EventSource = createEventSource();
const Promise = createPromise();
const getCache = domEventSourceCache(function () {}, EventSource, Promise);
assert.strictEqual(typeof getCache, 'function');
});
test('getCache returns a function', function (assert) {
const EventSource = createEventSource();
const Promise = createPromise();
const getCache = domEventSourceCache(function () {}, EventSource, Promise);
const obj = {};
const cache = getCache(obj);
assert.strictEqual(typeof cache, 'function');
});
test('cache creates the default EventSource and keeps it open when there is a cursor', function (assert) {
const EventSource = createEventSource();
const stub = {
configuration: { cursor: 1 },
};
const Promise = createPromise(function () {
return stub;
});
const source = sinon.stub().returns(Promise.resolve());
const cb = sinon.stub();
const getCache = domEventSourceCache(source, EventSource, Promise);
const obj = {};
const cache = getCache(obj);
const promisedEventSource = cache(cb, {
key: 'key',
settings: {
enabled: true,
},
});
assert.ok(source.calledOnce, 'promisifying source called once');
assert.ok(promisedEventSource instanceof Promise, 'source returns a Promise');
const retrievedEventSource = cache(cb, {
key: 'key',
settings: {
enabled: true,
},
});
assert.deepEqual(promisedEventSource, retrievedEventSource);
assert.ok(source.calledTwice, 'promisifying source called once');
assert.ok(retrievedEventSource instanceof Promise, 'source returns a Promise');
});
test('cache creates the default EventSource and keeps it open when there is a cursor 2', function (assert) {
assert.expect(4);
const EventSource = createEventSource();
const stub = {
close: sinon.stub(),
configuration: { cursor: 1 },
};
const Promise = createPromise(function () {
return stub;
});
const source = sinon.stub().returns(Promise.resolve());
const cb = sinon.stub();
const getCache = domEventSourceCache(source, EventSource, Promise);
const obj = {};
const cache = getCache(obj);
const promisedEventSource = cache(cb, {
key: 0,
settings: {
enabled: true,
},
});
assert.ok(source.calledOnce, 'promisifying source called once');
assert.ok(cb.calledOnce, 'callable event source callable called once');
assert.ok(promisedEventSource instanceof Promise, 'source returns a Promise');
// >>
return promisedEventSource.then(function () {
assert.notOk(stub.close.called, "close wasn't called");
});
});
test("cache creates the default EventSource and closes it when there isn't a cursor", function (assert) {
assert.expect(4);
const EventSource = createEventSource();
const stub = {
close: sinon.stub(),
configuration: {},
};
const Promise = createPromise(function () {
return stub;
});
const source = sinon.stub().returns(Promise.resolve());
const cb = sinon.stub();
const getCache = domEventSourceCache(source, EventSource, Promise);
const obj = {};
const cache = getCache(obj);
const promisedEventSource = cache(cb, {
key: 0,
});
assert.ok(source.calledOnce, 'promisifying source called once');
assert.ok(cb.calledOnce, 'callable event source callable called once');
assert.ok(promisedEventSource instanceof Promise, 'source returns a Promise');
// >>
return promisedEventSource.then(function () {
assert.ok(stub.close.calledOnce, 'close was called');
});
});
});