|
| 1 | +import { getPersistentState } from './getPersistentState'; |
| 2 | +import { StateMetadata } from '@metamask/base-controller'; |
| 3 | + |
| 4 | +describe('getPersistentState', () => { |
| 5 | + it('return empty state', () => { |
| 6 | + expect(getPersistentState({}, {})).toStrictEqual({}); |
| 7 | + }); |
| 8 | + |
| 9 | + it('return empty state when no properties are persistent', () => { |
| 10 | + const persistentState = getPersistentState( |
| 11 | + { count: 1 }, |
| 12 | + { count: { anonymous: false, persist: false } }, |
| 13 | + ); |
| 14 | + expect(persistentState).toStrictEqual({}); |
| 15 | + }); |
| 16 | + |
| 17 | + it('return persistent state', () => { |
| 18 | + const persistentState = getPersistentState( |
| 19 | + { |
| 20 | + password: 'secret password', |
| 21 | + privateKey: '123', |
| 22 | + network: 'mainnet', |
| 23 | + tokens: ['DAI', 'USDC'], |
| 24 | + }, |
| 25 | + { |
| 26 | + password: { |
| 27 | + anonymous: false, |
| 28 | + persist: true, |
| 29 | + }, |
| 30 | + privateKey: { |
| 31 | + anonymous: false, |
| 32 | + persist: true, |
| 33 | + }, |
| 34 | + network: { |
| 35 | + anonymous: false, |
| 36 | + persist: false, |
| 37 | + }, |
| 38 | + tokens: { |
| 39 | + anonymous: false, |
| 40 | + persist: false, |
| 41 | + }, |
| 42 | + }, |
| 43 | + ); |
| 44 | + expect(persistentState).toStrictEqual({ |
| 45 | + password: 'secret password', |
| 46 | + privateKey: '123', |
| 47 | + }); |
| 48 | + }); |
| 49 | + |
| 50 | + it('use function to derive persistent state', () => { |
| 51 | + const normalizeTransactionHash = (hash: string) => hash.toLowerCase(); |
| 52 | + |
| 53 | + const persistentState = getPersistentState( |
| 54 | + { |
| 55 | + transactionHash: '0X1234', |
| 56 | + }, |
| 57 | + { |
| 58 | + transactionHash: { |
| 59 | + anonymous: false, |
| 60 | + persist: normalizeTransactionHash, |
| 61 | + }, |
| 62 | + }, |
| 63 | + ); |
| 64 | + |
| 65 | + expect(persistentState).toStrictEqual({ transactionHash: '0x1234' }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('allow returning a partial object from a persist function', () => { |
| 69 | + const getPersistentTxMeta = (txMeta: { hash: string; value: number }) => ({ |
| 70 | + value: txMeta.value, |
| 71 | + }); |
| 72 | + |
| 73 | + const persistentState = getPersistentState( |
| 74 | + { |
| 75 | + txMeta: { |
| 76 | + hash: '0x123', |
| 77 | + value: 10, |
| 78 | + }, |
| 79 | + }, |
| 80 | + { |
| 81 | + txMeta: { |
| 82 | + anonymous: false, |
| 83 | + persist: getPersistentTxMeta, |
| 84 | + }, |
| 85 | + }, |
| 86 | + ); |
| 87 | + |
| 88 | + expect(persistentState).toStrictEqual({ txMeta: { value: 10 } }); |
| 89 | + }); |
| 90 | + |
| 91 | + it('allow returning a nested partial object from a persist function', () => { |
| 92 | + const getPersistentTxMeta = (txMeta: { |
| 93 | + hash: string; |
| 94 | + value: number; |
| 95 | + history: { hash: string; value: number }[]; |
| 96 | + }) => ({ |
| 97 | + history: txMeta.history.map((entry) => ({ value: entry.value })), |
| 98 | + value: txMeta.value, |
| 99 | + }); |
| 100 | + |
| 101 | + const persistentState = getPersistentState( |
| 102 | + { |
| 103 | + txMeta: { |
| 104 | + hash: '0x123', |
| 105 | + history: [ |
| 106 | + { |
| 107 | + hash: '0x123', |
| 108 | + value: 9, |
| 109 | + }, |
| 110 | + ], |
| 111 | + value: 10, |
| 112 | + }, |
| 113 | + }, |
| 114 | + { |
| 115 | + txMeta: { |
| 116 | + anonymous: false, |
| 117 | + persist: getPersistentTxMeta, |
| 118 | + }, |
| 119 | + }, |
| 120 | + ); |
| 121 | + |
| 122 | + expect(persistentState).toStrictEqual({ |
| 123 | + txMeta: { history: [{ value: 9 }], value: 10 }, |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('allow transforming types in a persist function', () => { |
| 128 | + const persistentState = getPersistentState( |
| 129 | + { |
| 130 | + count: '1', |
| 131 | + }, |
| 132 | + { |
| 133 | + count: { |
| 134 | + anonymous: false, |
| 135 | + persist: (count) => Number(count), |
| 136 | + }, |
| 137 | + }, |
| 138 | + ); |
| 139 | + |
| 140 | + expect(persistentState).toStrictEqual({ count: 1 }); |
| 141 | + }); |
| 142 | + |
| 143 | + // New test cases for the two key changes |
| 144 | + |
| 145 | + it('exclude state property when no metadata exists for a key', () => { |
| 146 | + const state = { |
| 147 | + password: 'secret password', |
| 148 | + privateKey: '123', |
| 149 | + network: 'mainnet', |
| 150 | + }; |
| 151 | + const metadata = { |
| 152 | + password: { |
| 153 | + anonymous: false, |
| 154 | + persist: true, |
| 155 | + }, |
| 156 | + privateKey: { |
| 157 | + anonymous: false, |
| 158 | + persist: true, |
| 159 | + }, |
| 160 | + } as unknown as StateMetadata<typeof state>; |
| 161 | + |
| 162 | + const persistentState = getPersistentState(state, metadata); |
| 163 | + |
| 164 | + expect(persistentState).toStrictEqual({ |
| 165 | + password: 'secret password', |
| 166 | + privateKey: '123', |
| 167 | + }); |
| 168 | + }); |
| 169 | + |
| 170 | + it('exclude state property when an error occurs during derivation', () => { |
| 171 | + const state = { |
| 172 | + password: 'secret password', |
| 173 | + privateKey: '123', |
| 174 | + network: 'mainnet', |
| 175 | + }; |
| 176 | + const metadata = { |
| 177 | + password: { |
| 178 | + anonymous: false, |
| 179 | + persist: true, |
| 180 | + }, |
| 181 | + privateKey: { |
| 182 | + anonymous: false, |
| 183 | + persist: () => { |
| 184 | + throw new Error('Derivation error'); |
| 185 | + }, |
| 186 | + }, |
| 187 | + } as unknown as StateMetadata<typeof state>; |
| 188 | + |
| 189 | + const persistentState = getPersistentState(state, metadata); |
| 190 | + |
| 191 | + expect(persistentState).toStrictEqual({ |
| 192 | + password: 'secret password', |
| 193 | + }); |
| 194 | + }); |
| 195 | + |
| 196 | + it('exclude nested objects without metadata', () => { |
| 197 | + const state = { |
| 198 | + user: { |
| 199 | + name: 'John', |
| 200 | + settings: { |
| 201 | + theme: 'dark', |
| 202 | + notifications: true, |
| 203 | + }, |
| 204 | + }, |
| 205 | + preferences: { |
| 206 | + language: 'en', |
| 207 | + currency: 'USD', |
| 208 | + }, |
| 209 | + }; |
| 210 | + const metadata = { |
| 211 | + user: { |
| 212 | + anonymous: false, |
| 213 | + persist: true, |
| 214 | + }, |
| 215 | + } as unknown as StateMetadata<typeof state>; |
| 216 | + |
| 217 | + const persistentState = getPersistentState(state, metadata); |
| 218 | + |
| 219 | + expect(persistentState).toStrictEqual({ |
| 220 | + user: { |
| 221 | + name: 'John', |
| 222 | + settings: { |
| 223 | + theme: 'dark', |
| 224 | + notifications: true, |
| 225 | + }, |
| 226 | + }, |
| 227 | + }); |
| 228 | + }); |
| 229 | +}); |
0 commit comments