From 2ef3e38e987c54bba48b846e6916808423fa4680 Mon Sep 17 00:00:00 2001 From: xdan Date: Fri, 3 Nov 2023 12:32:53 +0300 Subject: [PATCH] Calls to setTimout without the async module have been removed from autotests, and most of the asynchronous tests have been rewritten from done to async/await --- CHANGELOG.md | 12 + src/core/async/async.test.js | 108 +++---- src/core/async/async.ts | 7 +- .../event-emitter/object-observer.test.js | 182 ++++++----- src/modules/messages/messages.test.js | 11 +- src/modules/uploader/uploader.test.js | 17 +- src/plugins/backspace/backspace.test.js | 76 ++--- src/plugins/clean-html/clean-html.test.js | 17 +- src/plugins/iframe/iframe.test.js | 2 +- src/plugins/limit/limit.test.js | 16 +- src/plugins/resizer/resizer.test.js | 283 ++++++++---------- src/plugins/source/source.test.js | 8 +- test/bootstrap.js | 20 +- test/tests/acceptance/commands.test.js | 1 + test/tests/acceptance/inline-mode.test.js | 8 +- 15 files changed, 370 insertions(+), 398 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e850a93e5..c8bc6a6a5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,16 +9,28 @@ > - :house: [Internal] > - :nail_care: [Polish] +## 4.0.0-beta.97 + +#### :house: Internal + +- Calls to setTimout without the async module have been removed from autotests, and most of the asynchronous tests have been rewritten from done to async/await + ## 4.0.0-beta.96 +#### :boom: Breaking Change + - Removed `Jodit.modules.Helpers.val` method ## 4.0.0-beta.95 +### :bug: Bug Fix + - Fixed the logic of the file upload module. When HTTP errors were simply ignored. ## 4.0.0-beta.93 +### :bug: Bug Fix + - Fixed a bug with the `editor.selection.setCursorIn(box)` method, which could set the cursor inside a
. ## 4.0.0-beta.91 diff --git a/src/core/async/async.test.js b/src/core/async/async.test.js index e664ed816..ed15d4067 100644 --- a/src/core/async/async.test.js +++ b/src/core/async/async.test.js @@ -18,7 +18,7 @@ describe('Test Async module', () => { }); describe('All async tasks', () => { - it('Should be called', done => { + it('Should be called', async () => { asyncM.setTimeout(callSpy, 100); asyncM .promise(r => Promise.resolve().then(r)) @@ -26,14 +26,12 @@ describe('Test Async module', () => { .catch(e => null); asyncM.requestIdleCallback(callSpy); - setTimeout(() => { - expect(callCount).equals(3); - done(); - }, 200); + await delay(200); + expect(callCount).equals(3); }); describe('After View was destroyed', () => { - it('Should not be called', done => { + it('Should not be called', async () => { asyncM.setTimeout(callSpy, 100); asyncM .promise(r => Promise.resolve().then(r)) @@ -41,15 +39,12 @@ describe('Test Async module', () => { .catch(e => null); asyncM.requestIdleCallback(callSpy); asyncM.destruct(); - - setTimeout(() => { - expect(callCount).equals(0); - done(); - }, 200); + await delay(200); + expect(callCount).equals(0); }); describe('Jodit instance', () => { - it('Should work same way', done => { + it('Should work same way', async () => { const editor = getJodit(); editor.async.setTimeout(callSpy, 100); editor.async @@ -58,78 +53,63 @@ describe('Test Async module', () => { .catch(e => null); editor.async.requestIdleCallback(callSpy); editor.destruct(); - - setTimeout(() => { - expect(callCount).equals(0); - done(); - }, 200); + await delay(200); + expect(callCount).equals(0); }); }); }); }); describe('Promise', () => { - it('Should has method for rejection in the outside', done => { + it('Should has method for rejection in the outside', async () => { const promise = asyncM.promise(r => Promise.resolve().then(r)); promise.then(callSpy).catch(e => null); promise.rejectCallback(); - setTimeout(() => { - expect(callCount).equals(0); - done(); - }, 200); + await delay(200); + expect(callCount).equals(0); }); }); describe('setTimeout', () => { - it('Should can be called with number timeout', done => { + it('Should can be called with number timeout', async () => { asyncM.setTimeout(callSpy, 100); - setTimeout(() => { - expect(callCount).equals(1); - done(); - }, 200); + await delay(200); + expect(callCount).equals(1); }); - it('Should can be called with options', done => { + it('Should can be called with options', async () => { asyncM.setTimeout(callSpy, { timeout: 100 }); - setTimeout(() => { - expect(callCount).equals(1); - done(); - }, 200); + await delay(200); + expect(callCount).equals(1); }); describe('Clear', () => { - it('Should can be cleared with timeout id', done => { + it('Should can be cleared with timeout id', async () => { const id = asyncM.setTimeout(callSpy, { timeout: 100 }); asyncM.clearTimeout(id); - setTimeout(() => { - expect(callCount).equals(0); - done(); - }, 200); + await delay(200); + expect(callCount).equals(0); }); - it('Should can be cleared with timeout id', done => { + it('Should can be cleared with timeout id', async () => { const id = asyncM.setTimeout(callSpy, { timeout: 100 }); asyncM.clearTimeout(id); - setTimeout(() => { - expect(callCount).equals(0); - done(); - }, 200); + await delay(200); + expect(callCount).equals(0); }); describe('With label', () => { - it('Should can be cleared with label', done => { + it('Should can be cleared with label', async () => { asyncM.setTimeout(callSpy, { timeout: 100, label: 'test' }); asyncM.clearTimeout('test'); - setTimeout(() => { - expect(callCount).equals(0); - done(); - }, 200); + await delay(200); + expect(callCount).equals(0); }); describe('Several call with one label', () => { - it('Should clear previous call', done => { + it('Should clear previous call', async () => { asyncM.setTimeout(callSpy, { timeout: 100, label: 'test' @@ -150,17 +130,16 @@ describe('Test Async module', () => { timeout: 100 }); - setTimeout(() => { - expect(callCount).equals(2); - done(); - }, 200); + await delay(200); + + expect(callCount).equals(2); }); }); }); }); describe('Update', () => { - it('Can be updated', done => { + it('Can be updated', async () => { const time = new Date().getTime(); let callTime; @@ -174,16 +153,15 @@ describe('Test Async module', () => { } ); - setTimeout(() => { - asyncM.updateTimeout('test', 100); - setTimeout(() => { - asyncM.updateTimeout('test', 100); - setTimeout(() => { - expect(callTime - time).is.above(200); - done(); - }, 200); - }, 70); - }, 70); + await delay(70); + + asyncM.updateTimeout('test', 100); + + await delay(70); + asyncM.updateTimeout('test', 100); + + await delay(200); + expect(callTime - time).is.above(200); }); }); }); @@ -191,7 +169,7 @@ describe('Test Async module', () => { describe('requestAnimationFrame', () => { it('Should call as usual requestAnimationFrame', done => { asyncM.requestAnimationFrame(callSpy); - requestAnimationFrame(() => { + /*ok*/ requestAnimationFrame(() => { expect(callCount).equals(1); done(); }); @@ -201,7 +179,7 @@ describe('Test Async module', () => { it('Should not be called after destruct', done => { asyncM.requestAnimationFrame(callSpy); asyncM.destruct(); - requestAnimationFrame(() => { + /*ok*/ requestAnimationFrame(() => { expect(callCount).equals(0); done(); }); diff --git a/src/core/async/async.ts b/src/core/async/async.ts index 67dad1247..f91263b55 100644 --- a/src/core/async/async.ts +++ b/src/core/async/async.ts @@ -26,6 +26,7 @@ import { isPromise } from 'jodit/core/helpers/checker/is-promise'; import { isString } from 'jodit/core/helpers/checker/is-string'; import { isNumber } from 'jodit/core/helpers/checker/is-number'; import { assert } from 'jodit/core/helpers/utils/assert'; +import { isVoid } from 'jodit/core/helpers/checker/is-void'; import { IS_ES_NEXT } from 'jodit/core/constants'; type Callback = (...args: any[]) => void; @@ -40,7 +41,7 @@ export class Async implements IAsync { setTimeout( callback: Callback, - timeout: number | IAsyncParams, + timeout: number | IAsyncParams | undefined, ...args: any[] ): number { if (this.isDestructed) { @@ -49,6 +50,10 @@ export class Async implements IAsync { let options: IAsyncParams = {}; + if (isVoid(timeout)) { + timeout = 0; + } + if (!isNumber(timeout)) { options = timeout; timeout = options.timeout || 0; diff --git a/src/core/event-emitter/object-observer.test.js b/src/core/event-emitter/object-observer.test.js index a97d60ec8..f8bcc62e3 100644 --- a/src/core/event-emitter/object-observer.test.js +++ b/src/core/event-emitter/object-observer.test.js @@ -4,8 +4,8 @@ * Copyright (c) 2013-2023 Valeriy Chupurnov. All rights reserved. https://xdsoft.net */ -describe('Test object observer', function () { - const getTestObject = function () { +describe('Test object observer', () => { + const getTestObject = () => { return { editable: true, disabled: false, @@ -56,8 +56,8 @@ describe('Test object observer', function () { const that = this; this.countCall++; - return new Promise(function (res) { - setTimeout(function () { + return this.async.promise(res => { + this.async.setTimeout(() => { that.countPromiseCall++; res(); }, 100); @@ -73,8 +73,8 @@ describe('Test object observer', function () { return A; }; - describe('Test debounce decorator', function () { - it('Should call method only once in time', function (done) { + describe('Test debounce decorator', () => { + it('Should call method only once in time', async () => { const result = [], AClass = A(result, 'state.editable'); @@ -93,14 +93,12 @@ describe('Test object observer', function () { a.callTime(); expect(a.countCall).eq(0); - setTimeout(() => { - expect(a.countCall).eq(1); - done(); - }, 500); + await delay(120); + expect(a.countCall).eq(1); }); describe('Compose with `wait`', () => { - it('Should work correct', function (done) { + it('Should work correct', async () => { const result = [], AClass = A(result, 'state.editable'); @@ -122,27 +120,24 @@ describe('Test object observer', function () { a.callTime(); expect(a.countCall).eq(0); - setTimeout(() => { - expect(a.countCall).eq(0); - a.someFlag = 4; - - a.callTime(); - expect(a.countCall).eq(0); - a.callTime(); - expect(a.countCall).eq(0); - a.callTime(); - expect(a.countCall).eq(0); - - setTimeout(() => { - expect(a.countCall).eq(2); - done(); - }, 500); - }, 500); + await delay(120); + expect(a.countCall).eq(0); + a.someFlag = 4; + + a.callTime(); + expect(a.countCall).eq(0); + a.callTime(); + expect(a.countCall).eq(0); + a.callTime(); + expect(a.countCall).eq(0); + + await delay(120); + expect(a.countCall).eq(2); }); }); - describe('Options', function () { - it('Should call method only once in time', function (done) { + describe('Options', () => { + it('Should call method only once in time', async () => { const result = [], AClass = A(result, 'state.editable'); @@ -165,15 +160,13 @@ describe('Test object observer', function () { a.callTime(); expect(a.countCall).eq(0); - setTimeout(() => { - expect(a.countCall).eq(1); - done(); - }, 500); + await delay(120); + expect(a.countCall).eq(1); }); }); - describe('Promisify debounce decorator', function () { - it('Should call method only once in time', function (done) { + describe('Promisify debounce decorator', () => { + it('Should call method only once in time', async () => { unmockPromise(); const result = [], @@ -194,33 +187,32 @@ describe('Test object observer', function () { let counter = 0; - a.callPromiseTime().then(function () { - counter++; - expect(a.countPromiseCall).eq(1); - }); - - a.callPromiseTime().then(function () { - counter++; - expect(a.countPromiseCall).eq(1); - }); - - a.callPromiseTime().then(function () { - counter++; - expect(a.countPromiseCall).eq(1); - }); + await Promise.all([ + a.callPromiseTime().then(() => { + counter++; + expect(a.countPromiseCall).eq(1); + }), + + a.callPromiseTime().then(() => { + counter++; + expect(a.countPromiseCall).eq(1); + }), + + a.callPromiseTime().then(() => { + counter++; + expect(a.countPromiseCall).eq(1); + }) + ]); - setTimeout(() => { - expect(counter).eq(3); - expect(a.countCall).eq(1); - expect(a.countPromiseCall).eq(1); - done(); - }, 500); + expect(counter).eq(3); + expect(a.countCall).eq(1); + expect(a.countPromiseCall).eq(1); }); }); }); - describe('Test watch decorator', function () { - it('Should add watcher to whole field object', function () { + describe('Test watch decorator', () => { + it('Should add watcher to whole field object', () => { const result = [], AClass = A(result, 'state.editable'); @@ -246,7 +238,7 @@ describe('Test object observer', function () { ]); }); - it('Should add watcher to some field in Component', function () { + it('Should add watcher to some field in Component', () => { const result = [], AClass = A(result, 'state.some.element.enable'); @@ -267,9 +259,9 @@ describe('Test object observer', function () { ]); }); - describe('Add several watchers', function () { - describe('on same fields', function () { - it('Should call all handlers', function () { + describe('Add several watchers', () => { + describe('on same fields', () => { + it('Should call all handlers', () => { const result = [], AClass = A(result, 'state.some.element.enable'); @@ -298,8 +290,8 @@ describe('Test object observer', function () { }); }); - describe('on different fields', function () { - it('Should call only matched handlers', function () { + describe('on different fields', () => { + it('Should call only matched handlers', () => { const result = [], AClass = A( result, @@ -334,8 +326,8 @@ describe('Test object observer', function () { }); }); - describe('On change field', function () { - it('Should fire change all parent field', function () { + describe('On change field', () => { + it('Should fire change all parent field', () => { const result = [], AClass = A(result, 'state.some.element.one'); @@ -356,7 +348,7 @@ describe('Test object observer', function () { ['A', 15] ]); }); - it('Should add in handler - old value as first argument', function () { + it('Should add in handler - old value as first argument', () => { const result = [], AClass = A(result, 'state.some.element.one'); @@ -379,8 +371,8 @@ describe('Test object observer', function () { }); }); - describe('Test safe stringify', function () { - it('Should safe stringify any circular object to string', function () { + describe('Test safe stringify', () => { + it('Should safe stringify any circular object to string', () => { const a = {}, b = getTestObject(); @@ -397,9 +389,9 @@ describe('Test object observer', function () { }); }); - describe('Test object properties', function () { - describe('Observed object', function () { - it('Should has only own object properties', function () { + describe('Test object properties', () => { + describe('Observed object', () => { + it('Should has only own object properties', () => { const a = { a: 1, b: 2 }; const observed = Jodit.modules.observable(a); expect(Object.keys(observed)).deep.equals(Object.keys(a)); @@ -407,10 +399,10 @@ describe('Test object observer', function () { }); }); - describe('Test equal checker', function () { - describe('Two object', function () { - describe('Check one object', function () { - it('Should check that is one object', function () { + describe('Test equal checker', () => { + describe('Two object', () => { + describe('Check one object', () => { + it('Should check that is one object', () => { const a = {}, b = []; @@ -420,21 +412,21 @@ describe('Test object observer', function () { }); }); - describe('Check scalar value', function () { - it('Should check normal', function () { + describe('Check scalar value', () => { + it('Should check normal', () => { expect( isEqual( - function () {}, - function () {} + () => {}, + () => {} ) ).is.true; expect( isEqual( - function () { + () => { return 1; }, - function () {} + () => {} ) ).is.false; @@ -446,8 +438,8 @@ describe('Test object observer', function () { }); }); - describe('Check array', function () { - it('Should deep check', function () { + describe('Check array', () => { + it('Should deep check', () => { expect(isEqual([1], [1])).is.true; expect(isEqual([1], [2])).is.false; expect(isEqual(['test'], ['test'])).is.true; @@ -455,8 +447,8 @@ describe('Test object observer', function () { }); }); - describe('Check ref object', function () { - it('Should deep check and add instead ref some const', function () { + describe('Check ref object', () => { + it('Should deep check and add instead ref some const', () => { const a = getTestObject(), b = getTestObject(); @@ -478,8 +470,8 @@ describe('Test object observer', function () { }); }); - describe('Event on change', function () { - it('Should fire event when field value was changed', function () { + describe('Event on change', () => { + it('Should fire event when field value was changed', () => { const counter = []; const data = Jodit.modules.observable(getTestObject()); @@ -497,8 +489,8 @@ describe('Test object observer', function () { expect(counter).to.deep.equal(['editable', 'some.element.one']); }); - describe('Key change event', function () { - it('Should fire event.key when field value was changed', function () { + describe('Key change event', () => { + it('Should fire event.key when field value was changed', () => { const counter = []; const data = Jodit.modules.observable(getTestObject()); @@ -516,7 +508,7 @@ describe('Test object observer', function () { expect(counter).to.deep.equal(['some.element.one']); }); - it('Should fire event with old and new Value', function () { + it('Should fire event with old and new Value', () => { const counter = []; const data = Jodit.modules.observable(getTestObject()); @@ -542,12 +534,12 @@ describe('Test object observer', function () { }); }); - describe('Change watched property', function () { - it('Should fire handler', function () { + describe('Change watched property', () => { + it('Should fire handler', () => { const counter = [], obj = { mode: 'top', - methodA: function () { + methodA: () => { counter.push(obj.mode); } }; @@ -565,8 +557,8 @@ describe('Test object observer', function () { }); }); - describe('Change whole branch', function () { - it('Should fire event.key when field value was changed', function () { + describe('Change whole branch', () => { + it('Should fire event.key when field value was changed', () => { const counter = []; const data = Jodit.modules.observable(getTestObject()); diff --git a/src/modules/messages/messages.test.js b/src/modules/messages/messages.test.js index 3d54400b1..7640be4de 100644 --- a/src/modules/messages/messages.test.js +++ b/src/modules/messages/messages.test.js @@ -34,12 +34,13 @@ describe('Test Messages module', () => { editor.container.querySelector('.jodit-ui-message_active_true') ).is.not.null; - setTimeout(() => { + editor.async.setTimeout(() => { expect( editor.container.querySelector( '.jodit-ui-message_active_true' ) ).is.null; + done(); }, 200); }); @@ -60,19 +61,19 @@ describe('Test Messages module', () => { expect(editor.container.querySelector('.jodit-ui-message')).is .not.null; - setTimeout(() => { + editor.async.setTimeout(() => { editor.message.success('Hello Mars!', 150); - setTimeout(() => { + editor.async.setTimeout(() => { editor.message.success('Hello Mars!', 150); }, 100); }, 100); - setTimeout(() => { + editor.async.setTimeout(() => { expect(editor.container.querySelector('.jodit-ui-message')) .is.not.null; done(); - }, 300); + }, 400); }); }); }); diff --git a/src/modules/uploader/uploader.test.js b/src/modules/uploader/uploader.test.js index b4d33062b..a7debe9a9 100644 --- a/src/modules/uploader/uploader.test.js +++ b/src/modules/uploader/uploader.test.js @@ -133,8 +133,13 @@ describe('Test uploader module', function () { describe('For iframe mode', function () { it('Should upload file and insert image with SRC from server', function (done) { - const timer = setTimeout(function () { - done('Timeout error'); + let isFinished = false; + + const timer = /*ok*/ setTimeout(function () { + if (!isFinished) { + isFinished = true; + done('Timeout error'); + } }, 4000); const file = new FileImage(), @@ -148,7 +153,13 @@ describe('Test uploader module', function () { }, events: { afterInsertImage: function (img) { + if (isFinished) { + return; + } + try { + isFinished = true; + clearTimeout(timer); expect(img.src).equals( @@ -170,7 +181,7 @@ describe('Test uploader module', function () { editor.value = '

