From 40c05173a935a9ec319d95f0fad640c43db5e560 Mon Sep 17 00:00:00 2001 From: Ilya Moroz <37909603+ilyamore88@users.noreply.github.com> Date: Thu, 13 Jan 2022 00:46:07 +0300 Subject: [PATCH] [Fix] isFunction function for async functions (#1857) * fix test of async function, fix TS type because Function is banned type * add tests for isFunction function * fix eslint * add changelog for 2.23.1 * fix changelog * fix changelog --- docs/CHANGELOG.md | 1 + src/components/utils.ts | 4 +- test/cypress/tests/utils.spec.ts | 63 ++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 test/cypress/tests/utils.spec.ts diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 52adf8fc..e9a4f930 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -20,6 +20,7 @@ - `New` — `API` — The new `UiApi` section was added. It allows accessing some editor UI nodes and methods. - `Refactoring` — Toolbox became a standalone class instead of a Module. It can be accessed only through the Toolbar module. - `Refactoring` — CI flow optimized. +- `Fix` - Recognize async `onPaste` handlers in tools [#1803](https://github.com/codex-team/editor.js/issues/1803). ### 2.22.3 diff --git a/src/components/utils.ts b/src/components/utils.ts index 8b402bbf..e1e756f6 100644 --- a/src/components/utils.ts +++ b/src/components/utils.ts @@ -194,8 +194,8 @@ export function typeOf(object: any): string { * @returns {boolean} */ // eslint-disable-next-line @typescript-eslint/no-explicit-any -export function isFunction(fn: any): fn is Function { - return typeOf(fn) === 'function'; +export function isFunction(fn: any): fn is (...args: any[]) => any { + return typeOf(fn) === 'function' || typeOf(fn) === 'asyncfunction'; } /** diff --git a/test/cypress/tests/utils.spec.ts b/test/cypress/tests/utils.spec.ts new file mode 100644 index 00000000..bb0cb51a --- /dev/null +++ b/test/cypress/tests/utils.spec.ts @@ -0,0 +1,63 @@ +/* eslint-disable @typescript-eslint/no-empty-function */ +import { isFunction } from '../../../src/components/utils'; + +function syncFunction(): void {} + +async function asyncFunction(): Promise {} + +const syncArrowFunction = (): void => {}; + +const asyncArrowFunction = async (): Promise => {}; + +describe('isFunction function', () => { + it('should recognise sync functions', () => { + /** + * Act + */ + const commonFunctionResult = isFunction(syncFunction); + const arrowFunctionResult = isFunction(syncArrowFunction); + + /** + * Assert + */ + expect(commonFunctionResult).to.eq(true); + expect(arrowFunctionResult).to.eq(true); + }); + + it('should recognise async functions', () => { + /** + * Act + */ + const commonFunctionResult = isFunction(asyncFunction); + const arrowFunctionResult = isFunction(asyncArrowFunction); + + /** + * Assert + */ + expect(commonFunctionResult).to.eq(true); + expect(arrowFunctionResult).to.eq(true); + }); + + it('should return false if it isn\'t a function', () => { + /** + * Arrange + */ + const obj = {}; + const num = 123; + const str = '123'; + + /** + * Act + */ + const objResult = isFunction(obj); + const numResult = isFunction(num); + const strResult = isFunction(str); + + /** + * Assert + */ + expect(objResult).to.eq(false); + expect(numResult).to.eq(false); + expect(strResult).to.eq(false); + }); +});