UNPKG

devextreme

Version:

HTML5 JavaScript Component Suite for Responsive Web Development

76 lines (75 loc) 3.48 kB
/** * DevExtreme (esm/__internal/scheduler/utils/macro_task_array/dispatcher.test.js) * Version: 24.2.6 * Build date: Mon Mar 17 2025 * * Copyright (c) 2012 - 2025 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ import { describe, expect, it, jest } from "@jest/globals"; import dispatcher, { macroTaskIdSet } from "./dispatcher"; jest.useFakeTimers(); describe("Scheduler", (() => { describe("MacroTaskArray", (() => { describe("Dispatcher", (() => { describe("schedule", (() => { it("should add timeout ids to timeout ids set", (() => { dispatcher.schedule(jest.fn(), 0).finally((() => {})); dispatcher.schedule(jest.fn(), 0).finally((() => {})); expect(macroTaskIdSet.size).toBe(2) })); it("should remove timeout id from timeout ids set after macro task execution", (async () => { const p1 = dispatcher.schedule(jest.fn(), 0); const p2 = dispatcher.schedule(jest.fn(), 0); jest.advanceTimersByTime(0); await Promise.all([p1, p2]); expect(macroTaskIdSet.size).toBe(0) })); it("should call callback as macro task", (() => { const callbackMock = jest.fn(); dispatcher.schedule(callbackMock, 0).finally((() => {})); expect(callbackMock).toHaveBeenCalledTimes(0); jest.advanceTimersByTime(0); expect(callbackMock).toHaveBeenCalledTimes(1) })); it("should use macroTaskTimeoutMs form macro task delay", (() => { const callbackMock = jest.fn(); dispatcher.schedule(callbackMock, 1e3).finally((() => {})); expect(callbackMock).toHaveBeenCalledTimes(0); jest.advanceTimersByTime(500); expect(callbackMock).toHaveBeenCalledTimes(0); jest.advanceTimersByTime(500); expect(callbackMock).toHaveBeenCalledTimes(1) })) })); describe("dispose", (() => { it("should clear scheduled macro tasks", (() => { const clearTimeoutSpy = jest.spyOn(window, "clearTimeout"); dispatcher.schedule(jest.fn(), 0).finally((() => {})); dispatcher.schedule(jest.fn(), 0).finally((() => {})); const [firstId, secondId] = Array.from(macroTaskIdSet); dispatcher.dispose(); expect(clearTimeoutSpy).toHaveBeenCalledTimes(2); expect(clearTimeoutSpy.mock.calls).toEqual([ [firstId], [secondId] ]) })); it("should clear timeout ids set", (() => { dispatcher.schedule(jest.fn(), 0).finally((() => {})); dispatcher.schedule(jest.fn(), 0).finally((() => {})); expect(macroTaskIdSet.size).toBe(2); dispatcher.dispose(); expect(macroTaskIdSet.size).toBe(0) })) })) })) })) }));