test|

'; setCursorToChar(editor); - setTimeout(function () { + editor.async.setTimeout(function () { simulateEvent( 'drop', diff --git a/src/plugins/backspace/backspace.test.js b/src/plugins/backspace/backspace.test.js index 66f4b1844..809d5aa84 100644 --- a/src/plugins/backspace/backspace.test.js +++ b/src/plugins/backspace/backspace.test.js @@ -5,14 +5,23 @@ */ describe('Backspace/Delete key', function () { - let editor, range; + let editor, range, area; - beforeEach(function () { - editor = getJodit(); + beforeEach(() => { + area = appendTestArea(undefined, false); + unmockPromise(); + editor = getJodit({}, area); editor.value = '

test

'; range = editor.s.createRange(true); }); + afterEach(() => { + editor.destruct(); + area.remove(); + editor = null; + range = null; + }); + describe('More cases', function () { [ '
t|est
  1. test2
  2. te|st3
  3. test4
=>
t|
  1. st3
  2. test4
', @@ -69,10 +78,17 @@ describe('Backspace/Delete key', function () { describe(`For key "${key}"`, function () { it(`Should be ${value}`, async () => { - editor.destruct(); - const jodit = getJodit( - (options && JSON.parse(options)) || {} - ); + let jodit = editor; + + if (options) { + editor.destruct(); + + jodit = getJodit( + (options && JSON.parse(options)) || {}, + area + ); + } + jodit.value = key; setCursorToChar(jodit); simulateEvent( @@ -753,31 +769,31 @@ describe('Backspace/Delete key', function () { describe('HR has different display style', function () { it('Should also remove HR but cursor should leave inside P', function () { - const editor = getJodit({ - iframe: true, - iframeStyle: - Jodit.defaultOptions.iframeStyle + - 'hr {display: inline-block;}' - }); + editor.destruct(); + const jodit = getJodit( + { + iframe: true, + iframeStyle: + Jodit.defaultOptions.iframeStyle + + 'hr {display: inline-block;}' + }, + area + ); - editor.value = '

