Skip to content

Commit

Permalink
Calls to setTimout without the async module have been removed from au…
Browse files Browse the repository at this point in the history
…totests, and most of the asynchronous tests have been rewritten from done to async/await
  • Loading branch information
xdan committed Nov 3, 2023
1 parent f9fd995 commit a07513e
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 404 deletions.
24 changes: 18 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

- Removed `Jodit.modules.Helpers.val` method
#### :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 <br>.

## 4.0.0-beta.91
Expand Down Expand Up @@ -2316,11 +2328,11 @@ Related with https://github.com/xdan/jodit/issues/574. In some cases need to lim
- @property {IUIOption[]} link.selectOptionsClassName=[] The list of the option for the select (to use with
modeClassName="select")
- ex: [
- { value: "", text: "" },
- { value: "val1", text: "text1" },
- { value: "val2", text: "text2" },
- { value: "val3", text: "text3" }
- ]
- { value: "", text: "" },
- { value: "val1", text: "text1" },
- { value: "val2", text: "text2" },
- { value: "val3", text: "text3" }
- ]
PR: https://github.com/xdan/jodit/pull/577 Thanks @s-renier-taonix-fr
##### New option `statusbar: boolean = true`
Expand Down
108 changes: 43 additions & 65 deletions src/core/async/async.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,33 @@ 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))
.then(callSpy)
.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))
.then(callSpy)
.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
Expand All @@ -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'
Expand All @@ -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;

Expand All @@ -174,24 +153,23 @@ 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);
});
});
});

describe('requestAnimationFrame', () => {
it('Should call as usual requestAnimationFrame', done => {
asyncM.requestAnimationFrame(callSpy);
requestAnimationFrame(() => {
/*ok*/ requestAnimationFrame(() => {
expect(callCount).equals(1);
done();
});
Expand All @@ -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();
});
Expand Down
7 changes: 6 additions & 1 deletion src/core/async/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -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;
Expand Down
Loading

0 comments on commit a07513e

Please sign in to comment.