-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
app/components/UI/Stake/utils/metaMetrics/withMetaMetrics.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { withMetaMetrics } from './withMetaMetrics'; | ||
import { MetaMetrics } from '../../../../../core/Analytics'; | ||
import { MetaMetricsEvents } from '../../../../hooks/useMetrics'; | ||
|
||
describe('withMetaMetrics', () => { | ||
let trackEventSpy: jest.SpyInstance; | ||
|
||
const MOCK_HANDLER_RESULT = 123; | ||
const mockHandler = () => MOCK_HANDLER_RESULT; | ||
const mockAsyncHandler = async () => MOCK_HANDLER_RESULT; | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
trackEventSpy = jest.spyOn(MetaMetrics.getInstance(), 'trackEvent'); | ||
}); | ||
|
||
it('fires single event when wrapping sync function', () => { | ||
const mockHandlerWithMetaMetrics = withMetaMetrics(mockHandler, { | ||
event: MetaMetricsEvents.STAKE_BUTTON_CLICKED, | ||
properties: { | ||
sample: 'value', | ||
}, | ||
}); | ||
|
||
const result = mockHandlerWithMetaMetrics(); | ||
|
||
expect(result).toEqual(MOCK_HANDLER_RESULT); | ||
expect(trackEventSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('fires array of events when wrapping sync function', () => { | ||
const mockHandlerWithMetaMetrics = withMetaMetrics(mockHandler, [ | ||
{ | ||
event: MetaMetricsEvents.TOOLTIP_OPENED, | ||
properties: { | ||
selected_provider: 'consensys', | ||
text: 'Tooltip Opened', | ||
location: 'Unit Test', | ||
tooltip_name: 'Test Tooltip 1', | ||
}, | ||
}, | ||
{ | ||
event: MetaMetricsEvents.TOOLTIP_OPENED, | ||
properties: { | ||
selected_provider: 'consensys', | ||
text: 'Tooltip Opened', | ||
location: 'Unit Test', | ||
tooltip_name: 'Test Tooltip 2', | ||
}, | ||
}, | ||
]); | ||
|
||
const result = mockHandlerWithMetaMetrics(); | ||
expect(result).toEqual(MOCK_HANDLER_RESULT); | ||
expect(trackEventSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it('fires single event when wrapping async function', async () => { | ||
const mockAsyncHandlerWithMetaMetrics = withMetaMetrics(mockAsyncHandler, { | ||
event: MetaMetricsEvents.STAKE_BUTTON_CLICKED, | ||
properties: { | ||
sample: 'value', | ||
}, | ||
}); | ||
|
||
const result = await mockAsyncHandlerWithMetaMetrics(); | ||
|
||
expect(result).toEqual(MOCK_HANDLER_RESULT); | ||
expect(trackEventSpy).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('fires all events when wrapping async function', async () => { | ||
const mockAsyncHandlerWithMetaMetrics = withMetaMetrics(mockAsyncHandler, [ | ||
{ | ||
event: MetaMetricsEvents.TOOLTIP_OPENED, | ||
properties: { | ||
selected_provider: 'consensys', | ||
text: 'Tooltip Opened', | ||
location: 'Unit Test', | ||
tooltip_name: 'Test Tooltip 1', | ||
}, | ||
}, | ||
{ | ||
event: MetaMetricsEvents.TOOLTIP_OPENED, | ||
properties: { | ||
selected_provider: 'consensys', | ||
text: 'Tooltip Opened', | ||
location: 'Unit Test', | ||
tooltip_name: 'Test Tooltip 2', | ||
}, | ||
}, | ||
]); | ||
|
||
const result = await mockAsyncHandlerWithMetaMetrics(); | ||
|
||
expect(result).toEqual(MOCK_HANDLER_RESULT); | ||
expect(trackEventSpy).toHaveBeenCalledTimes(2); | ||
}); | ||
}); |