UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

179 lines (177 loc) • 7.88 kB
/** * DevExtreme (cjs/__internal/scheduler/utils/macro_task_array/methods.test.js) * Version: 25.2.7 * Build date: Tue May 05 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 = _interopRequireDefault(require("./dispatcher")); var _methods = require("./methods"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } _globals.jest.mock("./dispatcher", () => { const actualModule = _globals.jest.requireActual("./dispatcher"); return Object.assign({}, actualModule.default, { schedule: _globals.jest.fn() }) }); const scheduleFnMock = _globals.jest.fn(); _globals.jest.useFakeTimers(); (0, _globals.describe)("Scheduler", () => { (0, _globals.describe)("MacroTaskArray", () => { (0, _globals.describe)("Methods", () => { (0, _globals.beforeEach)(() => { scheduleFnMock.mockReset(); _dispatcher.default.schedule = scheduleFnMock }); (0, _globals.describe)("macroTaskArrayForEach", () => { _globals.it.each([{ arraySize: 10, step: 1, expectedCalls: 10 }, { arraySize: 10, step: 2, expectedCalls: 5 }, { arraySize: 10, step: 3, expectedCalls: 4 }, { arraySize: 3, step: 4, expectedCalls: 1 }, { arraySize: 0, step: 10, expectedCalls: 0 }])("should split array into batches (arraySize = $arraySize | step = $step)", async _ref => { let { arraySize: arraySize, step: step, expectedCalls: expectedCalls } = _ref; await (0, _methods.macroTaskArrayForEach)(new Array(arraySize), _globals.jest.fn(), step); (0, _globals.expect)(scheduleFnMock).toHaveBeenCalledTimes(expectedCalls) }); _globals.it.each([{ arraySize: 10, step: 1 }, { arraySize: 10, step: 2 }, { arraySize: 10, step: 3 }, { arraySize: 3, step: 4 }, { arraySize: 0, step: 10 }])("should call callback for each array item (arraySize = $arraySize | step = $step)", async _ref2 => { let { arraySize: arraySize, step: step } = _ref2; const callbackMock = _globals.jest.fn(); scheduleFnMock.mockImplementation(callback => { callback(); return Promise.resolve() }); const array = new Array(arraySize).fill(0).map((_, idx) => idx); await (0, _methods.macroTaskArrayForEach)(array, callbackMock, step); (0, _globals.expect)(callbackMock).toHaveBeenCalledTimes(arraySize); (0, _globals.expect)(callbackMock.mock.calls).toEqual(array.map(item => [item])) }); (0, _globals.it)("should pass macroTaskTimeoutMs to dispatcher", async () => { await (0, _methods.macroTaskArrayForEach)(new Array(10), _globals.jest.fn(), 100, 12345); (0, _globals.expect)(scheduleFnMock).toHaveBeenCalledTimes(1); (0, _globals.expect)(scheduleFnMock).toHaveBeenCalledWith(_globals.expect.anything(), 12345) }) }); (0, _globals.describe)("macroTaskArrayMap", () => { _globals.it.each([{ arraySize: 10, step: 1, expectedCalls: 10 }, { arraySize: 10, step: 2, expectedCalls: 5 }, { arraySize: 10, step: 3, expectedCalls: 4 }, { arraySize: 3, step: 4, expectedCalls: 1 }, { arraySize: 0, step: 10, expectedCalls: 0 }])("should split array into batches (arraySize = $arraySize | step = $step)", async _ref3 => { let { arraySize: arraySize, step: step, expectedCalls: expectedCalls } = _ref3; await (0, _methods.macroTaskArrayMap)(new Array(arraySize), _globals.jest.fn(), step); (0, _globals.expect)(scheduleFnMock).toHaveBeenCalledTimes(expectedCalls) }); _globals.it.each([{ arraySize: 10, step: 1 }, { arraySize: 10, step: 2 }, { arraySize: 10, step: 3 }, { arraySize: 3, step: 4 }, { arraySize: 0, step: 10 }])("should call callback for each array item (arraySize = $arraySize | step = $step)", async _ref4 => { let { arraySize: arraySize, step: step } = _ref4; const callbackMock = _globals.jest.fn(); scheduleFnMock.mockImplementation(callback => { callback(); return Promise.resolve() }); const array = new Array(arraySize).fill(0).map((_, idx) => idx); await (0, _methods.macroTaskArrayMap)(array, callbackMock, step); (0, _globals.expect)(callbackMock).toHaveBeenCalledTimes(arraySize); (0, _globals.expect)(callbackMock.mock.calls).toEqual(array.map(item => [item])) }); (0, _globals.it)("should return mapped result", async () => { const callbackFn = item => `${item}_processed`; scheduleFnMock.mockImplementation(callback => { callback(); return Promise.resolve() }); const array = new Array(10).fill(0).map((_, idx) => idx); const result = await (0, _methods.macroTaskArrayMap)(array, callbackFn, 10); (0, _globals.expect)(result).toEqual(array.map(callbackFn)) }); (0, _globals.it)("should pass macroTaskTimeoutMs to dispatcher", async () => { await (0, _methods.macroTaskArrayMap)(new Array(10), _globals.jest.fn(), 100, 12345); (0, _globals.expect)(scheduleFnMock).toHaveBeenCalledTimes(1); (0, _globals.expect)(scheduleFnMock).toHaveBeenCalledWith(_globals.expect.anything(), 12345) }) }) }) }) });