Skip to content

Commit 8ab1add

Browse files
author
percypyan
committed
Replace setTimeout by async/await expressions
1 parent 1bb51c4 commit 8ab1add

File tree

1 file changed

+82
-88
lines changed

1 file changed

+82
-88
lines changed

spec/ParseLiveQueryServer.spec.js

Lines changed: 82 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,7 @@ const testClassName = 'TestObject';
1111

1212
const ASYNC_TEST_WAIT_TIME = 100;
1313

14-
function resolveAfter(result, msTimeout) {
15-
return new Promise(res => {
16-
setTimeout(() => {
17-
res(result);
18-
}, msTimeout);
19-
});
20-
}
14+
const timeout = t => new Promise(resolve => setTimeout(resolve, t || ASYNC_TEST_WAIT_TIME));
2115

2216
describe('ParseLiveQueryServer', function () {
2317
beforeEach(function (done) {
@@ -760,10 +754,10 @@ describe('ParseLiveQueryServer', function () {
760754

761755
// Make sure we send command to client, since _matchesACL is async, we have to
762756
// wait and check
763-
setTimeout(function () {
764-
expect(client.pushDelete).toHaveBeenCalled();
765-
done();
766-
}, ASYNC_TEST_WAIT_TIME);
757+
await timeout();
758+
759+
expect(client.pushDelete).toHaveBeenCalled();
760+
done();
767761
});
768762

769763
it('has no subscription and can handle object save command', async () => {
@@ -795,14 +789,14 @@ describe('ParseLiveQueryServer', function () {
795789
parseLiveQueryServer._onAfterSave(message);
796790

797791
// Make sure we do not send command to client
798-
setTimeout(function () {
799-
expect(client.pushCreate).not.toHaveBeenCalled();
800-
expect(client.pushEnter).not.toHaveBeenCalled();
801-
expect(client.pushUpdate).not.toHaveBeenCalled();
802-
expect(client.pushDelete).not.toHaveBeenCalled();
803-
expect(client.pushLeave).not.toHaveBeenCalled();
804-
done();
805-
}, ASYNC_TEST_WAIT_TIME);
792+
await timeout();
793+
794+
expect(client.pushCreate).not.toHaveBeenCalled();
795+
expect(client.pushEnter).not.toHaveBeenCalled();
796+
expect(client.pushUpdate).not.toHaveBeenCalled();
797+
expect(client.pushDelete).not.toHaveBeenCalled();
798+
expect(client.pushLeave).not.toHaveBeenCalled();
799+
done();
806800
});
807801

808802
it('can handle object enter command which matches some subscriptions', async done => {
@@ -832,14 +826,14 @@ describe('ParseLiveQueryServer', function () {
832826
parseLiveQueryServer._onAfterSave(message);
833827

834828
// Make sure we send enter command to client
835-
setTimeout(function () {
836-
expect(client.pushCreate).not.toHaveBeenCalled();
837-
expect(client.pushEnter).toHaveBeenCalled();
838-
expect(client.pushUpdate).not.toHaveBeenCalled();
839-
expect(client.pushDelete).not.toHaveBeenCalled();
840-
expect(client.pushLeave).not.toHaveBeenCalled();
841-
done();
842-
}, ASYNC_TEST_WAIT_TIME);
829+
await timeout();
830+
831+
expect(client.pushCreate).not.toHaveBeenCalled();
832+
expect(client.pushEnter).toHaveBeenCalled();
833+
expect(client.pushUpdate).not.toHaveBeenCalled();
834+
expect(client.pushDelete).not.toHaveBeenCalled();
835+
expect(client.pushLeave).not.toHaveBeenCalled();
836+
done();
843837
});
844838

845839
it('can handle object update command which matches some subscriptions', async done => {
@@ -865,14 +859,14 @@ describe('ParseLiveQueryServer', function () {
865859
parseLiveQueryServer._onAfterSave(message);
866860

867861
// Make sure we send update command to client
868-
setTimeout(function () {
869-
expect(client.pushCreate).not.toHaveBeenCalled();
870-
expect(client.pushEnter).not.toHaveBeenCalled();
871-
expect(client.pushUpdate).toHaveBeenCalled();
872-
expect(client.pushDelete).not.toHaveBeenCalled();
873-
expect(client.pushLeave).not.toHaveBeenCalled();
874-
done();
875-
}, ASYNC_TEST_WAIT_TIME);
862+
await timeout();
863+
864+
expect(client.pushCreate).not.toHaveBeenCalled();
865+
expect(client.pushEnter).not.toHaveBeenCalled();
866+
expect(client.pushUpdate).toHaveBeenCalled();
867+
expect(client.pushDelete).not.toHaveBeenCalled();
868+
expect(client.pushLeave).not.toHaveBeenCalled();
869+
done();
876870
});
877871

878872
it('can handle object leave command which matches some subscriptions', async done => {
@@ -902,22 +896,22 @@ describe('ParseLiveQueryServer', function () {
902896
parseLiveQueryServer._onAfterSave(message);
903897

904898
// Make sure we send leave command to client
905-
setTimeout(function () {
906-
expect(client.pushCreate).not.toHaveBeenCalled();
907-
expect(client.pushEnter).not.toHaveBeenCalled();
908-
expect(client.pushUpdate).not.toHaveBeenCalled();
909-
expect(client.pushDelete).not.toHaveBeenCalled();
910-
expect(client.pushLeave).toHaveBeenCalled();
911-
done();
912-
}, ASYNC_TEST_WAIT_TIME);
899+
await timeout();
900+
901+
expect(client.pushCreate).not.toHaveBeenCalled();
902+
expect(client.pushEnter).not.toHaveBeenCalled();
903+
expect(client.pushUpdate).not.toHaveBeenCalled();
904+
expect(client.pushDelete).not.toHaveBeenCalled();
905+
expect(client.pushLeave).toHaveBeenCalled();
906+
done();
913907
});
914908

915-
it('can handle object multiple commands which matches some subscriptions', async done => {
909+
it('sends correct events for object with multiple subscriptions', async done => {
916910
const parseLiveQueryServer = new ParseLiveQueryServer({});
917911

918912
Parse.Cloud.afterLiveQueryEvent('TestObject', () => {
919913
// Simulate delay due to trigger, auth, etc.
920-
return resolveAfter(null, 10);
914+
return timeout(10);
921915
});
922916

923917
// Make mock request message
@@ -954,29 +948,29 @@ describe('ParseLiveQueryServer', function () {
954948
};
955949
parseLiveQueryServer._matchesACL = function () {
956950
// Simulate call
957-
return resolveAfter(true, 10);
951+
return timeout(10).then(() => true);
958952
};
959953
parseLiveQueryServer._onAfterSave(message);
960954

961955
// Make sure we send leave and enter command to client
962-
setTimeout(function () {
963-
expect(client.pushCreate).not.toHaveBeenCalled();
964-
expect(client.pushEnter).toHaveBeenCalledTimes(1);
965-
expect(client.pushEnter).toHaveBeenCalledWith(
966-
requestId3,
967-
{ key: 'value', className: 'TestObject' },
968-
{ key: 'originalValue', className: 'TestObject' }
969-
);
970-
expect(client.pushUpdate).not.toHaveBeenCalled();
971-
expect(client.pushDelete).not.toHaveBeenCalled();
972-
expect(client.pushLeave).toHaveBeenCalledTimes(1);
973-
expect(client.pushLeave).toHaveBeenCalledWith(
974-
requestId2,
975-
{ key: 'value', className: 'TestObject' },
976-
{ key: 'originalValue', className: 'TestObject' }
977-
);
978-
done();
979-
}, ASYNC_TEST_WAIT_TIME);
956+
await timeout();
957+
958+
expect(client.pushCreate).not.toHaveBeenCalled();
959+
expect(client.pushEnter).toHaveBeenCalledTimes(1);
960+
expect(client.pushEnter).toHaveBeenCalledWith(
961+
requestId3,
962+
{ key: 'value', className: 'TestObject' },
963+
{ key: 'originalValue', className: 'TestObject' }
964+
);
965+
expect(client.pushUpdate).not.toHaveBeenCalled();
966+
expect(client.pushDelete).not.toHaveBeenCalled();
967+
expect(client.pushLeave).toHaveBeenCalledTimes(1);
968+
expect(client.pushLeave).toHaveBeenCalledWith(
969+
requestId2,
970+
{ key: 'value', className: 'TestObject' },
971+
{ key: 'originalValue', className: 'TestObject' }
972+
);
973+
done();
980974
});
981975

982976
it('can handle update command with original object', async done => {
@@ -1013,15 +1007,15 @@ describe('ParseLiveQueryServer', function () {
10131007
parseLiveQueryServer._onAfterSave(message);
10141008

10151009
// Make sure we send update command to client
1016-
setTimeout(function () {
1017-
expect(client.pushUpdate).toHaveBeenCalled();
1018-
const args = parseWebSocket.send.calls.mostRecent().args;
1019-
const toSend = JSON.parse(args[0]);
1010+
await timeout();
10201011

1021-
expect(toSend.object).toBeDefined();
1022-
expect(toSend.original).toBeDefined();
1023-
done();
1024-
}, ASYNC_TEST_WAIT_TIME);
1012+
expect(client.pushUpdate).toHaveBeenCalled();
1013+
const args = parseWebSocket.send.calls.mostRecent().args;
1014+
const toSend = JSON.parse(args[0]);
1015+
1016+
expect(toSend.object).toBeDefined();
1017+
expect(toSend.original).toBeDefined();
1018+
done();
10251019
});
10261020

10271021
it('can handle object create command which matches some subscriptions', async done => {
@@ -1047,14 +1041,14 @@ describe('ParseLiveQueryServer', function () {
10471041
parseLiveQueryServer._onAfterSave(message);
10481042

10491043
// Make sure we send create command to client
1050-
setTimeout(function () {
1051-
expect(client.pushCreate).toHaveBeenCalled();
1052-
expect(client.pushEnter).not.toHaveBeenCalled();
1053-
expect(client.pushUpdate).not.toHaveBeenCalled();
1054-
expect(client.pushDelete).not.toHaveBeenCalled();
1055-
expect(client.pushLeave).not.toHaveBeenCalled();
1056-
done();
1057-
}, ASYNC_TEST_WAIT_TIME);
1044+
await timeout();
1045+
1046+
expect(client.pushCreate).toHaveBeenCalled();
1047+
expect(client.pushEnter).not.toHaveBeenCalled();
1048+
expect(client.pushUpdate).not.toHaveBeenCalled();
1049+
expect(client.pushDelete).not.toHaveBeenCalled();
1050+
expect(client.pushLeave).not.toHaveBeenCalled();
1051+
done();
10581052
});
10591053

10601054
it('can handle create command with fields', async done => {
@@ -1097,14 +1091,14 @@ describe('ParseLiveQueryServer', function () {
10971091
parseLiveQueryServer._onAfterSave(message);
10981092

10991093
// Make sure we send create command to client
1100-
setTimeout(function () {
1101-
expect(client.pushCreate).toHaveBeenCalled();
1102-
const args = parseWebSocket.send.calls.mostRecent().args;
1103-
const toSend = JSON.parse(args[0]);
1104-
expect(toSend.object).toBeDefined();
1105-
expect(toSend.original).toBeUndefined();
1106-
done();
1107-
}, ASYNC_TEST_WAIT_TIME);
1094+
await timeout();
1095+
1096+
expect(client.pushCreate).toHaveBeenCalled();
1097+
const args = parseWebSocket.send.calls.mostRecent().args;
1098+
const toSend = JSON.parse(args[0]);
1099+
expect(toSend.object).toBeDefined();
1100+
expect(toSend.original).toBeUndefined();
1101+
done();
11081102
});
11091103

11101104
it('can match subscription for null or undefined parse object', function () {

0 commit comments

Comments
 (0)