From 850a39ac36469142f8073ef001aa02bb278f5cd0 Mon Sep 17 00:00:00 2001 From: Bernardo Garces Chapero Date: Mon, 12 Dec 2022 21:20:13 +0000 Subject: [PATCH 1/3] await add acounts method --- index.js | 2 +- test/index.js | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0e4d88ea..83c57686 100644 --- a/index.js +++ b/index.js @@ -247,7 +247,7 @@ class KeyringController extends EventEmitter { if ((!opts || !opts.mnemonic) && type === KEYRINGS_TYPE_MAP.HD_KEYRING) { keyring.generateRandomMnemonic(); - keyring.addAccounts(); + await keyring.addAccounts(); } const accounts = await keyring.getAccounts(); diff --git a/test/index.js b/test/index.js index 4d961496..7fbd01ef 100644 --- a/test/index.js +++ b/test/index.js @@ -280,6 +280,59 @@ describe('KeyringController', function () { sinon.assert.calledOnce(initSpy); }); + + it('should finish adding HD Keyring accounts before continuing execution', async function () { + let timeoutSet = false; + let addAccountsResolved = false; + + const originalAccAccounts = HdKeyring.prototype.addAccounts; + sinon.stub(HdKeyring.prototype, 'addAccounts').callsFake(function () { + return new Promise((resolve) => { + originalAccAccounts + .bind(this)() + .then((accounts) => { + setTimeout(() => { + addAccountsResolved = true; + resolve(accounts); + }, 100); + + timeoutSet = true; + }); + }); + }); + + const originalGetAccounts = HdKeyring.prototype.getAccounts; + sinon.stub(HdKeyring.prototype, 'getAccounts').callsFake(function () { + return new Promise((resolve, rejects) => { + if (!addAccountsResolved) { + rejects( + new Error('Should not be called before accounts are added'), + ); + } + + originalGetAccounts + .bind(this)() + .then((accounts) => { + resolve(accounts); + }); + }); + }); + + const keyringPromise = keyringController.addNewKeyring('HD Key Tree'); + + await new Promise((resolve) => { + const timer = setInterval(() => { + if (timeoutSet) { + clearInterval(timer); + resolve(); + } + }, 50); + }); + + const keyring = await keyringPromise; + + expect(keyring).toBeInstanceOf(HdKeyring); + }); }); describe('restoreKeyring', function () { From db768fb50e4ae16e84d31067172c3c6561625950 Mon Sep 17 00:00:00 2001 From: Bernardo Garces Chapero Date: Tue, 13 Dec 2022 12:17:57 +0000 Subject: [PATCH 2/3] simplify test --- test/index.js | 38 +++++++++----------------------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/test/index.js b/test/index.js index 7fbd01ef..a532f330 100644 --- a/test/index.js +++ b/test/index.js @@ -282,54 +282,34 @@ describe('KeyringController', function () { }); it('should finish adding HD Keyring accounts before continuing execution', async function () { - let timeoutSet = false; let addAccountsResolved = false; const originalAccAccounts = HdKeyring.prototype.addAccounts; sinon.stub(HdKeyring.prototype, 'addAccounts').callsFake(function () { return new Promise((resolve) => { + // By using setImmediate, this won't be resolved before getAccounts unless it is awaited originalAccAccounts .bind(this)() .then((accounts) => { - setTimeout(() => { + setImmediate(() => { addAccountsResolved = true; resolve(accounts); - }, 100); - - timeoutSet = true; + }); }); }); }); const originalGetAccounts = HdKeyring.prototype.getAccounts; sinon.stub(HdKeyring.prototype, 'getAccounts').callsFake(function () { - return new Promise((resolve, rejects) => { - if (!addAccountsResolved) { - rejects( - new Error('Should not be called before accounts are added'), - ); - } - - originalGetAccounts - .bind(this)() - .then((accounts) => { - resolve(accounts); - }); - }); - }); + // Adds a check to getAccounts to determine if addAccounts is being awaited + if (!addAccountsResolved) { + throw new Error('Should not be called before accounts are added'); + } - const keyringPromise = keyringController.addNewKeyring('HD Key Tree'); - - await new Promise((resolve) => { - const timer = setInterval(() => { - if (timeoutSet) { - clearInterval(timer); - resolve(); - } - }, 50); + return originalGetAccounts.bind(this)(); }); - const keyring = await keyringPromise; + const keyring = await keyringController.addNewKeyring('HD Key Tree'); expect(keyring).toBeInstanceOf(HdKeyring); }); From 9367ababbcf2a15612a6c1e435cab7ae972ccad0 Mon Sep 17 00:00:00 2001 From: Bernardo Garces Chapero Date: Wed, 14 Dec 2022 11:17:29 +0000 Subject: [PATCH 3/3] fix: change the test --- test/index.js | 29 ++++++----------------------- 1 file changed, 6 insertions(+), 23 deletions(-) diff --git a/test/index.js b/test/index.js index a532f330..02713e3e 100644 --- a/test/index.js +++ b/test/index.js @@ -281,37 +281,20 @@ describe('KeyringController', function () { sinon.assert.calledOnce(initSpy); }); - it('should finish adding HD Keyring accounts before continuing execution', async function () { - let addAccountsResolved = false; - + it('should add HD Key Tree when addAccounts is asynchronous', async function () { const originalAccAccounts = HdKeyring.prototype.addAccounts; sinon.stub(HdKeyring.prototype, 'addAccounts').callsFake(function () { return new Promise((resolve) => { - // By using setImmediate, this won't be resolved before getAccounts unless it is awaited - originalAccAccounts - .bind(this)() - .then((accounts) => { - setImmediate(() => { - addAccountsResolved = true; - resolve(accounts); - }); - }); + setImmediate(() => { + resolve(originalAccAccounts.bind(this)()); + }); }); }); - const originalGetAccounts = HdKeyring.prototype.getAccounts; - sinon.stub(HdKeyring.prototype, 'getAccounts').callsFake(function () { - // Adds a check to getAccounts to determine if addAccounts is being awaited - if (!addAccountsResolved) { - throw new Error('Should not be called before accounts are added'); - } - - return originalGetAccounts.bind(this)(); - }); - const keyring = await keyringController.addNewKeyring('HD Key Tree'); - expect(keyring).toBeInstanceOf(HdKeyring); + const keyringAccounts = await keyring.getAccounts(); + expect(keyringAccounts).toHaveLength(1); }); });