Skip to content

Commit

Permalink
Basic integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Aug 18, 2021
1 parent a1b554a commit 57d357d
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
40 changes: 40 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"@types/config": "0.0.38",
"@types/country-list": "^2.1.0",
"@types/event-to-promise": "^0.7.1",
"@types/eventsource": "^1.1.6",
"@types/express": "^4.17.11",
"@types/express-fileupload": "^1.1.6",
"@types/express-handlebars": "^3.1.0",
Expand Down Expand Up @@ -133,6 +134,7 @@
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-html": "^6.1.1",
"event-to-promise": "^0.8.0",
"eventsource": "^1.1.0",
"highlight.js": "^10.6.0",
"html-webpack-plugin": "^5.2.0",
"image-minimizer-webpack-plugin": "^2.2.0",
Expand Down
41 changes: 40 additions & 1 deletion src/test/integration/things-test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { server, chai, mockAdapter } from '../common';
import { server, httpServer, chai, mockAdapter } from '../common';
import { TEST_USER, createUser, headerAuth } from '../user';
import e2p from 'event-to-promise';
import { webSocketOpen, webSocketRead, webSocketSend, webSocketClose } from '../websocket-util';
import WebSocket from 'ws';
import EventSource from 'eventsource';
import { AddressInfo } from 'net';
import * as Constants from '../../constants';
import Event from '../../models/event';
import Events from '../../models/events';
Expand Down Expand Up @@ -57,6 +59,18 @@ const VALIDATION_THING = {
},
};

const EVENT_THING = {
id: 'event-thing1',
title: 'Event Thing',
'@context': 'https://webthings.io/schemas',
events: {
overheated: {
type: 'number',
unit: 'degree celsius',
},
},
};

const piDescr = {
id: 'pi-1',
title: 'pi-1',
Expand Down Expand Up @@ -1021,6 +1035,31 @@ describe('things/', function () {
expect(res.body[0].a).toHaveProperty('timestamp');
});

it('should be able to subscribe to events using EventSource', async () => {
await addDevice(EVENT_THING);
if (!httpServer.address()) {
httpServer.listen();
await e2p(httpServer, 'listening');
}
const addr = <AddressInfo>httpServer.address()!;
const eventSourceURL =
`http://127.0.0.1:${addr.port}${Constants.THINGS_PATH}/` +
`${EVENT_THING.id}/events/overheated?jwt=${jwt}`;
const eventSource = new EventSource(eventSourceURL) as EventTarget & EventSource;
await e2p(eventSource, 'open');
const overheatedEvent = new Event('overheated', 101, EVENT_THING.id);
const [, event] = await Promise.all([
Events.add(overheatedEvent),
e2p(eventSource, 'overheated'),
]);

expect(event.type).toEqual('overheated');
expect(event.data).toEqual('101');

eventSource.close();
httpServer.close();
});

// eslint-disable-next-line @typescript-eslint/quotes
it("should receive thing's action status messages over websocket", async () => {
await addDevice();
Expand Down

0 comments on commit 57d357d

Please sign in to comment.