From 1a269cab9b266a9e3933da5ed247caa3242c8994 Mon Sep 17 00:00:00 2001 From: JasonEtco Date: Thu, 7 Dec 2017 16:41:20 -0500 Subject: [PATCH] Add a failing test :( --- tests/server.test.js | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/tests/server.test.js b/tests/server.test.js index ba326d42..1fee8cea 100644 --- a/tests/server.test.js +++ b/tests/server.test.js @@ -1,11 +1,26 @@ const createServer = require('../lib/server') const request = require('supertest') +const EventSource = require('eventsource') describe('server', () => { - let server + let app, server, events, url, channel - beforeEach(() => { - server = createServer() + beforeEach((done) => { + channel = '/fake-channel' + app = createServer() + + server = app.listen(0, () => { + url = `http://127.0.0.1:${server.address().port}${channel}` + + // Wait for event source to be ready + events = new EventSource(url) + events.addEventListener('ready', () => done()) + }) + }) + + afterEach(() => { + server && server.close() + events && events.close() }) describe('GET /', () => { @@ -18,12 +33,26 @@ describe('server', () => { describe('GET /:channel', () => { it('returns the proper HTML', async () => { - const res = await request(server).get('/fake-channel') + const res = await request(server).get(channel) expect(res.status).toBe(200) expect(res.text).toMatchSnapshot() }) }) + describe('events', () => { + it('emits events', async () => { + const spy = jest.fn() + + events.addEventListener('message', (message) => { + spy(message) + }) + + const res = await request(server).post(channel).send({ payload: true }) + expect(res.status).toBe(200) + expect(spy).toHaveBeenCalled() + }) + }) + it('POST /:channel/redeliver re-emits a payload', async () => { const payload = { foo: true,