lets


test

'; + jodit.value = '

lets


test

'; - const range = editor.s.createRange(); + const range = jodit.s.createRange(); // set cursor in start of element - range.selectNodeContents(editor.editor.lastChild); + range.selectNodeContents(jodit.editor.lastChild); range.collapse(true); - editor.s.selectRange(range); + jodit.s.selectRange(range); - simulateEvent( - 'keydown', - Jodit.KEY_BACKSPACE, - editor.editor - ); + simulateEvent('keydown', Jodit.KEY_BACKSPACE, jodit.editor); - editor.s.insertNode(editor.createInside.text(' 2 ')); - expect(editor.value).equals('

lets

2 test

'); + jodit.s.insertNode(jodit.createInside.text(' 2 ')); + expect(jodit.value).equals('

lets

2 test

'); }); }); }); @@ -801,8 +817,6 @@ describe('Backspace/Delete key', function () { describe('H1 with BR', function () { it('Should simple remove this H1', function () { - const editor = getJodit(); - editor.value = '


|test

'; setCursorToChar(editor); @@ -819,8 +833,6 @@ describe('Backspace/Delete key', function () { describe('Delete', function () { it('Should simple remove this H1', function () { - const editor = getJodit(); - editor.value = '

test|

'; setCursorToChar(editor); @@ -831,8 +843,6 @@ describe('Backspace/Delete key', function () { describe('H1 with BR', function () { it('Should simple remove this H1', function () { - const editor = getJodit(); - editor.value = '

