mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
* Making sure we have bindings properly set with our Classes + tests
This commit is contained in:
parent
c0dbc2c7f1
commit
f83db32ad5
4 changed files with 67 additions and 13 deletions
58
x-pack/plugins/reporting/common/cancellation_token.test.ts
Normal file
58
x-pack/plugins/reporting/common/cancellation_token.test.ts
Normal file
|
@ -0,0 +1,58 @@
|
|||
/*
|
||||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||
* or more contributor license agreements. Licensed under the Elastic License;
|
||||
* you may not use this file except in compliance with the Elastic License.
|
||||
*/
|
||||
|
||||
import { CancellationToken } from './cancellation_token';
|
||||
|
||||
describe('Cancellation Token', () => {
|
||||
it('registers callbacks for cancellation events', () => {
|
||||
const cancellationToken = new CancellationToken();
|
||||
const onCancelled = jest.fn();
|
||||
cancellationToken.on(onCancelled);
|
||||
cancellationToken.cancel();
|
||||
|
||||
expect(onCancelled).toBeCalled();
|
||||
});
|
||||
|
||||
it('emits a cancellation event immediately when already cancelled', () => {
|
||||
const cancellationToken = new CancellationToken();
|
||||
const onCancelled = jest.fn();
|
||||
cancellationToken.cancel();
|
||||
cancellationToken.on(onCancelled);
|
||||
|
||||
expect(onCancelled).toBeCalled();
|
||||
});
|
||||
|
||||
it('binds the `on` method properly so that it can be passed around', () => {
|
||||
const cancellationToken = new CancellationToken();
|
||||
const onCancelled = jest.fn();
|
||||
const unboundOn = cancellationToken.on;
|
||||
|
||||
cancellationToken.cancel();
|
||||
unboundOn(onCancelled);
|
||||
|
||||
expect(onCancelled).toBeCalled();
|
||||
});
|
||||
|
||||
it('binds the `cancel` method properly so that it can be passed around', () => {
|
||||
const cancellationToken = new CancellationToken();
|
||||
const onCancelled = jest.fn();
|
||||
const unboundCancel = cancellationToken.cancel;
|
||||
|
||||
unboundCancel();
|
||||
cancellationToken.on(onCancelled);
|
||||
|
||||
expect(onCancelled).toBeCalled();
|
||||
});
|
||||
|
||||
it('throws an error when the callback is not a function', () => {
|
||||
const cancellationToken = new CancellationToken();
|
||||
|
||||
expect(() => {
|
||||
// @ts-ignore
|
||||
cancellationToken.on('cool!');
|
||||
}).toThrowError('Expected callback to be a function');
|
||||
});
|
||||
});
|
|
@ -8,14 +8,14 @@ import { isFunction } from 'lodash';
|
|||
|
||||
export class CancellationToken {
|
||||
private isCancelled: boolean;
|
||||
private _callbacks: any[];
|
||||
private _callbacks: Function[];
|
||||
|
||||
constructor() {
|
||||
this.isCancelled = false;
|
||||
this._callbacks = [];
|
||||
}
|
||||
|
||||
on(callback: Function) {
|
||||
public on = (callback: Function) => {
|
||||
if (!isFunction(callback)) {
|
||||
throw new Error('Expected callback to be a function');
|
||||
}
|
||||
|
@ -26,10 +26,10 @@ export class CancellationToken {
|
|||
}
|
||||
|
||||
this._callbacks.push(callback);
|
||||
}
|
||||
};
|
||||
|
||||
cancel() {
|
||||
public cancel = () => {
|
||||
this.isCancelled = true;
|
||||
this._callbacks.forEach(callback => callback());
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -58,8 +58,7 @@ function executeJobFn(server) {
|
|||
}))
|
||||
);
|
||||
|
||||
const boundCancelOn = cancellationToken.on.bind(cancellationToken);
|
||||
const stop$ = Rx.fromEventPattern(boundCancelOn);
|
||||
const stop$ = Rx.fromEventPattern(cancellationToken.on);
|
||||
|
||||
return process$.pipe(takeUntil(stop$)).toPromise();
|
||||
});
|
||||
|
|
|
@ -17,14 +17,12 @@ describe('CancellationToken', function () {
|
|||
describe('on', function () {
|
||||
[true, null, undefined, 1, 'string', {}, []].forEach(function (value) {
|
||||
it(`should throw an Error if value is ${value}`, function () {
|
||||
const boundOn = cancellationToken.on.bind(cancellationToken);
|
||||
expect(boundOn).withArgs(value).to.throwError();
|
||||
expect(cancellationToken.on).withArgs(value).to.throwError();
|
||||
});
|
||||
});
|
||||
|
||||
it('accepts a function', function () {
|
||||
const boundOn = cancellationToken.on.bind(cancellationToken);
|
||||
expect(boundOn).withArgs(function () {}).not.to.throwError();
|
||||
expect(cancellationToken.on).withArgs(function () {}).not.to.throwError();
|
||||
});
|
||||
|
||||
it(`calls function if cancel has previously been called`, function () {
|
||||
|
@ -37,8 +35,7 @@ describe('CancellationToken', function () {
|
|||
|
||||
describe('cancel', function () {
|
||||
it('should be a function accepting no parameters', function () {
|
||||
const boundCancel = cancellationToken.cancel.bind(cancellationToken);
|
||||
expect(boundCancel).withArgs().to.not.throwError();
|
||||
expect(cancellationToken.cancel).withArgs().to.not.throwError();
|
||||
});
|
||||
|
||||
it('should call a single callback', function () {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue