Skip to content

Commit

Permalink
Add more integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benfrancis committed Aug 18, 2021
1 parent 57d357d commit ee82cc4
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/test/integration/things-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,7 +1042,9 @@ describe('things/', function () {
await e2p(httpServer, 'listening');
}
const addr = <AddressInfo>httpServer.address()!;
const eventSourceURL =

// Test event subscription
let 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;
Expand All @@ -1052,11 +1054,48 @@ describe('things/', function () {
Events.add(overheatedEvent),
e2p(eventSource, 'overheated'),
]);

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

eventSource.close();

// Test events subscription
eventSourceURL =
`http://127.0.0.1:${addr.port}${Constants.THINGS_PATH}/` +
`${EVENT_THING.id}/events?jwt=${jwt}`;
const eventsSource = new EventSource(eventSourceURL) as EventTarget & EventSource;
await e2p(eventsSource, 'open');
const overheatedEvent2 = new Event('overheated', 101, EVENT_THING.id);
const [, event2] = await Promise.all([
Events.add(overheatedEvent2),
e2p(eventsSource, 'overheated'),
]);
expect(event2.type).toEqual('overheated');
expect(event2.data).toEqual('101');
eventsSource.close();

// Test non-existent thing errors
eventSourceURL =
`http://127.0.0.1:${addr.port}${Constants.THINGS_PATH}` +
`/non-existent-thing/events/overheated?jwt=${jwt}`;
const thinglessEventSource = new EventSource(eventSourceURL) as EventTarget & EventSource;
thinglessEventSource.onerror = jest.fn();
thinglessEventSource.onopen = jest.fn();
await e2p(thinglessEventSource, 'error');
expect(thinglessEventSource.onopen).not.toBeCalled();
expect(thinglessEventSource.onerror).toBeCalled();

// Test non-existent event errors
eventSourceURL =
`http://127.0.0.1:${addr.port}${Constants.THINGS_PATH}` +
`${EVENT_THING.id}/events/non-existentevent?jwt=${jwt}`;
const eventlessEventSource = new EventSource(eventSourceURL) as EventTarget & EventSource;
eventlessEventSource.onerror = jest.fn();
eventlessEventSource.onopen = jest.fn();
await e2p(eventlessEventSource, 'error');
expect(eventlessEventSource.onopen).not.toBeCalled();
expect(eventlessEventSource.onerror).toBeCalled();

// Clean up
httpServer.close();
});

Expand Down

0 comments on commit ee82cc4

Please sign in to comment.