test|


'; setCursorToChar(editor); simulateEvent( @@ -850,8 +860,6 @@ describe('Backspace/Delete key', function () { describe('inside empty TD', function () { it('Should doing nothing', function () { - const editor = getJodit(); - editor.value = '' + '' + '
'; @@ -899,7 +907,6 @@ describe('Backspace/Delete key', function () { describe('Cursor after/before element', function () { describe('Backspace key', function () { it('Should remove that element', function () { - const editor = getJodit(); editor.value = '

|test

'; setCursorToChar(editor); @@ -913,7 +920,6 @@ describe('Backspace/Delete key', function () { describe('After P before Table', function () { it('Should remove P', function () { - const editor = getJodit(); editor.value = '


1
'; @@ -943,7 +949,6 @@ describe('Backspace/Delete key', function () { describe('Delete key', function () { it('Should remove that element', function () { - const editor = getJodit(); editor.value = '

test|

'; setCursorToChar(editor); @@ -959,7 +964,6 @@ describe('Backspace/Delete key', function () { describe('After contenteditable false', () => { it('Should remove this element', () => { - const editor = getJodit(); editor.value = '
Page break

|

'; setCursorToChar(editor); diff --git a/src/plugins/clean-html/clean-html.test.js b/src/plugins/clean-html/clean-html.test.js index 7915f75ad..b596a7ed5 100644 --- a/src/plugins/clean-html/clean-html.test.js +++ b/src/plugins/clean-html/clean-html.test.js @@ -217,7 +217,7 @@ describe('Clean html plugin', function () { }); describe('History', function () { - it('Should not change history stack length', function (done) { + it('Should not change history stack length', async () => { const editor = getJodit({ cleanHTML: { timeout: 0 @@ -227,14 +227,15 @@ describe('Clean html plugin', function () { editor.value = '

test old test

'; expect(editor.history.length).eq(1); - editor.e.on('finishedCleanHTMLWorker', () => { - expect(editor.value).equals( - '

test old test

' - ); + await editor.async.promise(resolve => + editor.e.on('finishedCleanHTMLWorker', resolve) + ); - expect(editor.history.length).eq(1); - done(); - }); + expect(editor.value).equals( + '

test old test

' + ); + + expect(editor.history.length).eq(1); }); describe('Replace old tags', function () { diff --git a/src/plugins/iframe/iframe.test.js b/src/plugins/iframe/iframe.test.js index d46466734..64c9aabac 100644 --- a/src/plugins/iframe/iframe.test.js +++ b/src/plugins/iframe/iframe.test.js @@ -274,7 +274,7 @@ resolve(); }; - setTimeout(function () { + jodit.async.setTimeout(function () { resolve(); }, 4000); diff --git a/src/plugins/limit/limit.test.js b/src/plugins/limit/limit.test.js index 8d49bdeb8..5fff9bd9e 100644 --- a/src/plugins/limit/limit.test.js +++ b/src/plugins/limit/limit.test.js @@ -18,7 +18,7 @@ describe('Limit plugin', function () { editor.value = '11111'; editor.s.insertHTML('a'); - setTimeout(() => { + editor.async.setTimeout(() => { expect(editor.value).equals('

11111

'); done(); }, 200); @@ -64,11 +64,11 @@ describe('Limit plugin', function () { limitChars: 5 }); - editor.value = '

11111

'; + editor.value = '

|11111|

'; setCursorToChar(editor); editor.e.on('keyup.limit', e => { - setTimeout(() => { + editor.async.setTimeout(() => { try { expect(e.ctrlKey).is.true; expect(e.defaultPrevented).is.false; @@ -99,7 +99,7 @@ describe('Limit plugin', function () { editor.s.insertHTML('a'); editor.s.insertHTML('a'); - setTimeout(() => { + editor.async.setTimeout(() => { expect(editor.value).equals('

