Skip to content

Commit

Permalink
supplement unit test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
L-Qun committed Aug 29, 2024
1 parent 8eead90 commit b884605
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions __tests__/scheduler.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { addTask, initiateWorkLoop } from '../src/workLoop'
import { NORMAL_PRIORITY, IMMEDIATE_PRIORITY, LOW_PRIORITY } from '../src'

describe('Scheduler', () => {
beforeEach(() => {
jest.useFakeTimers()
})

it('executes normal priority tasks in the order they are scheduled', async () => {
const log: string[] = []

addTask(NORMAL_PRIORITY, () => {
log.push('A')
})
addTask(NORMAL_PRIORITY, () => {
log.push('B')
})
addTask(NORMAL_PRIORITY, () => {
log.push('C')
})

expect(log).toEqual([])
initiateWorkLoop()
jest.runAllTimers()
expect(log).toEqual(['A', 'B', 'C'])
})

it('executes low priority tasks in the order they are scheduled', async () => {
const log: string[] = []

addTask(LOW_PRIORITY, () => {
log.push('A')
})
addTask(LOW_PRIORITY, () => {
log.push('B')
})
addTask(LOW_PRIORITY, () => {
log.push('C')
})

expect(log).toEqual([])
initiateWorkLoop()
jest.runAllTimers()
expect(log).toEqual(['A', 'B', 'C'])
})

it('executes callbacks in order of priority', async () => {
const log: string[] = []

addTask(NORMAL_PRIORITY, () => {
log.push('A')
})
addTask(NORMAL_PRIORITY, () => {
log.push('B')
})
addTask(IMMEDIATE_PRIORITY, () => {
log.push('C')
})
addTask(IMMEDIATE_PRIORITY, () => {
log.push('D')
})

expect(log).toEqual([])
initiateWorkLoop()
jest.runAllTimers()
expect(log).toEqual(['C', 'D', 'A', 'B'])
})
})

0 comments on commit b884605

Please sign in to comment.