|
| 1 | +import { Task } from './task'; |
| 2 | +import { TaskScheduler } from './task-scheduler'; |
| 3 | +import { TaskStackType } from './types'; |
| 4 | +import { createMockTask } from './utils/_testing'; |
| 5 | + |
| 6 | +describe('TaskScheduler', () => { |
| 7 | + let taskScheduler: TaskScheduler; |
| 8 | + beforeEach(() => { |
| 9 | + taskScheduler = new TaskScheduler(); |
| 10 | + }); |
| 11 | + |
| 12 | + describe('when enqueueing a task', () => { |
| 13 | + let executeMock: jest.Mock; |
| 14 | + let task: Task |
| 15 | + beforeEach(() => { |
| 16 | + ({ task, executeMock } = createMockTask()); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should add the task to the running list when ticked', () => { |
| 20 | + taskScheduler.enqueue(task); |
| 21 | + taskScheduler.tick(); |
| 22 | + expect(executeMock).toHaveBeenCalled(); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should not add the task to the running list until the next tick', () => { |
| 26 | + taskScheduler.enqueue(task); |
| 27 | + expect(executeMock).not.toHaveBeenCalled(); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('when ticked multiple times', () => { |
| 31 | + beforeEach(() => { |
| 32 | + taskScheduler.enqueue(task); |
| 33 | + taskScheduler.tick(); |
| 34 | + taskScheduler.tick(); |
| 35 | + }); |
| 36 | + |
| 37 | + it('should tick the task twice', () => { |
| 38 | + expect(executeMock).toHaveBeenCalledTimes(2); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + describe('when the task is stopped', () => { |
| 43 | + beforeEach(() => { |
| 44 | + taskScheduler.enqueue(task); |
| 45 | + taskScheduler.tick(); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should not tick the task after stopping', () => { |
| 49 | + task.stop(); |
| 50 | + taskScheduler.tick(); |
| 51 | + expect(executeMock).toHaveBeenCalledTimes(1); |
| 52 | + }); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + describe('when enqueueing a task that cannot stack', () => { |
| 57 | + const interval = 0; |
| 58 | + const stackType = TaskStackType.NEVER; |
| 59 | + const stackGroup = 'foo'; |
| 60 | + |
| 61 | + let firstExecuteMock: jest.Mock; |
| 62 | + let firstTask: Task |
| 63 | + beforeEach(() => { |
| 64 | + ({ task: firstTask, executeMock: firstExecuteMock } = createMockTask(interval, stackType, stackGroup)); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should stop any other tasks with the same stack group', () => { |
| 68 | + const { task: secondTask, executeMock: secondExecuteMock } = createMockTask(interval, stackType, stackGroup); |
| 69 | + |
| 70 | + taskScheduler.enqueue(firstTask); |
| 71 | + taskScheduler.enqueue(secondTask); |
| 72 | + taskScheduler.tick(); |
| 73 | + |
| 74 | + expect(firstExecuteMock).not.toHaveBeenCalled(); |
| 75 | + expect(secondExecuteMock).toHaveBeenCalled(); |
| 76 | + }); |
| 77 | + |
| 78 | + it('should not stop any other tasks with a different stack group', () => { |
| 79 | + const otherStackGroup = 'bar'; |
| 80 | + const { task: secondTask, executeMock: secondExecuteMock } = createMockTask(interval, stackType, otherStackGroup); |
| 81 | + |
| 82 | + taskScheduler.enqueue(firstTask); |
| 83 | + taskScheduler.enqueue(secondTask); |
| 84 | + taskScheduler.tick(); |
| 85 | + |
| 86 | + expect(firstExecuteMock).toHaveBeenCalled(); |
| 87 | + expect(secondExecuteMock).toHaveBeenCalled(); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe('when clearing the scheduler', () => { |
| 92 | + let executeMock: jest.Mock; |
| 93 | + let task: Task |
| 94 | + beforeEach(() => { |
| 95 | + ({ task, executeMock } = createMockTask()); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should stop all tasks', () => { |
| 99 | + taskScheduler.enqueue(task); |
| 100 | + taskScheduler.tick(); |
| 101 | + taskScheduler.clear(); |
| 102 | + taskScheduler.tick(); |
| 103 | + expect(executeMock).toHaveBeenCalledTimes(1); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments