uci-utils-obj-buffer/test/test.js

37 lines
1.0 KiB
JavaScript

import { expect } from 'chai'
import JsonStream from '../src/json-stream.js'
const obj = { cmd: 'test', _header: { name: 'a name', id: '12345' }, arg: 5 }
const ser = '63#{"cmd":"test","_header":{"name":"a name","id":"12345"},"arg":5}'
const jss = new JsonStream()
describe('JSON stream check', function () {
it('Should stringify and serialize a message/object', async function () {
expect(await jss.serialize(obj)).to.equal(ser)
})
it('Should serialize and decode a message', async function () {
jss.on('message', mes => {
// console.log(mes)
expect(mes).to.deep.equal(obj)
})
const ser = await jss.serialize(obj)
jss.onData(ser)
})
it('Should queue messages if paused and resumed', async function () {
jss.on('message', mes => {
// console.log('queued message', mes)
expect(mes).to.deep.equal(obj)
})
const ser = await jss.serialize(obj)
jss.pause()
jss.onData(ser)
// console.log(jss.state, jss.queue[0])
expect(jss.queue[0]).to.deep.equal(obj)
jss.resume()
})
})