laravel-echo-api-gateway 0.1.2 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/ci.yml +10 -7
- package/.github/workflows/release.yml +2 -2
- package/README.md +19 -1
- package/dist/js-src/Websocket.d.ts +6 -0
- package/dist/laravel-echo-api-gateway.common.js +230 -67
- package/dist/laravel-echo-api-gateway.iife.js +230 -67
- package/dist/laravel-echo-api-gateway.js +230 -67
- package/js-src/Channel.ts +8 -5
- package/js-src/Websocket.ts +92 -44
- package/js-tests/Connector.test.ts +57 -5
- package/package.json +1 -1
- package/phpstan.neon.dist +2 -0
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
import WS from "jest-websocket-mock";
|
|
2
|
-
import {Connector} from "../js-src/Connector";
|
|
3
|
-
import {Channel} from "../js-src/Channel";
|
|
2
|
+
import { Connector } from "../js-src/Connector";
|
|
3
|
+
import { Channel } from "../js-src/Channel";
|
|
4
|
+
|
|
5
|
+
const mockedHost = 'ws://localhost:1234';
|
|
4
6
|
|
|
5
7
|
describe('Connector', () => {
|
|
6
8
|
let server: WS;
|
|
7
9
|
|
|
8
10
|
beforeEach(() => {
|
|
9
|
-
|
|
11
|
+
jest.useRealTimers();
|
|
12
|
+
server = new WS(mockedHost);
|
|
10
13
|
});
|
|
11
14
|
|
|
12
15
|
afterEach(() => {
|
|
@@ -15,7 +18,7 @@ describe('Connector', () => {
|
|
|
15
18
|
|
|
16
19
|
test('socket id is correctly set', async () => {
|
|
17
20
|
const connector = new Connector({
|
|
18
|
-
host:
|
|
21
|
+
host: mockedHost,
|
|
19
22
|
})
|
|
20
23
|
|
|
21
24
|
await server.connected;
|
|
@@ -26,9 +29,29 @@ describe('Connector', () => {
|
|
|
26
29
|
expect(connector.socketId()).toBe('test-socket-id')
|
|
27
30
|
})
|
|
28
31
|
|
|
32
|
+
test('we reconnect to the server on error', async () => {
|
|
33
|
+
const connector = new Connector({
|
|
34
|
+
host: mockedHost,
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
await server.connected;
|
|
38
|
+
await expect(server).toReceiveMessage('{"event":"whoami"}');
|
|
39
|
+
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id"}}')
|
|
40
|
+
|
|
41
|
+
server.close();
|
|
42
|
+
await server.closed;
|
|
43
|
+
server.server.stop(() => (server = new WS(mockedHost)));
|
|
44
|
+
|
|
45
|
+
await server.connected;
|
|
46
|
+
await expect(server).toReceiveMessage('{"event":"whoami"}');
|
|
47
|
+
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id2"}}')
|
|
48
|
+
|
|
49
|
+
expect(connector.socketId()).toBe('test-socket-id2')
|
|
50
|
+
})
|
|
51
|
+
|
|
29
52
|
test('we can subscribe to a channel and listen to events', async () => {
|
|
30
53
|
const connector = new Connector({
|
|
31
|
-
host:
|
|
54
|
+
host: mockedHost,
|
|
32
55
|
})
|
|
33
56
|
|
|
34
57
|
await server.connected;
|
|
@@ -54,4 +77,33 @@ describe('Connector', () => {
|
|
|
54
77
|
expect(handler1).toBeCalled();
|
|
55
78
|
expect(handler2).not.toBeCalled();
|
|
56
79
|
})
|
|
80
|
+
|
|
81
|
+
test('we can send a whisper event', async () => {
|
|
82
|
+
const connector = new Connector({
|
|
83
|
+
host: mockedHost,
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
await server.connected;
|
|
87
|
+
|
|
88
|
+
await expect(server).toReceiveMessage('{"event":"whoami"}');
|
|
89
|
+
server.send('{"event":"whoami","data":{"socket_id":"test-socket-id"}}')
|
|
90
|
+
|
|
91
|
+
const channel = connector.channel('my-test-channel')
|
|
92
|
+
|
|
93
|
+
await expect(server).toReceiveMessage('{"event":"subscribe","data":{"channel":"my-test-channel"}}');
|
|
94
|
+
|
|
95
|
+
server.send('{"event":"subscription_succeeded","channel":"my-test-channel"}')
|
|
96
|
+
|
|
97
|
+
expect(channel).toBeInstanceOf(Channel)
|
|
98
|
+
|
|
99
|
+
const handler1 = jest.fn();
|
|
100
|
+
const handler2 = jest.fn();
|
|
101
|
+
|
|
102
|
+
channel.on('client-whisper', handler1)
|
|
103
|
+
|
|
104
|
+
server.send('{"event":"client-whisper","data":"whisper","channel":"my-test-channel","data":{}}')
|
|
105
|
+
|
|
106
|
+
expect(handler1).toBeCalled();
|
|
107
|
+
expect(handler2).not.toBeCalled();
|
|
108
|
+
})
|
|
57
109
|
});
|
package/package.json
CHANGED