From 3ea09cb9daea1eb84080b908ad4a67ad67c37bc7 Mon Sep 17 00:00:00 2001 From: Alex Fung <2409008+alex-fung@users.noreply.github.com> Date: Thu, 5 Sep 2024 17:35:05 -0400 Subject: [PATCH 1/3] update test --- .../__tests__/WalletProviderMobile-test.tsx | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx b/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx index 33507cf33..0e31b0f8f 100644 --- a/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx +++ b/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx @@ -221,7 +221,7 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { }); it('loads the custom mobile wallet adapter into state as the default', () => { renderTest({}); - expect(ref.current?.getWalletContextState().wallet?.adapter).toBe(customAdapter); + expect(ref.current?.getWalletContextState().wallet?.adapter).toBe(undefined); }); it('does not construct any further mobile wallet adapters', () => { renderTest({}); @@ -234,7 +234,7 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { }); it('loads the mobile wallet adapter into state as the default', () => { renderTest({}); - expect(ref.current?.getWalletContextState().wallet?.adapter.name).toBe(SolanaMobileWalletAdapterWalletName); + expect(ref.current?.getWalletContextState().wallet?.adapter.name).toBe(undefined); }); it('loads no public key into state', () => { renderTest({}); @@ -326,10 +326,14 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { describe('onError', () => { let onError: jest.Mock; let errorThrown: WalletError; - beforeEach(() => { + beforeEach(async () => { errorThrown = new WalletError('o no'); onError = jest.fn(); renderTest({ onError }); + await act(async () => { + ref.current?.getWalletContextState().select(SolanaMobileWalletAdapterWalletName); + await Promise.resolve(); // Flush all promises in effects after calling `select()`. + }); }); describe('when the wallet emits an error', () => { let adapter: Adapter; @@ -452,8 +456,8 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { mobileWalletAdapter.disconnect(); }); }); - it('should not clear the stored wallet name', () => { - expect(localStorage.removeItem).not.toHaveBeenCalled(); + it('should clear the stored wallet name', () => { + expect(localStorage.removeItem).toHaveBeenCalled(); }); }); describe('when window beforeunload event fires', () => { @@ -470,8 +474,8 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { mobileWalletAdapter.disconnect(); }); }); - it('should not clear the stored wallet name', () => { - expect(localStorage.removeItem).not.toHaveBeenCalled(); + it('should clear the stored wallet name', () => { + expect(localStorage.removeItem).toHaveBeenCalled(); }); it('should clear out the state', () => { expect(ref.current?.getWalletContextState()).toMatchObject({ From 5df03dfb219628ac97b5c226ccff5f9dabf5b292 Mon Sep 17 00:00:00 2001 From: Alex Fung <2409008+alex-fung@users.noreply.github.com> Date: Thu, 5 Sep 2024 17:36:45 -0400 Subject: [PATCH 2/3] update test names --- .../core/react/src/__tests__/WalletProviderMobile-test.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx b/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx index 0e31b0f8f..e27c22b34 100644 --- a/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx +++ b/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx @@ -219,7 +219,7 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { adapters.push(customAdapter); jest.clearAllMocks(); }); - it('loads the custom mobile wallet adapter into state as the default', () => { + it('does not load the custom mobile wallet adapter into state as the default', () => { renderTest({}); expect(ref.current?.getWalletContextState().wallet?.adapter).toBe(undefined); }); @@ -232,7 +232,7 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { beforeEach(() => { localStorage.removeItem(WALLET_NAME_CACHE_KEY); }); - it('loads the mobile wallet adapter into state as the default', () => { + it('does not load the mobile wallet adapter into state as the default', () => { renderTest({}); expect(ref.current?.getWalletContextState().wallet?.adapter.name).toBe(undefined); }); From c28ccdd0063133d513d9d3927242d9e366669351 Mon Sep 17 00:00:00 2001 From: Alex Fung <2409008+alex-fung@users.noreply.github.com> Date: Thu, 5 Sep 2024 18:32:18 -0400 Subject: [PATCH 3/3] update tests --- .../__tests__/WalletProviderMobile-test.tsx | 42 ++++++++++++------- 1 file changed, 27 insertions(+), 15 deletions(-) diff --git a/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx b/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx index e27c22b34..e1153784c 100644 --- a/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx +++ b/packages/core/react/src/__tests__/WalletProviderMobile-test.tsx @@ -201,7 +201,32 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { expect(jest.mocked(SolanaMobileWalletAdapter).mock.calls[0][0].cluster).toBe('fake-cluster-for-test'); }); }); - + describe('when another wallet adapter is supplied in the adapters array', () => { + beforeEach(() => { + adapters.push(new FooWalletAdapter()); + }); + it('does not load the mobile wallet adapter into state as the default', () => { + renderTest({}); + expect(ref.current?.getWalletContextState().wallet?.adapter.name).toBe(undefined); + }); + it('loads no public key into state', () => { + renderTest({}); + expect(ref.current?.getWalletContextState().publicKey).toBeNull(); + }); + }); + describe('when no wallet adapter is supplied in the adapters array', () => { + beforeEach(() => { + adapters = []; + }); + it('does load the mobile wallet adapter into state as the default', () => { + renderTest({}); + expect(ref.current?.getWalletContextState().wallet?.adapter.name).toBe(SolanaMobileWalletAdapterWalletName); + }); + it('loads no public key into state', () => { + renderTest({}); + expect(ref.current?.getWalletContextState().publicKey).toBeNull(); + }); + }); describe('when a custom mobile wallet adapter is supplied in the adapters array', () => { let customAdapter: Adapter; const CUSTOM_APP_IDENTITY = { @@ -228,19 +253,6 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { expect(jest.mocked(SolanaMobileWalletAdapter).mock.calls.length).toBe(0); }); }); - describe('when there exists no stored wallet name', () => { - beforeEach(() => { - localStorage.removeItem(WALLET_NAME_CACHE_KEY); - }); - it('does not load the mobile wallet adapter into state as the default', () => { - renderTest({}); - expect(ref.current?.getWalletContextState().wallet?.adapter.name).toBe(undefined); - }); - it('loads no public key into state', () => { - renderTest({}); - expect(ref.current?.getWalletContextState().publicKey).toBeNull(); - }); - }); describe('when there exists a stored wallet name', () => { beforeEach(() => { localStorage.setItem(WALLET_NAME_CACHE_KEY, JSON.stringify('FooWallet')); @@ -337,7 +349,7 @@ describe('WalletProvider when the environment is `MOBILE_WEB`', () => { }); describe('when the wallet emits an error', () => { let adapter: Adapter; - beforeEach(() => { + beforeEach(async () => { act(() => { adapter = ref.current?.getWalletContextState().wallet?.adapter as SolanaMobileWalletAdapter; adapter.emit('error', errorThrown);