From 87a8e4c216ef41e3371df66e4cee90e561fa6aa5 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Wed, 4 Dec 2019 23:37:49 +0000 Subject: [PATCH] Apply uhoreg's patch (with jesty stuff changed out) Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- spec/unit/crypto.spec.js | 39 +++++++++------------------------------ 1 file changed, 9 insertions(+), 30 deletions(-) diff --git a/spec/unit/crypto.spec.js b/spec/unit/crypto.spec.js index 3ec55a28f..c449d4624 100644 --- a/spec/unit/crypto.spec.js +++ b/spec/unit/crypto.spec.js @@ -312,52 +312,31 @@ describe("Crypto", function() { sender_key: "senderkey", }, }); - /* return a promise and a function. When the function is called, - * the promise will be resolved. - */ - function awaitFunctionCall() { - let func; - const promise = new Promise((resolve, reject) => { - func = function(...args) { - resolve(args); - return new Promise((resolve, reject) => { - // give us some time to process the result before - // continuing - global.setTimeout(resolve, 1); - }); - }; - }); - return {func, promise}; - } - + // replace Alice's sendToDevice function with a mock + aliceClient.sendToDevice = jest.fn().mockResolvedValue(undefined); aliceClient.startClient(); - let promise; // make a room key request, and record the transaction ID for the // sendToDevice call - ({promise, func: aliceClient.sendToDevice} = awaitFunctionCall()); await aliceClient.cancelAndResendEventRoomKeyRequest(event); jest.runAllTimers(); - let args = await promise; - const txnId = args[2]; - jest.runAllTimers(); + await Promise.resolve(); + expect(aliceClient.sendToDevice).toBeCalledTimes(1); + const txnId = aliceClient.sendToDevice.mock.calls[0][2]; // give the room key request manager time to update the state // of the request await Promise.resolve(); // cancel and resend the room key request - ({promise, func: aliceClient.sendToDevice} = awaitFunctionCall()); await aliceClient.cancelAndResendEventRoomKeyRequest(event); jest.runAllTimers(); + await Promise.resolve(); + // cancelAndResend will call sendToDevice twice: // the first call to sendToDevice will be the cancellation - args = await promise; // the second call to sendToDevice will be the key request - ({promise, func: aliceClient.sendToDevice} = awaitFunctionCall()); - jest.runAllTimers(); - args = await promise; - jest.runAllTimers(); - expect(args[2]).not.toBe(txnId); + expect(aliceClient.sendToDevice).toBeCalledTimes(3); + expect(aliceClient.sendToDevice.mock.calls[2][2]).not.toBe(txnId); }); }); });