a1111

'); const chars = editor.statusbar.container.querySelector( '.jodit-status-bar__item' @@ -132,7 +132,7 @@ describe('Limit plugin', function () { expect(simulateEvent('keydown', 'v', editor.editor)).is .false; - setTimeout(() => { + editor.async.setTimeout(() => { expect(editor.value).equals('

11111

'); const chars = editor.statusbar.container.querySelector( @@ -168,7 +168,7 @@ describe('Limit plugin', function () { }; }); - setTimeout(() => { + editor.async.setTimeout(() => { expect(editor.value).equals('

11111

'); done(); }, 200); @@ -194,7 +194,7 @@ describe('Limit plugin', function () { } })); - setTimeout(() => { + editor.async.setTimeout(() => { expect(editor.value).equals('

11111

'); done(); }, 200); @@ -230,7 +230,7 @@ describe('Limit plugin', function () { }; const timeout = () => { - setTimeout(() => { + editor.async.setTimeout(() => { expect(editor.value).equals('

11111 aaa aaa

'); done(); }, 200); diff --git a/src/plugins/resizer/resizer.test.js b/src/plugins/resizer/resizer.test.js index 767d7f96e..067b5eb8a 100644 --- a/src/plugins/resizer/resizer.test.js +++ b/src/plugins/resizer/resizer.test.js @@ -4,10 +4,10 @@ * Copyright (c) 2013-2023 Valeriy Chupurnov. All rights reserved. https://xdsoft.net */ -describe('Resize plugin', function () { - describe('Resize box', function () { - describe('In relative object', function () { - it('should be in front of image', function () { +describe('Resize plugin', () => { + describe('Resize box', () => { + describe('In relative object', () => { + it('should be in front of image', () => { const div = document.createElement('div'); div.innerHTML = '
\n' + @@ -40,8 +40,8 @@ describe('Resize plugin', function () { }); }); - describe('After resize - popup', function () { - it('should be hidden and after this should be shown', function () { + describe('After resize - popup', () => { + it('should be hidden and after this should be shown', () => { const div = document.createElement('div'); div.innerHTML = '
\n' + @@ -95,7 +95,7 @@ describe('Resize plugin', function () { }); }); - describe('Resize image', function () { + describe('Resize image', () => { const resizeImage = function (editor) { const img = editor.editor.querySelector('img'); @@ -124,7 +124,7 @@ describe('Resize plugin', function () { }); }; - describe('Size box', function () { + describe('Size box', () => { it('Should show size for image', function (done) { const editor = getJodit({ history: { @@ -182,14 +182,14 @@ describe('Resize plugin', function () { expect(sizer.style.opacity).equals('1'); - setTimeout(function () { + editor.async.setTimeout(() => { expect(sizer.style.opacity).equals('0'); done(); }, 500); }); - describe('For small state', function () { - it('Should hide size', function () { + describe('For small state', () => { + it('Should hide size', () => { const editor = getJodit({ history: { timeout: 0 @@ -251,7 +251,7 @@ describe('Resize plugin', function () { describe('Save aspect ratio', () => { describe('Disable useAspectRatio option', () => { - it("should don't save it", done => { + it("should don't save it", async () => { const editor = getJodit({ resizer: { useAspectRatio: false @@ -261,48 +261,39 @@ describe('Resize plugin', function () { editor.value = '

'; - onLoadImage( - editor.editor.querySelector('img'), - () => { - resizeImage(editor); + await onLoadImage( + editor.editor.querySelector('img') + ); + resizeImage(editor); - expect(sortAttributes(editor.value)).eq( - '

' - ); - done(); - } + expect(sortAttributes(editor.value)).eq( + '

' ); }); describe('With Alt button', () => { - it("should don't save it", done => { + it("should don't save it", async () => { const editor = getJodit(); editor.value = '

'; - onLoadImage( - editor.editor.querySelector('img'), - () => { - simulateEvent( - 'keydown', - 'Alt', - editor.ew - ); - resizeImage(editor); - - expect(sortAttributes(editor.value)).eq( - '

' - ); - done(); - } + await onLoadImage( + editor.editor.querySelector('img') + ); + + simulateEvent('keydown', 'Alt', editor.ew); + resizeImage(editor); + + expect(sortAttributes(editor.value)).eq( + '

' ); }); }); }); describe('Enable useAspectRatio only for table', () => { - it("should don't save it for images", done => { + it("should don't save it for images", async () => { const editor = getJodit({ resizer: { useAspectRatio: new Set(['table']) @@ -312,22 +303,20 @@ describe('Resize plugin', function () { editor.value = '

'; - onLoadImage( - editor.editor.querySelector('img'), - () => { - resizeImage(editor); + await onLoadImage( + editor.editor.querySelector('img') + ); - expect(sortAttributes(editor.value)).eq( - '

' - ); - done(); - } + resizeImage(editor); + + expect(sortAttributes(editor.value)).eq( + '

' ); }); }); describe('Enable useAspectRatio for all', () => { - it('should save it for all', done => { + it('should save it for all', async () => { const editor = getJodit({ resizer: { useAspectRatio: true @@ -337,16 +326,14 @@ describe('Resize plugin', function () { editor.value = '

'; - onLoadImage( - editor.editor.querySelector('img'), - () => { - resizeImage(editor); + await onLoadImage( + editor.editor.querySelector('img') + ); - expect(sortAttributes(editor.value)).eq( - '

' - ); - done(); - } + resizeImage(editor); + + expect(sortAttributes(editor.value)).eq( + '

