Skip to content

Commit e5c893d

Browse files
committed
Add new tests
Signed-off-by: dan437 <80175477+dan437@users.noreply.github.com>
1 parent 1faf66d commit e5c893d

1 file changed

Lines changed: 120 additions & 0 deletions

File tree

src/SmartTransactionsController.test.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2090,6 +2090,126 @@ describe('SmartTransactionsController', () => {
20902090
);
20912091
});
20922092
});
2093+
2094+
describe('createOrUpdateSmartTransaction', () => {
2095+
beforeEach(() => {
2096+
jest
2097+
.spyOn(SmartTransactionsController.prototype, 'checkPoll')
2098+
.mockImplementation(() => ({}));
2099+
});
2100+
2101+
it('adds metaMetricsProps to new smart transactions', async () => {
2102+
const { smartTransactionsState } =
2103+
getDefaultSmartTransactionsControllerState();
2104+
const newSmartTransaction = {
2105+
uuid: 'new-uuid-test',
2106+
status: SmartTransactionStatuses.PENDING,
2107+
txParams: {
2108+
from: addressFrom,
2109+
},
2110+
};
2111+
2112+
await withController(
2113+
{
2114+
options: {
2115+
state: {
2116+
smartTransactionsState: {
2117+
...smartTransactionsState,
2118+
smartTransactions: {
2119+
[ChainId.mainnet]: [],
2120+
},
2121+
},
2122+
},
2123+
getMetaMetricsProps: jest.fn().mockResolvedValue({
2124+
accountHardwareType: 'Test Hardware',
2125+
accountType: 'test-account',
2126+
deviceModel: 'test-model',
2127+
}),
2128+
},
2129+
},
2130+
async ({ controller }) => {
2131+
controller.updateSmartTransaction(
2132+
newSmartTransaction as SmartTransaction,
2133+
);
2134+
2135+
// Allow async operations to complete
2136+
await flushPromises();
2137+
2138+
// Verify MetaMetricsProps were added
2139+
const updatedTransaction =
2140+
controller.state.smartTransactionsState.smartTransactions[
2141+
ChainId.mainnet
2142+
][0];
2143+
expect(updatedTransaction.accountHardwareType).toBe('Test Hardware');
2144+
expect(updatedTransaction.accountType).toBe('test-account');
2145+
expect(updatedTransaction.deviceModel).toBe('test-model');
2146+
},
2147+
);
2148+
});
2149+
2150+
it('continues without metaMetricsProps if adding them fails', async () => {
2151+
const { smartTransactionsState } =
2152+
getDefaultSmartTransactionsControllerState();
2153+
const newSmartTransaction = {
2154+
uuid: 'new-uuid-test',
2155+
status: SmartTransactionStatuses.PENDING,
2156+
txParams: {
2157+
from: addressFrom,
2158+
},
2159+
};
2160+
2161+
// Mock console.error to verify it's called
2162+
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation();
2163+
2164+
await withController(
2165+
{
2166+
options: {
2167+
state: {
2168+
smartTransactionsState: {
2169+
...smartTransactionsState,
2170+
smartTransactions: {
2171+
[ChainId.mainnet]: [],
2172+
},
2173+
},
2174+
},
2175+
// Mock getting MetaMetricsProps to fail
2176+
getMetaMetricsProps: jest
2177+
.fn()
2178+
.mockRejectedValue(new Error('Test metrics error')),
2179+
},
2180+
},
2181+
async ({ controller }) => {
2182+
controller.updateSmartTransaction(
2183+
newSmartTransaction as SmartTransaction,
2184+
);
2185+
2186+
// Allow async operations to complete
2187+
await flushPromises();
2188+
2189+
// Verify transaction was still added even without metrics props
2190+
const updatedTransaction =
2191+
controller.state.smartTransactionsState.smartTransactions[
2192+
ChainId.mainnet
2193+
][0];
2194+
expect(updatedTransaction.uuid).toBe('new-uuid-test');
2195+
2196+
// These should be undefined since getting metrics props failed
2197+
expect(updatedTransaction.accountHardwareType).toBeUndefined();
2198+
expect(updatedTransaction.accountType).toBeUndefined();
2199+
expect(updatedTransaction.deviceModel).toBeUndefined();
2200+
2201+
// Verify the error was logged
2202+
expect(consoleErrorSpy).toHaveBeenCalledWith(
2203+
'Failed to add metrics props to smart transaction:',
2204+
expect.any(Error),
2205+
);
2206+
2207+
// Clean up the spy
2208+
consoleErrorSpy.mockRestore();
2209+
},
2210+
);
2211+
});
2212+
});
20932213
});
20942214

20952215
type WithControllerCallback<ReturnValue> = ({

0 commit comments

Comments
 (0)