test: test for scheduler

This commit is contained in:
Evan You 2018-09-19 15:36:56 -04:00
parent 73106b8553
commit d69d0c0216
5 changed files with 1371 additions and 38 deletions

12
jest.config.js Normal file
View File

@ -0,0 +1,12 @@
module.exports = {
preset: 'ts-jest',
coverageDirectory: 'coverage',
coverageReporters: ['html', 'lcov', 'text'],
collectCoverageFrom: ['packages/*/src/**/*.ts'],
moduleFileExtensions: ['ts', 'js', 'json'],
moduleNameMapper: {
'^@vue/(.*?)$': '<rootDir>/packages/$1/src'
},
rootDir: __dirname,
testMatch: ['<rootDir>/packages/**/__tests__/**/*spec.(t|j)s']
}

View File

@ -23,10 +23,12 @@
]
},
"devDependencies": {
"@types/jest": "^23.3.2",
"chalk": "^2.4.1",
"dts-bundle": "^0.7.3",
"execa": "^1.0.0",
"fs-extra": "^7.0.0",
"jest": "^23.6.0",
"lerna": "^3.4.0",
"lint-staged": "^7.2.2",
"minimist": "^1.2.0",
@ -36,6 +38,7 @@
"rollup-plugin-replace": "^2.0.0",
"rollup-plugin-terser": "^2.0.2",
"rollup-plugin-typescript2": "^0.17.0",
"ts-jest": "^23.10.0",
"typescript": "^3.0.3",
"yorkie": "^2.0.0"
}

View File

@ -0,0 +1,78 @@
import { queueJob, nextTick } from '../src/index'
test('queueJob', async () => {
const calls: any = []
const job1 = () => {
calls.push('job1')
}
const job2 = () => {
calls.push('job2')
}
queueJob(job1)
queueJob(job2)
expect(calls).toEqual([])
await nextTick()
expect(calls).toEqual(['job1', 'job2'])
})
test('queueJob while already flushing', async () => {
const calls: any = []
const job1 = () => {
calls.push('job1')
// job1 queues job2
queueJob(job2)
// should be called sync
expect(calls).toEqual(['job1', 'job2'])
}
const job2 = () => {
calls.push('job2')
}
queueJob(job1)
expect(calls).toEqual([])
await nextTick()
expect(calls).toEqual(['job1', 'job2'])
})
test('queueJob w/ postFlushCb', async () => {
const calls: any = []
const job1 = () => {
calls.push('job1')
}
const job2 = () => {
calls.push('job2')
}
const cb1 = () => {
calls.push('cb1')
}
const cb2 = () => {
calls.push('cb2')
}
queueJob(job1, cb1)
queueJob(job2, cb2)
await nextTick()
expect(calls).toEqual(['job1', 'job2', 'cb1', 'cb2'])
})
test('queueJob w/ postFlushCb while flushing', async () => {
const calls: any = []
const job1 = () => {
calls.push('job1')
// job1 queues job2
queueJob(job2, cb2)
// should be called sync
expect(calls).toEqual(['job1', 'job2'])
}
const job2 = () => {
calls.push('job2')
}
const cb1 = () => {
calls.push('cb1')
}
const cb2 = () => {
calls.push('cb2')
}
queueJob(job1, cb1)
expect(calls).toEqual([])
await nextTick()
expect(calls).toEqual(['job1', 'job2', 'cb1', 'cb2'])
})

View File

@ -1,10 +1,11 @@
const queue: Array<() => void> = []
const postFlushCbs: Array<() => void> = []
const p = Promise.resolve()
let flushing = false
export function nextTick(fn: () => void) {
p.then(fn)
export function nextTick(fn?: () => void): Promise<void> {
return p.then(fn)
}
export function queueJob(job: () => void, postFlushCb?: () => void) {

1311
yarn.lock

File diff suppressed because it is too large Load Diff