|
| 1 | +import { expect, test } from "@playwright/test" |
| 2 | +import { |
| 3 | + type Connector, |
| 4 | + sortConnectorsByExplorerWallet, |
| 5 | + sortConnectorsOptimized, |
| 6 | + sortConnectorsWithUnnecessarySpread, |
| 7 | +} from "../src/utils/ConnectorUtil" |
| 8 | + |
| 9 | +test.describe("ConnectorUtil", () => { |
| 10 | + const mockConnectors: Connector[] = [ |
| 11 | + { |
| 12 | + id: "metamask", |
| 13 | + name: "MetaMask", |
| 14 | + type: "injected", |
| 15 | + isExplorerWallet: false, |
| 16 | + }, |
| 17 | + { |
| 18 | + id: "coinbase", |
| 19 | + name: "Coinbase Wallet", |
| 20 | + type: "coinbase", |
| 21 | + isExplorerWallet: true, |
| 22 | + }, |
| 23 | + { |
| 24 | + id: "phantom", |
| 25 | + name: "Phantom", |
| 26 | + type: "phantom", |
| 27 | + isExplorerWallet: false, |
| 28 | + }, |
| 29 | + { |
| 30 | + id: "explorer1", |
| 31 | + name: "Explorer Wallet 1", |
| 32 | + type: "walletConnect", |
| 33 | + isExplorerWallet: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + id: "explorer2", |
| 37 | + name: "A Explorer Wallet", |
| 38 | + type: "walletConnect", |
| 39 | + isExplorerWallet: true, |
| 40 | + }, |
| 41 | + ] |
| 42 | + |
| 43 | + test("sortConnectorsByExplorerWallet should create a copy internally", () => { |
| 44 | + const original = [...mockConnectors] |
| 45 | + const sorted = sortConnectorsByExplorerWallet(mockConnectors) |
| 46 | + |
| 47 | + // Original array should not be mutated |
| 48 | + expect(mockConnectors).toEqual(original) |
| 49 | + |
| 50 | + // Explorer wallets should come first |
| 51 | + const explorerWallets = sorted.filter(c => c.isExplorerWallet) |
| 52 | + const nonExplorerWallets = sorted.filter(c => !c.isExplorerWallet) |
| 53 | + |
| 54 | + expect(explorerWallets.length).toBe(3) |
| 55 | + expect(nonExplorerWallets.length).toBe(2) |
| 56 | + |
| 57 | + // First connectors should be explorer wallets |
| 58 | + expect(sorted.slice(0, 3).every(c => c.isExplorerWallet)).toBe(true) |
| 59 | + // Last connectors should be non-explorer wallets |
| 60 | + expect(sorted.slice(3).every(c => !c.isExplorerWallet)).toBe(true) |
| 61 | + }) |
| 62 | + |
| 63 | + test("unnecessary spread operation should be avoided", () => { |
| 64 | + const original = [...mockConnectors] |
| 65 | + |
| 66 | + // Both methods should produce the same result |
| 67 | + const withSpread = sortConnectorsWithUnnecessarySpread(mockConnectors) |
| 68 | + const optimized = sortConnectorsOptimized(mockConnectors) |
| 69 | + |
| 70 | + expect(withSpread).toEqual(optimized) |
| 71 | + |
| 72 | + // Original should remain unchanged in both cases |
| 73 | + expect(mockConnectors).toEqual(original) |
| 74 | + }) |
| 75 | + |
| 76 | + test("demonstrate performance difference (conceptual)", () => { |
| 77 | + // This test demonstrates the concept - in practice, the spread operation |
| 78 | + // creates an unnecessary copy since the sort function already creates one |
| 79 | + const connectors = Array.from({ length: 1000 }, (_, i) => ({ |
| 80 | + id: `connector-${i}`, |
| 81 | + name: `Connector ${i}`, |
| 82 | + type: "injected" as const, |
| 83 | + isExplorerWallet: i % 3 === 0, |
| 84 | + })) |
| 85 | + |
| 86 | + // The unnecessary spread version creates two array copies: |
| 87 | + // 1. [...connectors] creates the first copy |
| 88 | + // 2. sortConnectorsByExplorerWallet creates the second copy internally |
| 89 | + const start1 = Date.now() |
| 90 | + const withSpread = sortConnectorsWithUnnecessarySpread(connectors) |
| 91 | + const time1 = Date.now() - start1 |
| 92 | + |
| 93 | + // The optimized version creates only one copy: |
| 94 | + // 1. sortConnectorsByExplorerWallet creates the copy internally |
| 95 | + const start2 = Date.now() |
| 96 | + const optimized = sortConnectorsOptimized(connectors) |
| 97 | + const time2 = Date.now() - start2 |
| 98 | + |
| 99 | + // Results should be identical |
| 100 | + expect(withSpread).toEqual(optimized) |
| 101 | + |
| 102 | + // Log the performance difference (optimized should be faster or equal) |
| 103 | + console.log(`With spread: ${time1}ms, Optimized: ${time2}ms`) |
| 104 | + |
| 105 | + // In most cases, the optimized version should perform better or equal |
| 106 | + expect(time2).toBeLessThanOrEqual(time1 + 5) // Allow 5ms tolerance |
| 107 | + }) |
| 108 | +}) |
0 commit comments