From f838715b7c0512cb46d2beef2d3f9fb2b6c19ee2 Mon Sep 17 00:00:00 2001 From: JiZhi <471695625@qq.com> Date: Sun, 17 Nov 2019 05:32:06 +0800 Subject: [PATCH] test(runtime-core): add tests for scheduler (#462) --- .../runtime-core/__tests__/scheduler.spec.ts | 233 ++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 packages/runtime-core/__tests__/scheduler.spec.ts diff --git a/packages/runtime-core/__tests__/scheduler.spec.ts b/packages/runtime-core/__tests__/scheduler.spec.ts new file mode 100644 index 0000000000..03a7abc162 --- /dev/null +++ b/packages/runtime-core/__tests__/scheduler.spec.ts @@ -0,0 +1,233 @@ +import { queueJob, nextTick, queuePostFlushCb } from '../src/scheduler' + +describe('scheduler', () => { + it('nextTick', async () => { + const calls: string[] = [] + const dummyThen = Promise.resolve().then() + const job1 = () => { + calls.push('job1') + } + const job2 = () => { + calls.push('job2') + } + nextTick(job1) + job2() + + expect(calls.length).toBe(1) + await dummyThen + // job1 will be pushed in nextTick + expect(calls.length).toBe(2) + expect(calls).toMatchObject(['job2', 'job1']) + }) + + describe('queueJob', () => { + it('basic usage', async () => { + const calls: string[] = [] + const job1 = () => { + calls.push('job1') + } + const job2 = () => { + calls.push('job2') + } + queueJob(job1) + queueJob(job2) + expect(calls).toEqual([]) + await nextTick() + expect(calls).toEqual(['job1', 'job2']) + }) + + it('should dedupe queued jobs', async () => { + const calls: string[] = [] + const job1 = () => { + calls.push('job1') + } + const job2 = () => { + calls.push('job2') + } + queueJob(job1) + queueJob(job2) + queueJob(job1) + queueJob(job2) + expect(calls).toEqual([]) + await nextTick() + expect(calls).toEqual(['job1', 'job2']) + }) + + it('queueJob while flushing', async () => { + const calls: string[] = [] + const job1 = () => { + calls.push('job1') + // job2 will be excuted after job1 at the same tick + queueJob(job2) + } + const job2 = () => { + calls.push('job2') + } + queueJob(job1) + + await nextTick() + expect(calls).toEqual(['job1', 'job2']) + }) + }) + + describe('queuePostFlushCb', () => { + it('basic usage', async () => { + const calls: string[] = [] + const cb1 = () => { + calls.push('cb1') + } + const cb2 = () => { + calls.push('cb2') + } + const cb3 = () => { + calls.push('cb3') + } + + queuePostFlushCb([cb1, cb2]) + queuePostFlushCb(cb3) + + expect(calls).toEqual([]) + await nextTick() + expect(calls).toEqual(['cb1', 'cb2', 'cb3']) + }) + + it('should dedupe queued postFlushCb', async () => { + const calls: string[] = [] + const cb1 = () => { + calls.push('cb1') + } + const cb2 = () => { + calls.push('cb2') + } + const cb3 = () => { + calls.push('cb3') + } + + queuePostFlushCb([cb1, cb2]) + queuePostFlushCb(cb3) + + queuePostFlushCb([cb1, cb3]) + queuePostFlushCb(cb2) + + expect(calls).toEqual([]) + await nextTick() + expect(calls).toEqual(['cb1', 'cb2', 'cb3']) + }) + + it('queuePostFlushCb while flushing', async () => { + const calls: string[] = [] + const cb1 = () => { + calls.push('cb1') + // cb2 will be excuted after cb1 at the same tick + queuePostFlushCb(cb2) + } + const cb2 = () => { + calls.push('cb2') + } + queuePostFlushCb(cb1) + + await nextTick() + expect(calls).toEqual(['cb1', 'cb2']) + }) + }) + + describe('queueJob w/ queuePostFlushCb', () => { + it('queueJob inside postFlushCb', async () => { + const calls: string[] = [] + const job1 = () => { + calls.push('job1') + } + const cb1 = () => { + // queueJob in postFlushCb + calls.push('cb1') + queueJob(job1) + } + + queuePostFlushCb(cb1) + await nextTick() + expect(calls).toEqual(['cb1', 'job1']) + }) + + it('queueJob & postFlushCb inside postFlushCb', async () => { + const calls: string[] = [] + const job1 = () => { + calls.push('job1') + } + const cb1 = () => { + calls.push('cb1') + queuePostFlushCb(cb2) + // job1 will executed before cb2 + // Job has higher priority than postFlushCb + queueJob(job1) + } + const cb2 = () => { + calls.push('cb2') + } + + queuePostFlushCb(cb1) + await nextTick() + expect(calls).toEqual(['cb1', 'job1', 'cb2']) + }) + + it('postFlushCb inside queueJob', async () => { + const calls: string[] = [] + const job1 = () => { + calls.push('job1') + // postFlushCb in queueJob + queuePostFlushCb(cb1) + } + const cb1 = () => { + calls.push('cb1') + } + + queueJob(job1) + await nextTick() + expect(calls).toEqual(['job1', 'cb1']) + }) + + it('queueJob & postFlushCb inside queueJob', async () => { + const calls: string[] = [] + const job1 = () => { + calls.push('job1') + // cb1 will executed after job2 + // Job has higher priority than postFlushCb + queuePostFlushCb(cb1) + queueJob(job2) + } + const job2 = () => { + calls.push('job2') + } + const cb1 = () => { + calls.push('cb1') + } + + queueJob(job1) + await nextTick() + expect(calls).toEqual(['job1', 'job2', 'cb1']) + }) + + it('nested queueJob w/ postFlushCb', async () => { + const calls: string[] = [] + const job1 = () => { + calls.push('job1') + + queuePostFlushCb(cb1) + queueJob(job2) + } + const job2 = () => { + calls.push('job2') + queuePostFlushCb(cb2) + } + const cb1 = () => { + calls.push('cb1') + } + const cb2 = () => { + calls.push('cb2') + } + + queueJob(job1) + await nextTick() + expect(calls).toEqual(['job1', 'job2', 'cb1', 'cb2']) + }) + }) +}) -- 2.47.3