napi-rs/examples/napi-compat-mode/__tests__/napi7/arraybuffer.spec.ts
forehal a781a4f27e feat(cli): brand new cli tool with both cli and programmatical usage (#1492)
BREAKING CHANGE: requires node >= 16 and some cli options have been renamed
2023-04-06 11:04:53 +08:00

31 lines
912 B
TypeScript

import ava from 'ava'
import { napiVersion } from '../napi-version'
const bindings = require('../../index.node')
const test = napiVersion >= 7 ? ava : ava.skip
test('should be able to detach ArrayBuffer', (t) => {
const buf = Buffer.from('hello world')
const ab = buf.buffer.slice(0, buf.length)
try {
bindings.testDetachArrayBuffer(ab)
t.is(ab.byteLength, 0)
} catch (e) {
t.is((e as any).code, 'DetachableArraybufferExpected')
}
})
test('is detached arraybuffer should work fine', (t) => {
const buf = Buffer.from('hello world')
const ab = buf.buffer.slice(0, buf.length)
try {
bindings.testDetachArrayBuffer(ab)
const nonDetachedArrayBuffer = new ArrayBuffer(10)
t.true(bindings.testIsDetachedArrayBuffer(ab))
t.false(bindings.testIsDetachedArrayBuffer(nonDetachedArrayBuffer))
} catch (e) {
t.is((e as any).code, 'DetachableArraybufferExpected')
}
})