From e97ca2c7f3a6e57845aeed1d05ae5e17dc1194ed Mon Sep 17 00:00:00 2001 From: William Grant Date: Mon, 8 Apr 2024 17:25:55 +1000 Subject: [PATCH] test: validity check for signInByLinkedDevice done made stubs for window whisper and storage --- .../unit/onboarding/Onboarding_test.ts | 18 +++++---- ts/test/test-utils/utils/stubbing.ts | 39 +++++++++++++++++++ ts/util/storage.ts | 2 +- 3 files changed, 51 insertions(+), 8 deletions(-) diff --git a/ts/test/session/unit/onboarding/Onboarding_test.ts b/ts/test/session/unit/onboarding/Onboarding_test.ts index ffe38dc4f..9eb152c17 100644 --- a/ts/test/session/unit/onboarding/Onboarding_test.ts +++ b/ts/test/session/unit/onboarding/Onboarding_test.ts @@ -1,6 +1,7 @@ import { expect } from 'chai'; import Sinon from 'sinon'; import { displayNameIsValid } from '../../../../components/registration/utils'; +import { getSwarmPollingInstance } from '../../../../session/apis/snode_api'; import { PubKey } from '../../../../session/types'; import { generateMnemonic, @@ -8,16 +9,16 @@ import { signInByLinkingDevice, } from '../../../../util/accountManager'; import { TestUtils } from '../../../test-utils'; -import { stubI18n, stubWindow, stubWindowLog } from '../../../test-utils/utils'; +import { stubWindow } from '../../../test-utils/utils'; describe('Onboarding', () => { - stubWindowLog(); - stubI18n(); - // TODO[epic=ses-1560] set mnemonic generateMnemonic() ? - beforeEach(() => { - TestUtils.stubData('removeItemById').resolves(); + TestUtils.stubWindowLog(); + TestUtils.stubWindowWhisper(); + TestUtils.stubStorage(); + TestUtils.stubI18n(); TestUtils.stubData('createOrUpdateItem').resolves(); + TestUtils.stubData('removeItemById').resolves(); stubWindow('setOpengroupPruning', () => {}); }); @@ -111,6 +112,9 @@ describe('Onboarding', () => { }); describe('signInByLinkingDevice', () => { + const polledDisplayName = 'Hello World'; + Sinon.stub(getSwarmPollingInstance(), 'pollOnceForOurDisplayName').resolves(polledDisplayName); + const abortController = new AbortController(); it('should return a valid pubkey/account id and display name given a valid recovery password', async () => { @@ -122,12 +126,12 @@ describe('Onboarding', () => { 'english', abortController.signal ); - expect(pubKeyString, 'should not be null').to.not.be.null; expect(pubKeyString, 'should be a string').to.be.a('string'); expect(PubKey.validate(pubKeyString!), 'should be a valid pubkey').to.equal(true); expect(displayName, 'should not be null').to.not.be.null; expect(displayName, 'should be a string').to.be.a('string'); + expect(displayName, `should equal ${polledDisplayName}`).to.equal(polledDisplayName); }); }); }); diff --git a/ts/test/test-utils/utils/stubbing.ts b/ts/test/test-utils/utils/stubbing.ts index a4e6a7bb7..ecca2bf01 100644 --- a/ts/test/test-utils/utils/stubbing.ts +++ b/ts/test/test-utils/utils/stubbing.ts @@ -101,6 +101,23 @@ export const stubWindowFeatureFlags = () => { stubWindow('sessionFeatureFlags', { debug: {} } as any); }; +export const stubWindowWhisper = () => { + stubWindow('Whisper', { + events: { + on: (name: string, callback: (param1?: any, param2?: any) => void) => { + if (enableLogRedirect) { + console.info(`Whisper Event registered ${name} ${callback}`); + } + callback(); + }, + trigger: (name: string, param1?: any, param2?: any) => + enableLogRedirect + ? console.info(`Whisper Event triggered ${name} ${param1} ${param2}`) + : {}, + }, + }); +}; + export async function expectAsyncToThrow(toAwait: () => Promise, errorMessageToCatch: string) { try { await toAwait(); @@ -116,3 +133,25 @@ export const stubI18n = () => { const locale = load({ appLocale: 'en', logger: window.log }); stubWindow('i18n', setupi18n('en', locale.messages)); }; + +export const stubStorage = () => { + return { + get: (key: string, defaultValue?: any) => { + if (enableLogRedirect) { + console.info(`Storage.get ${key} returns ${defaultValue || key}`); + } + + return defaultValue || key; + }, + put: (key: string, value: any) => { + if (enableLogRedirect) { + console.info(`Storage.put ${key} with ${value}`); + } + }, + remove: (key: string) => { + if (enableLogRedirect) { + console.info(`Storage.remove ${key}`); + } + }, + }; +}; diff --git a/ts/util/storage.ts b/ts/util/storage.ts index 2ff785f93..21702128a 100644 --- a/ts/util/storage.ts +++ b/ts/util/storage.ts @@ -47,7 +47,7 @@ function get(key: string, defaultValue?: ValueType) { async function remove(key: string) { if (!ready) { - window.log.warn('Called storage.get before storage is ready. key:', key); + window.log.warn('Called storage.remove before storage is ready. key:', key); } delete items[key];