UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

99 lines (97 loc) 4.97 kB
/** * DevExtreme (cjs/__internal/scheduler/utils/macro_task_array/dispatcher.test.js) * Version: 25.2.5 * Build date: Fri Feb 20 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; var _globals = require("@jest/globals"); var _dispatcher = _interopRequireWildcard(require("./dispatcher")); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) { var r = new WeakMap, n = new WeakMap } return (_interopRequireWildcard = function(e, t) { if (!t && e && e.__esModule) { return e } var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) { return f } if (o = t ? n : r) { if (o.has(e)) { return o.get(e) } o.set(e, f) } for (const t in e) { "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]) } return f })(e, t) } _globals.jest.useFakeTimers(); (0, _globals.describe)("Scheduler", (() => { (0, _globals.describe)("MacroTaskArray", (() => { (0, _globals.describe)("Dispatcher", (() => { (0, _globals.describe)("schedule", (() => { (0, _globals.it)("should add timeout ids to timeout ids set", (() => { _dispatcher.default.schedule(_globals.jest.fn(), 0).finally((() => {})); _dispatcher.default.schedule(_globals.jest.fn(), 0).finally((() => {})); (0, _globals.expect)(_dispatcher.macroTaskIdSet.size).toBe(2) })); (0, _globals.it)("should remove timeout id from timeout ids set after macro task execution", (async () => { const p1 = _dispatcher.default.schedule(_globals.jest.fn(), 0); const p2 = _dispatcher.default.schedule(_globals.jest.fn(), 0); _globals.jest.advanceTimersByTime(0); await Promise.all([p1, p2]); (0, _globals.expect)(_dispatcher.macroTaskIdSet.size).toBe(0) })); (0, _globals.it)("should call callback as macro task", (() => { const callbackMock = _globals.jest.fn(); _dispatcher.default.schedule(callbackMock, 0).finally((() => {})); (0, _globals.expect)(callbackMock).toHaveBeenCalledTimes(0); _globals.jest.advanceTimersByTime(0); (0, _globals.expect)(callbackMock).toHaveBeenCalledTimes(1) })); (0, _globals.it)("should use macroTaskTimeoutMs form macro task delay", (() => { const callbackMock = _globals.jest.fn(); _dispatcher.default.schedule(callbackMock, 1e3).finally((() => {})); (0, _globals.expect)(callbackMock).toHaveBeenCalledTimes(0); _globals.jest.advanceTimersByTime(500); (0, _globals.expect)(callbackMock).toHaveBeenCalledTimes(0); _globals.jest.advanceTimersByTime(500); (0, _globals.expect)(callbackMock).toHaveBeenCalledTimes(1) })) })); (0, _globals.describe)("dispose", (() => { (0, _globals.it)("should clear scheduled macro tasks", (() => { const clearTimeoutSpy = _globals.jest.spyOn(window, "clearTimeout"); _dispatcher.default.schedule(_globals.jest.fn(), 0).finally((() => {})); _dispatcher.default.schedule(_globals.jest.fn(), 0).finally((() => {})); const [firstId, secondId] = Array.from(_dispatcher.macroTaskIdSet); _dispatcher.default.dispose(); (0, _globals.expect)(clearTimeoutSpy).toHaveBeenCalledTimes(2); (0, _globals.expect)(clearTimeoutSpy.mock.calls).toEqual([ [firstId], [secondId] ]) })); (0, _globals.it)("should clear timeout ids set", (() => { _dispatcher.default.schedule(_globals.jest.fn(), 0).finally((() => {})); _dispatcher.default.schedule(_globals.jest.fn(), 0).finally((() => {})); (0, _globals.expect)(_dispatcher.macroTaskIdSet.size).toBe(2); _dispatcher.default.dispose(); (0, _globals.expect)(_dispatcher.macroTaskIdSet.size).toBe(0) })) })) })) })) }));