Skip to content

Commit 342e438

Browse files
chore: test code improvements
1 parent 54f37b6 commit 342e438

File tree

6 files changed

+86
-27
lines changed

6 files changed

+86
-27
lines changed

apps/native/tests/shared/constants/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,20 @@ export const DEFAULT_SESSION_PARAMS: SessionParams = {
88
optAccounts: ['1', '2'],
99
accept: true
1010
};
11-
export const DEFAULT_CHAIN_NAME = process.env.DEFAULT_CHAIN_NAME || 'Ethereum';
11+
12+
export const TEST_CHAINS = {
13+
POLYGON: 'Polygon',
14+
ETHEREUM: 'Ethereum',
15+
GNOSIS: 'Gnosis'
16+
} as const;
17+
18+
export type SupportedChain = (typeof TEST_CHAINS)[keyof typeof TEST_CHAINS];
19+
20+
export const TIMEOUTS = {
21+
ANIMATION: 300,
22+
NETWORK_SWITCH: 500,
23+
CONNECTION: 5000,
24+
SESSION_PROPOSAL: 30000
25+
} as const;
26+
27+
export const DEFAULT_CHAIN_NAME = process.env.DEFAULT_CHAIN_NAME || TEST_CHAINS.ETHEREUM;

apps/native/tests/shared/constants/timeouts.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

apps/native/tests/shared/pages/ModalPage.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Locator, Page } from '@playwright/test';
22
import { expect } from '@playwright/test';
3-
import { BASE_URL, DEFAULT_SESSION_PARAMS } from '../constants';
3+
import { BASE_URL, DEFAULT_SESSION_PARAMS, TIMEOUTS } from '../constants';
44
import { WalletValidator } from '../validators/WalletValidator';
55
import { WalletPage } from './WalletPage';
6-
import { TimingRecords } from '../types';
6+
import { SupportedChain, TimingRecords } from '../types';
77
import { ModalValidator } from '../validators/ModalValidator';
88

