test: unit tests for swapFlowLoggers (#7257)

This commit is contained in:
eddie 2023-09-07 10:07:56 -07:00 committed by GitHub
parent 7306423192
commit 4ba0d8ffc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 91 additions and 11 deletions

@ -57,3 +57,5 @@ export class SwapEventTimestampTracker {
return endTime - startTime
}
}
export const timestampTracker = SwapEventTimestampTracker.getInstance()

@ -0,0 +1,80 @@
import { SwapEventName } from '@uniswap/analytics-events'
import { sendAnalyticsEvent } from 'analytics'
jest.mock('analytics', () => ({
sendAnalyticsEvent: jest.fn(),
}))
jest.mock('./SwapEventTimestampTracker', () => ({
SwapEventType: {
FIRST_SWAP_ACTION: 'FIRST_SWAP_ACTION',
FIRST_QUOTE_FETCH_STARTED: 'FIRST_QUOTE_FETCH_STARTED',
FIRST_SWAP_SIGNATURE_REQUESTED: 'FIRST_SWAP_SIGNATURE_REQUESTED',
FIRST_SWAP_SIGNATURE_COMPLETED: 'FIRST_SWAP_SIGNATURE_COMPLETED',
FIRST_SWAP_SUCCESS: 'FIRST_SWAP_SUCCESS',
},
timestampTracker: {
hasTimestamp: () => false,
setElapsedTime: () => 100,
getElapsedTime: () => 100,
},
}))
import { INTERNAL_ROUTER_PREFERENCE_PRICE, RouterPreference } from 'state/routing/types'
import { logSwapQuoteRequest, logSwapSuccess, maybeLogFirstSwapAction } from './swapFlowLoggers'
describe('swapFlowLoggers', () => {
beforeEach(() => {
jest.clearAllMocks()
})
it('logSwapSuccess calls sendAnalyticsEvent with correct parameters', () => {
const mockHash = 'mockHash'
const mockChainId = 1
const mockAnalyticsContext = { page: 'mockContext' }
logSwapSuccess(mockHash, mockChainId, mockAnalyticsContext)
expect(sendAnalyticsEvent).toHaveBeenCalledWith(SwapEventName.SWAP_TRANSACTION_COMPLETED, {
time_to_swap: 100,
time_to_swap_since_first_input: 100,
hash: mockHash,
chainId: mockChainId,
...mockAnalyticsContext,
})
})
it('maybeLogFirstSwapAction calls sendAnalyticsEvent with correct parameters', () => {
const mockAnalyticsContext = { page: 'mockContext' }
maybeLogFirstSwapAction(mockAnalyticsContext)
expect(sendAnalyticsEvent).toHaveBeenCalledWith(SwapEventName.SWAP_FIRST_ACTION, {
time_to_first_swap_action: 100,
...mockAnalyticsContext,
})
})
it('logSwapQuoteRequest calls sendAnalyticsEvent with correct parameters', () => {
const mockChainId = 1
logSwapQuoteRequest(mockChainId, RouterPreference.X)
expect(sendAnalyticsEvent).toHaveBeenCalledWith(SwapEventName.SWAP_QUOTE_FETCH, {
chainId: mockChainId,
time_to_first_quote_request: 100,
time_to_first_quote_request_since_first_input: 100,
})
})
it('logSwapQuoteRequest excludes perf metrics for price quotes', () => {
const mockChainId = 1
logSwapQuoteRequest(mockChainId, INTERNAL_ROUTER_PREFERENCE_PRICE)
expect(sendAnalyticsEvent).toHaveBeenCalledWith(SwapEventName.SWAP_QUOTE_FETCH, {
chainId: mockChainId,
})
})
})