' ); }); }); @@ -354,7 +341,7 @@ describe('Resize plugin', function () { describe('For styled image', () => { describe('Disable forImageChangeAttributes', () => { - it('Should change only styles width and height', done => { + it('Should change only styles width and height', async () => { const editor = getJodit({ history: { timeout: 0 @@ -364,23 +351,20 @@ describe('Resize plugin', function () { editor.value = '

'; - onLoadImage( - editor.editor.querySelector('img'), - () => { - resizeImage(editor); + await onLoadImage( + editor.editor.querySelector('img') + ); + resizeImage(editor); - expect(sortAttributes(editor.value)).eq( - '

' - ); - done(); - } + expect(sortAttributes(editor.value)).eq( + '

' ); }); }); describe('Enable forImageChangeAttributes', () => { describe('Does not has width or height styles', () => { - it('Should change only attributes width and height', done => { + it('Should change only attributes width and height', async () => { const editor = getJodit({ resizer: { forImageChangeAttributes: true @@ -393,23 +377,20 @@ describe('Resize plugin', function () { editor.value = '

'; - onLoadImage( - editor.editor.querySelector('img'), - () => { - resizeImage(editor); + await onLoadImage( + editor.editor.querySelector('img') + ); + resizeImage(editor); - expect(sortAttributes(editor.value)).eq( - '

' - ); - done(); - } + expect(sortAttributes(editor.value)).eq( + '

' ); }); }); describe('Has width or height styles', () => { describe('Has width style', () => { - it('Should change attributes width and height and width styles', done => { + it('Should change attributes width and height and width styles', async () => { const editor = getJodit({ resizer: { forImageChangeAttributes: true @@ -422,24 +403,20 @@ describe('Resize plugin', function () { editor.value = '

'; - onLoadImage( - editor.editor.querySelector('img'), - () => { - resizeImage(editor); - - expect( - sortAttributes(editor.value) - ).eq( - '

' - ); - done(); - } + await onLoadImage( + editor.editor.querySelector('img') + ); + + resizeImage(editor); + + expect(sortAttributes(editor.value)).eq( + '

' ); }); }); describe('Has height style', () => { - it('Should change attributes width and height and height styles', done => { + it('Should change attributes width and height and height styles', async () => { const editor = getJodit({ resizer: { forImageChangeAttributes: true @@ -452,24 +429,20 @@ describe('Resize plugin', function () { editor.value = '

'; - onLoadImage( - editor.editor.querySelector('img'), - () => { - resizeImage(editor); - - expect( - sortAttributes(editor.value) - ).eq( - '

' - ); - done(); - } + await onLoadImage( + editor.editor.querySelector('img') + ); + + resizeImage(editor); + + expect(sortAttributes(editor.value)).eq( + '

' ); }); }); describe('Has both height and width style', () => { - it('Should change attributes width and height and width and height styles', done => { + it('Should change attributes width and height and width and height styles', async () => { const editor = getJodit({ resizer: { forImageChangeAttributes: true @@ -482,18 +455,14 @@ describe('Resize plugin', function () { editor.value = '

'; - onLoadImage( - editor.editor.querySelector('img'), - () => { - resizeImage(editor); - - expect( - sortAttributes(editor.value) - ).eq( - '

' - ); - done(); - } + await onLoadImage( + editor.editor.querySelector('img') + ); + + resizeImage(editor); + + expect(sortAttributes(editor.value)).eq( + '

' ); }); }); @@ -502,66 +471,56 @@ describe('Resize plugin', function () { }); }); - it('Should not allow to resize image more then width of editor', function (done) { + it('Should not allow to resize image more then width of editor', async () => { getBox().style.width = '600px'; const editor = getJodit(); const image = new Image(); - image.src = 'tests/artio.jpg'; + image.src = 'https://xdsoft.net/jodit/files/artio.jpg'; - const callback = function () { - const ratio = image.naturalWidth / image.naturalHeight; + await onLoadImage(image); - editor.value = - ''; - const img = editor.editor.querySelector('img'); - simulateEvent(['mousedown', 'mouseup', 'click'], 0, img); - const resizer = document.querySelector( - '.jodit-resizer[data-editor_id=' + editor.id + ']' - ); - expect(resizer).is.not.null; + const ratio = image.naturalWidth / image.naturalHeight; - const positionResizer = offset(resizer); - // + editor.value = + '

