Skip to content

Commit

Permalink
fix: some typos. Adds some tests for hyperdeck class
Browse files Browse the repository at this point in the history
  • Loading branch information
Julusian committed Oct 15, 2018
1 parent 7cea4ad commit 01b4634
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"test": "yarn lint && yarn unit",
"test:integration": "yarn lint && jest --config=jest-integration.config.js",
"watch": "jest --watch",
"cov": "jest --coverage; opn coverage/lcov-report/index.html",
"cov": "jest --coverage && opn coverage/lcov-report/index.html",
"cov-open": "opn coverage/lcov-report/index.html",
"send-coverage": "jest && codecov",
"docs": "yarn docs:html && opn docs/index.html",
Expand Down
98 changes: 98 additions & 0 deletions src/__mocks__/net.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { EventEmitter } from 'events'
let setTimeoutOrg = setTimeout
const sockets: Array<Socket> = []
const onNextSocket: Array<Function> = []

export class Socket extends EventEmitter {

public onWrite: (buff: Buffer, encoding: string) => void
public onConnect: (port: number, host: string) => void
public onClose: () => void

public expectedWrites: { call: string, response: string }[] = []

private _connected: boolean = false

constructor () {
super()

let cb = onNextSocket.shift()
if (cb) {
cb(this)
}

sockets.push(this)
}

public static mockSockets () {
return sockets
}
public static mockOnNextSocket (cb: (s: Socket) => void) {
onNextSocket.push(cb)
}
// this.emit('connect')
// this.emit('close')
// this.emit('end')

public setEncoding () {
}

public mockExpectedWrite(call: string, response: string) {
this.expectedWrites.push({ call, response })
}

public connect (port, host, cb) {
if (this.onConnect) this.onConnect(port, host)
setTimeoutOrg(() => {
if (cb)
cb()

this.setConnected()
}, 3)

}
public write (buff: Buffer, encoding: string = 'utf8') {
expect(this.expectedWrites).not.toHaveLength(0)

const w = this.expectedWrites.shift()
if (w) {
expect(buff).toEqual(w.call)
this.emit('data', w.response)
}

if (this.onWrite) {
this.onWrite(buff, encoding)
}
}
public end () {
this.setEnd()
this.setClosed()
}

public mockClose () {
this.setClosed()
}
public mockData (data: Buffer) {
this.emit('data', data)
}

private setConnected () {
if (this._connected !== true) {
this._connected = true
}
this.emit('connect')
}
private setClosed () {
if (this._connected !== false) {
this._connected = false
}
this.emit('close')
if (this.onClose) this.onClose()
}
private setEnd () {
if (this._connected !== false) {
this._connected = false
}
this.emit('end')
}
}
Loading

0 comments on commit 01b4634

Please sign in to comment.