@ -3,20 +3,18 @@ import { ITraceContext } from 'analytics'
import { sendAnalyticsEvent } from 'analytics'
import { INTERNAL_ROUTER_PREFERENCE_PRICE, RouterPreference } from 'state/routing/types'
import { SwapEventTimestampTracker, SwapEventType } from './SwapEventTimestampTracker'
const tracker = SwapEventTimestampTracker.getInstance()
import { SwapEventType, timestampTracker } from './SwapEventTimestampTracker'
export function logSwapSuccess(hash: string, chainId: number, analyticsContext: ITraceContext) {
const hasSetSwapSuccess = tracker.hasTimestamp(SwapEventType.FIRST_SWAP_SUCCESS)
const elapsedTime = tracker.setElapsedTime(SwapEventType.FIRST_SWAP_SUCCESS)
const hasSetSwapSuccess = timestampTracker.hasTimestamp(SwapEventType.FIRST_SWAP_SUCCESS)
const elapsedTime = timestampTracker.setElapsedTime(SwapEventType.FIRST_SWAP_SUCCESS)
sendAnalyticsEvent(SwapEventName.SWAP_TRANSACTION_COMPLETED, {
// We only log the time-to-swap metric for the first swap of a session,
// so if it was previously set we log undefined here.
time_to_swap: hasSetSwapSuccess ? undefined : elapsedTime,
time_to_swap_since_first_input: hasSetSwapSuccess
? undefined
: tracker.getElapsedTime(SwapEventType.FIRST_SWAP_SUCCESS, SwapEventType.FIRST_SWAP_ACTION),
: timestampTracker.getElapsedTime(SwapEventType.FIRST_SWAP_SUCCESS, SwapEventType.FIRST_SWAP_ACTION),
hash,
chainId,
...analyticsContext,
@ -25,8 +23,8 @@ export function logSwapSuccess(hash: string, chainId: number, analyticsContext:
// We only log the time-to-first-swap-input metric for the first swap input of a session.
export function maybeLogFirstSwapAction(analyticsContext: ITraceContext) {
if (!tracker.hasTimestamp(SwapEventType.FIRST_SWAP_ACTION)) {
const elapsedTime = tracker.setElapsedTime(SwapEventType.FIRST_SWAP_ACTION)
if (!timestampTracker.hasTimestamp(SwapEventType.FIRST_SWAP_ACTION)) {
const elapsedTime = timestampTracker.setElapsedTime(SwapEventType.FIRST_SWAP_ACTION)
sendAnalyticsEvent(SwapEventName.SWAP_FIRST_ACTION, {
time_to_first_swap_action: elapsedTime,
...analyticsContext,
@ -40,14 +38,14 @@ export function logSwapQuoteRequest(
) {
let performanceMetrics = {}
if (routerPreference !== INTERNAL_ROUTER_PREFERENCE_PRICE) {
const hasSetSwapQuote = tracker.hasTimestamp(SwapEventType.FIRST_QUOTE_FETCH_STARTED)
const elapsedTime = tracker.setElapsedTime(SwapEventType.FIRST_QUOTE_FETCH_STARTED)
const hasSetSwapQuote = timestampTracker.hasTimestamp(SwapEventType.FIRST_QUOTE_FETCH_STARTED)
const elapsedTime = timestampTracker.setElapsedTime(SwapEventType.FIRST_QUOTE_FETCH_STARTED)
performanceMetrics = {
// We only log the time_to_first_quote_request metric for the first quote request of a session.
time_to_first_quote_request: hasSetSwapQuote ? undefined : elapsedTime,
time_to_first_quote_request_since_first_input: hasSetSwapQuote
? undefined
: tracker.getElapsedTime(SwapEventType.FIRST_QUOTE_FETCH_STARTED, SwapEventType.FIRST_SWAP_ACTION),
: timestampTracker.getElapsedTime(SwapEventType.FIRST_QUOTE_FETCH_STARTED, SwapEventType.FIRST_SWAP_ACTION),
}
}
sendAnalyticsEvent(SwapEventName.SWAP_QUOTE_FETCH, {