'; + const img = editor.editor.querySelector('img'); + await onLoadImage(img); - simulateEvent( - 'mousedown', - resizer.getElementsByTagName('div')[1] - ); + simulateEvent(['mousedown', 'mouseup', 'click'], 0, img); + const resizer = document.querySelector( + '.jodit-resizer[data-editor_id=' + editor.id + ']' + ); + expect(resizer).is.not.null; - simulateEvent( - 'mousemove', - editor.ownerWindow, - function (data) { - data.clientX = positionResizer.left + 1000; - data.clientY = positionResizer.top + 1000; - } - ); + const positionResizer = offset(resizer); + // - simulateEvent( - 'mouseup', - editor.ownerWindow, - function (data) { - data.clientX = positionResizer.left + 1000; - data.clientY = positionResizer.top + 1000; - } - ); - const newratio = img.offsetWidth / img.offsetHeight; + simulateEvent( + 'mousedown', + resizer.getElementsByTagName('div')[1] + ); - expect(img.offsetWidth).equals( - editor.editor.offsetWidth - 16 - ); + simulateEvent('mousemove', editor.ownerWindow, function (data) { + data.clientX = positionResizer.left + 1000; + data.clientY = positionResizer.top + 1000; + }); + + simulateEvent('mouseup', editor.ownerWindow, function (data) { + data.clientX = positionResizer.left + 1000; + data.clientY = positionResizer.top + 1000; + }); + const newRatio = img.offsetWidth / img.offsetHeight; - expect(Math.abs(newratio - ratio) < 0.003).is.true; - done(); - }; + expect(img.offsetWidth).equals(editor.editor.offsetWidth - 16); - onLoadImage(image, callback); + console.log(newRatio, ratio); + expect(Math.abs(newRatio - ratio) < 0.003).is.true; }); }); }); - describe('For iframes', function () { - it('should wrap these iframes inside JODIT tag', function () { + describe('For iframes', () => { + it('should wrap these iframes inside JODIT tag', () => { const editor = getJodit(); editor.value = '' + @@ -573,8 +532,8 @@ describe('Resize plugin', function () { ).equals('IFRAME'); }); - describe('Output HTML', function () { - it('should not contains JODIT tag', function () { + describe('Output HTML', () => { + it('should not contains JODIT tag', () => { const editor = getJodit(); editor.value = '' + diff --git a/src/plugins/source/source.test.js b/src/plugins/source/source.test.js index a46fa8a86..32d6c9ef6 100644 --- a/src/plugins/source/source.test.js +++ b/src/plugins/source/source.test.js @@ -20,7 +20,7 @@ describe('Source code test', function () { let editor; - timeout = setTimeout(function () { + timeout = /*ok*/ setTimeout(function () { expect(false).is.true; __done.call(editor); }, 5000); @@ -63,7 +63,7 @@ describe('Source code test', function () { let editor; - timeout = setTimeout(function () { + timeout = /*ok*/ setTimeout(function () { expect(false).is.true; __done.call(editor); }, 5000); @@ -77,7 +77,7 @@ describe('Source code test', function () { return false; }, sourceEditorReady: function (editor) { - setTimeout(() => { + editor.async.setTimeout(() => { expect( editor.__plugins.source.sourceEditor.getValue() ).equals('

pop

'); @@ -145,7 +145,7 @@ describe('Source code test', function () { done(); }; - timeout = setTimeout(function () { + timeout = /*ok*/ setTimeout(function () { expect(false).is.true; __done(); }, 140100); diff --git a/test/bootstrap.js b/test/bootstrap.js index e66ea8f8d..44cae9e38 100644 --- a/test/bootstrap.js +++ b/test/bootstrap.js @@ -51,7 +51,7 @@ if (typeof window.toolbarButtonsCount !== 'number') { */ function delay(timeout) { return new naturalPromise(resolve => { - setTimeout(resolve, timeout); + /*ok*/ setTimeout(resolve, timeout); }); } @@ -937,12 +937,18 @@ function one(event, element, callback) { * @param {HTMLImageElement} image * @param {Function} callback */ -function onLoadImage(image, callback) { - if (!image.complete) { - one('load', image, callback); - } else { - callback.apply(image); - } +function onLoadImage(image, callback = () => {}) { + return new naturalPromise(resolve => { + if (!image.complete) { + one('load', image, () => { + callback.call(image); + resolve(); + }); + } else { + callback.call(image); + resolve(); + } + }); } /** diff --git a/test/tests/acceptance/commands.test.js b/test/tests/acceptance/commands.test.js index af23c70b9..12badb927 100644 --- a/test/tests/acceptance/commands.test.js +++ b/test/tests/acceptance/commands.test.js @@ -3,6 +3,7 @@ * Released under MIT see LICENSE.txt in the project root for license information. * Copyright (c) 2013-2023 Valeriy Chupurnov. All rights reserved. https://xdsoft.net */ + describe('Commands Jodit Editor Tests', function () { describe('Command "formatBlock"', function () { it('Try exec the command "formatBlock" for several elements', function () { diff --git a/test/tests/acceptance/inline-mode.test.js b/test/tests/acceptance/inline-mode.test.js index 004f388e6..eee6df0ba 100644 --- a/test/tests/acceptance/inline-mode.test.js +++ b/test/tests/acceptance/inline-mode.test.js @@ -567,7 +567,7 @@ describe('Test Inline mode', function () { simulateEvent('mouseup', input); - setTimeout(function () { + editor.async.setTimeout(function () { expect(getOpenedPopup(editor)).eq(linkPopup); input.value = 'https://xdsoft.net/jodit/'; @@ -611,6 +611,7 @@ describe('Test Inline mode', function () { describe('Disable toolbarInline = false', function () { it('Should show inline popup', function (done) { unmockPromise(); + Jodit.make(appendTestDiv(), { toolbarInline: false, iframe: true, @@ -622,6 +623,7 @@ describe('Test Inline mode', function () { simulateEvent('click', 0, img); const popup = getOpenedPopup(editor); expect(popup).is.null; + done(); } } @@ -641,7 +643,7 @@ describe('Test Inline mode', function () { editor.value = '

test test

'; const img = editor.editor.querySelector('img'); - simulateEvent('click', 0, img); + simulateEvent('click', img); const popup = getOpenedPopup(editor); expect(popup).is.not.null; done(); @@ -662,7 +664,7 @@ describe('Test Inline mode', function () { editor.value = '

test test test

'; const a = editor.editor.querySelector('a'); - simulateEvent('click', 0, a); + simulateEvent('click', a); const popup = getOpenedPopup(editor); expect(popup).is.not.null; done();