From ee9717f395b185ccde7b32203eff4ee82811abb3 Mon Sep 17 00:00:00 2001 From: Jannis R Date: Mon, 26 Mar 2018 01:47:55 +0200 Subject: [PATCH] test :white_check_mark: --- index.js | 6 ++--- package.json | 9 ++++---- test.js | 62 ++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 65 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index fbd6825..2ab6980 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/package.json b/package.json index 9e0c662..16a8d0f 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/test.js b/test.js index 71e067f..ba3643a 100644 --- a/test.js +++ b/test.js @@ -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)