Skip to content

Commit d9cd5ff

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

File tree

2 files changed

+84
-90
lines changed

2 files changed

+84
-90
lines changed

spec/ParseLiveQueryServer.spec.js

Lines changed: 82 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,7 @@ const queryHashValue = 'hash';
99
const testUserId = 'userId';
1010
const testClassName = 'TestObject';
1111

12-
const ASYNC_TEST_WAIT_TIME = 100;
13-
14-
function resolveAfter(result, msTimeout) {
15-
return new Promise(res => {
16-
setTimeout(() => {
17-
res(result);
18-
}, msTimeout);
19-
});
20-
}
12+
const timeout = () => jasmine.timeout(100);
2113

2214
describe('ParseLiveQueryServer', function () {
2315
beforeEach(function (done) {
@@ -760,10 +752,10 @@ describe('ParseLiveQueryServer', function () {
760752

761753
// Make sure we send command to client, since _matchesACL is async, we have to
762754
// wait and check
763-
setTimeout(function () {
764-
expect(client.pushDelete).toHaveBeenCalled();
765-
done();
766-
}, ASYNC_TEST_WAIT_TIME);
755+
await timeout();
756+
757+
expect(client.pushDelete).toHaveBeenCalled();
758+
done();
767759
});
768760

769761
it('has no subscription and can handle object save command', async () => {
@@ -795,14 +787,14 @@ describe('ParseLiveQueryServer', function () {
795787
parseLiveQueryServer._onAfterSave(message);
796788

797789
// 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);
790+
await timeout();
791+
792+
expect(client.pushCreate).not.toHaveBeenCalled();
793+
expect(client.pushEnter).not.toHaveBeenCalled();
794+
expect(client.pushUpdate).not.toHaveBeenCalled();
795+
expect(client.pushDelete).not.toHaveBeenCalled();
796+
expect(client.pushLeave).not.toHaveBeenCalled();
797+
done();
806798
});
807799

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

834826
// 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);
827+
await timeout();
828+
829+
expect(client.pushCreate).not.toHaveBeenCalled();
830+
expect(client.pushEnter).toHaveBeenCalled();
831+
expect(client.pushUpdate).not.toHaveBeenCalled();
832+
expect(client.pushDelete).not.toHaveBeenCalled();
833+
expect(client.pushLeave).not.toHaveBeenCalled();
834+
done();
843835
});
844836

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

867859
// 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);
860+
await timeout();
861+
862+
expect(client.pushCreate).not.toHaveBeenCalled();
863+
expect(client.pushEnter).not.toHaveBeenCalled();
864+
expect(client.pushUpdate).toHaveBeenCalled();
865+
expect(client.pushDelete).not.toHaveBeenCalled();
866+
expect(client.pushLeave).not.toHaveBeenCalled();
867+
done();
876868
});
877869

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

904896
// 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);
897+
await timeout();
898+
899+
expect(client.pushCreate).not.toHaveBeenCalled();
900+
expect(client.pushEnter).not.toHaveBeenCalled();
901+
expect(client.pushUpdate).not.toHaveBeenCalled();
902+
expect(client.pushDelete).not.toHaveBeenCalled();
903+
expect(client.pushLeave).toHaveBeenCalled();
904+
done();
913905
});
914906

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

918910
Parse.Cloud.afterLiveQueryEvent('TestObject', () => {
919911
// Simulate delay due to trigger, auth, etc.
920-
return resolveAfter(null, 10);
912+
return jasmine.timeout(10);
921913
});
922914

923915
// Make mock request message
@@ -954,29 +946,29 @@ describe('ParseLiveQueryServer', function () {
954946
};
955947
parseLiveQueryServer._matchesACL = function () {
956948
// Simulate call
957-
return resolveAfter(true, 10);
949+
return jasmine.timeout(10).then(() => true);
958950
};
959951
parseLiveQueryServer._onAfterSave(message);
960952

961953
// 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);
954+
await timeout();
955+
956+
expect(client.pushCreate).not.toHaveBeenCalled();
957+
expect(client.pushEnter).toHaveBeenCalledTimes(1);
958+
expect(client.pushEnter).toHaveBeenCalledWith(
959+
requestId3,
960+
{ key: 'value', className: 'TestObject' },
961+
{ key: 'originalValue', className: 'TestObject' }
962+
);
963+
expect(client.pushUpdate).not.toHaveBeenCalled();
964+
expect(client.pushDelete).not.toHaveBeenCalled();
965+
expect(client.pushLeave).toHaveBeenCalledTimes(1);
966+
expect(client.pushLeave).toHaveBeenCalledWith(
967+
requestId2,
968+
{ key: 'value', className: 'TestObject' },
969+
{ key: 'originalValue', className: 'TestObject' }
970+
);
971+
done();
980972
});
981973

982974
it('can handle update command with original object', async done => {
@@ -1013,15 +1005,15 @@ describe('ParseLiveQueryServer', function () {
10131005
parseLiveQueryServer._onAfterSave(message);
10141006

10151007
// 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]);
1008+
await timeout();
10201009

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

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

10491041
// 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);
1042+
await timeout();
1043+
1044+
expect(client.pushCreate).toHaveBeenCalled();
1045+
expect(client.pushEnter).not.toHaveBeenCalled();
1046+
expect(client.pushUpdate).not.toHaveBeenCalled();
1047+
expect(client.pushDelete).not.toHaveBeenCalled();
1048+
expect(client.pushLeave).not.toHaveBeenCalled();
1049+
done();
10581050
});
10591051

10601052
it('can handle create command with fields', async done => {
@@ -1097,14 +1089,14 @@ describe('ParseLiveQueryServer', function () {
10971089
parseLiveQueryServer._onAfterSave(message);
10981090

10991091
// 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);
1092+
await timeout();
1093+
1094+
expect(client.pushCreate).toHaveBeenCalled();
1095+
const args = parseWebSocket.send.calls.mostRecent().args;
1096+
const toSend = JSON.parse(args[0]);
1097+
expect(toSend.object).toBeDefined();
1098+
expect(toSend.original).toBeUndefined();
1099+
done();
11081100
});
11091101

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

spec/helper.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -515,3 +515,5 @@ jasmine.restoreLibrary = function (library, name) {
515515
}
516516
require(library)[name] = libraryCache[library][name];
517517
};
518+
519+
jasmine.timeout = t => new Promise(resolve => setTimeout(resolve, t));

0 commit comments

Comments
 (0)