napi-rs/examples/napi-compat-mode/__tests__/global.spec.ts

28 lines
645 B
TypeScript
Raw Normal View History

2020-10-03 01:05:43 +09:00
import test from 'ava'
import Sinon from 'sinon'
const bindings = require('../index.node')
function wait(delay: number) {
return new Promise((resolve) => setTimeout(resolve, delay))
}
2020-10-03 15:57:19 +09:00
const delay = 100
2020-10-03 01:05:43 +09:00
test('should setTimeout', async (t) => {
const handler = Sinon.spy()
bindings.setTimeout(handler, delay)
t.is(handler.callCount, 0)
await wait(delay + 10)
t.is(handler.callCount, 1)
})
2020-10-03 15:57:19 +09:00
test('should clearTimeout', async (t) => {
const handler = Sinon.spy()
const timer = setTimeout(() => handler(), delay)
t.is(handler.callCount, 0)
bindings.clearTimeout(timer)
await wait(delay + 10)
t.is(handler.callCount, 0)
})