99
export class ModalPage {
@@ -125,10 +125,10 @@ export class ModalPage {
125125
// await this.page.getByTestId('w3m-connecting-siwe-cancel').click();
126126
// }
127127

128-
async switchNetwork(network: string) {
128+
async switchNetwork(network: SupportedChain) {
129129
await this.page.getByTestId(`w3m-network-switch-${network}`).click();
130-
// The state is chaing too fast and test runner doesn't wait the loading page. It's fastly checking the network selection button and detect that it's switched already.
131-
await this.page.waitForTimeout(300);
130+
// The state is changing too fast and test runner doesn't wait for the loading page
131+
await this.page.waitForTimeout(TIMEOUTS.NETWORK_SWITCH);
132132
}
133133

134134
async openAccountModal() {
@@ -146,7 +146,7 @@ export class ModalPage {
146146
async closeModal() {
147147
await this.page.getByTestId('header-close')?.click?.();
148148
// Wait for the modal fade out animation
149-
await this.page.waitForTimeout(300);
149+
await this.page.waitForTimeout(TIMEOUTS.ANIMATION);
150150
}
151151

152152
async openAllWallets() {
@@ -209,10 +209,16 @@ export class ModalPage {
209209
}
210210

211211
async expectLoaderVisible() {
212-
await expect(this.page.getByTestId('loading-spinner')).toBeVisible();
212+
await expect(
213+
this.page.getByTestId('loading-spinner'),
214+
'Loading spinner should be visible'
215+
).toBeVisible();
213216
}
214217

215218
async expectLoaderHidden() {
216-
await expect(this.page.getByTestId('loading-spinner')).toBeHidden();
219+
await expect(
220+
this.page.getByTestId('loading-spinner'),
221+
'Loading spinner should be hidden'
222+
).toBeHidden();
217223
}
218224
}

apps/native/tests/shared/types/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { TEST_CHAINS } from '../constants';
2+
13
export interface SessionParams {
24
reqAccounts: string[];
35
optAccounts: string[];
@@ -7,3 +9,5 @@ export interface SessionParams {
79
export type TimingRecords = { item: string; timeMs: number }[];
810

911
export type CaipNetworkId = `${string}:${string}`;
12+
13+
export type SupportedChain = (typeof TEST_CHAINS)[keyof typeof TEST_CHAINS];
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { MAXIMUM_WAIT_CONNECTIONS } from '../constants/timeouts';
1+
import { TIMEOUTS } from '../constants';
22

33
export function getMaximumWaitConnections(): number {
44
if (process.env.CI) {
5-
return MAXIMUM_WAIT_CONNECTIONS;
5+
return TIMEOUTS.SESSION_PROPOSAL;
66
}
77

8-
return MAXIMUM_WAIT_CONNECTIONS * 2;
8+
return TIMEOUTS.SESSION_PROPOSAL * 2;
99
}

apps/native/tests/wallet.spec.ts

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { WalletPage } from './shared/pages/WalletPage';
33
import { WalletValidator } from './shared/validators/WalletValidator';
44
import { ModalPage } from './shared/pages/ModalPage';
55
import { ModalValidator } from './shared/validators/ModalValidator';
6-
import { DEFAULT_CHAIN_NAME } from './shared/constants';
6+
import { DEFAULT_CHAIN_NAME, TEST_CHAINS } from './shared/constants';
77

88
let modalPage: ModalPage;
99
let modalValidator: ModalValidator;
@@ -37,21 +37,36 @@ sampleWalletTest.afterAll(async () => {
3737
});
3838

3939
// -- Tests --------------------------------------------------------------------
40+
/**
41+
* Connection Tests
42+
* Tests the basic connection functionality including:
43+
* - Page refresh behavior
44+
* - Network switching
45+
* - Disconnection flows
46+
*/
47+
4048
sampleWalletTest('it should be connected instantly after page refresh', async () => {
4149
await modalPage.page.reload();
4250
await modalValidator.expectToBeConnectedInstantly();
4351
});
4452

53+
/**
54+
* Network Tests
55+
* Tests network-related functionality including:
56+
* - Disabled networks visibility
57+
* - Network switching
58+
* - Network persistence after refresh
59+
*/
60+
4561
sampleWalletTest('it should show disabled networks', async () => {
46-
const disabledNetworks = 'Gnosis';
4762
await modalPage.openAccountModal();
4863
await modalPage.goToNetworks();
49-
await modalValidator.expectNetworksDisabled(disabledNetworks);
64+
await modalValidator.expectNetworksDisabled(TEST_CHAINS.GNOSIS);
5065
await modalPage.closeModal();
5166
});
5267

5368
sampleWalletTest('it should switch networks and sign', async () => {
54-
const chains = ['Polygon', 'Ethereum'];
69+
const chains = [TEST_CHAINS.POLYGON, TEST_CHAINS.ETHEREUM];
5570

5671
async function processChain(index: number) {
5772
if (index >= chains.length) {
@@ -77,34 +92,45 @@ sampleWalletTest('it should switch networks and sign', async () => {
7792
await processChain(index + 1);
7893
}
7994

80-
// Start processing from the first chain
8195
await processChain(0);
8296
});
8397

8498
sampleWalletTest('it should show last connected network after refreshing', async () => {
85-
const chainName = 'Polygon';
86-
8799
await modalPage.openAccountModal();
88100
await modalPage.goToNetworks();
89-
await modalPage.switchNetwork(chainName);
90-
await modalValidator.expectSwitchedNetwork(chainName);
101+
await modalPage.switchNetwork(TEST_CHAINS.POLYGON);
102+
await modalValidator.expectSwitchedNetwork(TEST_CHAINS.POLYGON);
91103
await modalPage.closeModal();
92104

93105
await modalPage.page.reload();
94106

95107
await modalPage.openAccountModal();
96-
await modalValidator.expectSwitchedNetwork(chainName);
108+
await modalValidator.expectSwitchedNetwork(TEST_CHAINS.POLYGON);
97109
await modalPage.closeModal();
98110
});
99111

112+
/**
113+
* Signing Tests
114+
* Tests message signing functionality including:
115+
* - Successful signing flow
116+
* - Rejection flow
117+
*/
118+
100119
sampleWalletTest('it should reject sign', async () => {
101-
const chainName = 'Polygon';
102120
await modalPage.sign();
103-
await walletValidator.expectReceivedSign({ chainName });
121+
await walletValidator.expectReceivedSign({ chainName: TEST_CHAINS.POLYGON });
104122
await walletPage.handleRequest({ accept: false });
105123
await modalValidator.expectRejectedSign();
106124
});
107125

126+
/**
127+
* Disconnection Tests
128+
* Tests various disconnection scenarios including:
129+
* - Hook-based disconnection
130+
* - Wallet-initiated disconnection
131+
* - Manual disconnection
132+
*/
133+
108134
sampleWalletTest('it should disconnect using hook', async () => {
109135
await modalValidator.expectConnected();
110136
await modalPage.clickHookDisconnectButton();
@@ -129,6 +155,14 @@ sampleWalletTest('it should disconnect as expected', async () => {
129155
await modalValidator.expectDisconnected();
130156
});
131157

158+
/**
159+
* Activity Screen Tests
160+
* Tests the Activity screen behavior including:
161+
* - Loader visibility on first visit
162+
* - Loader behavior on subsequent visits
163+
* - Loader behavior after network changes
164+
*/
165+
132166
sampleWalletTest('shows loader behavior on first visit to Activity screen', async () => {
133167
// Connect to wallet
134168
await modalPage.qrCodeFlow(modalPage, walletPage);
@@ -158,8 +192,8 @@ sampleWalletTest('shows loader behavior after network change in Activity screen'
158192

159193
// Change network
160194
await modalPage.goToNetworks();
161-
await modalPage.switchNetwork('Polygon');
162-
await modalValidator.expectSwitchedNetwork('Polygon');
195+
await modalPage.switchNetwork(TEST_CHAINS.POLYGON);
196+
await modalValidator.expectSwitchedNetwork(TEST_CHAINS.POLYGON);
163197

164198
// Visit Activity screen after network change
165199
await modalPage.goToActivity();

0 commit comments

Comments
 (0)