Skip to content
This repository has been archived by the owner on Jun 9, 2019. It is now read-only.

Commit

Permalink
test ✅
Browse files Browse the repository at this point in the history
  • Loading branch information
derhuerst committed Mar 26, 2018
1 parent a9e145c commit ee9717f
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 12 deletions.
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

const {Server} = require('ws')
const createMonitor = require('hafas-monitor-departures')
const _createMonitor = require('hafas-monitor-departures')

const createMonitorServer = (httpServer, hafas, stations, interval) => {
const wsServer = new Server({server: httpServer})
const createMonitorServer = (server, hafas, stations, interval, createMonitor = _createMonitor) => {
const wsServer = new Server({server})
// todo: wsServer.on('error')

let monitor = null
Expand Down
9 changes: 4 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@
"utf-8-validate": "^4.0.0",
"ws": "^5.1.0"
},
"devDependencies": {
"tap-min": "^1.2.2",
"tape": "^4.8.0"
},
"scripts": {
"test": "env NODE_ENV=dev node test.js | tap-min",
"test": "env NODE_ENV=dev node test.js",
"prepublishOnly": "npm test"
},
"devDependencies": {
"vbb-hafas": "^4.1.1"
}
}
62 changes: 58 additions & 4 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,62 @@
'use strict'

const test = require('tape')
const {Readable} = require('stream')
const http = require('http')
const assert = require('assert')
const WebSocket = require('ws')

const todo = require('.')
const createMonitorServer = require('.')

test('todo', (t) => {
})
const stations = [ // array of station ids
'900000100003' // alexanderplatz
]
const interval = 5 * 1000 // every 5 seconds

const createMockMonitor = (hafas, stations, interval) => {
const monitor = new Readable({
objectMode: true,
read: () => {}
})

let _interval = setInterval(() => {
monitor.push({foo: 'bar'})
}, 1 * 1000)

monitor.stop = () => {
if (_interval !== null) {
clearInterval(_interval)
_interval = null
}
}

return monitor
}

const mockHafas = {
departures: (id, opt) => Promise.resolve([{foo: 'bar'}])
}

const httpServer = http.createServer()
createMonitorServer(httpServer, mockHafas, stations, interval, createMockMonitor)
httpServer.listen(3000)

setTimeout(() => {
const client = new WebSocket('ws://localhost:3000/')

let received = false
client.once('message', (msg) => {
received = true
try {
const dep = JSON.parse(msg)
assert.deepStrictEqual(dep, {foo: 'bar'})
} catch (err) {
assert.ifError(err)
}
client.close()
httpServer.close()
})

setTimeout(() => {
assert.ok(received)
}, interval + 1000)
}, 100)

0 comments on commit ee9717f

Please sign in to comment.