Skip to content

Commit 6aefdac

Browse files
author
Deepak Kumar
committed
Refactor heartbeat tests to use async/await for improved readability
1 parent 2c141df commit 6aefdac

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

spec/unit/heart-beat.spec.js

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ function executeTestCases(useWebWorkerHeartbeats, mode) {
3535
await disconnectStomp(client);
3636
});
3737

38-
// Find length -
38+
// Find length
3939
const length = data => {
4040
return typeof data === 'string' ? data.length : data.byteLength;
4141
};
4242

4343
// See https://github.com/stomp-js/stompjs/issues/188
44-
it('Should allow server to not send heartbeat header', done => {
44+
it('Should allow server to not send heartbeat header', async () => {
4545
overRideFactory(
4646
client,
4747
class extends WrapperWS {
@@ -59,14 +59,14 @@ function executeTestCases(useWebWorkerHeartbeats, mode) {
5959
},
6060
);
6161

62-
client.onConnect = () => {
63-
done();
64-
};
62+
await new Promise(resolve => {
63+
client.onConnect = resolve;
6564

66-
client.activate();
65+
client.activate();
66+
});
6767
});
6868

69-
const incomingPingTest = done => {
69+
const incomingPingTest = async () => {
7070
client.heartbeatIncoming = 1000;
7171
client.heartbeatOutgoing = 0;
7272

@@ -84,15 +84,17 @@ function executeTestCases(useWebWorkerHeartbeats, mode) {
8484
},
8585
);
8686

87-
client.onWebSocketClose = ev => {
88-
if (client.discardWebsocketOnCommFailure) {
89-
// Discarded socket is closed with a different set of codes.
90-
expect([1006, 4001]).toContain(ev.code);
91-
}
92-
done();
93-
};
87+
await new Promise(resolve => {
88+
client.onWebSocketClose = ev => {
89+
if (client.discardWebsocketOnCommFailure) {
90+
// The discarded socket is closed with a different set of codes.
91+
expect([1006, 4001]).toContain(ev.code);
92+
}
93+
resolve();
94+
};
9495

95-
client.activate();
96+
client.activate();
97+
});
9698
};
9799

98100
it('Should close connection when no incoming ping', incomingPingTest);
@@ -105,7 +107,7 @@ function executeTestCases(useWebWorkerHeartbeats, mode) {
105107
it('Should close connection when no incoming ping', incomingPingTest);
106108
});
107109

108-
it('Should close connection when no outgoing ping', done => {
110+
it('Should close connection when no outgoing ping', async () => {
109111
client.heartbeatIncoming = 0;
110112
client.heartbeatOutgoing = 1000;
111113

@@ -123,11 +125,11 @@ function executeTestCases(useWebWorkerHeartbeats, mode) {
123125
},
124126
);
125127

126-
client.onWebSocketClose = ev => {
127-
done();
128-
};
128+
await new Promise(resolve => {
129+
client.onWebSocketClose = resolve;
129130

130-
client.activate();
131+
client.activate();
132+
});
131133
});
132134
});
133135
}

0 commit comments

Comments
 (0)