Skip to content

Commit

Permalink
feat: added withMetaMetrics tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt561 committed Nov 25, 2024
1 parent 9234f40 commit 344e7d0
Showing 1 changed file with 99 additions and 0 deletions.
99 changes: 99 additions & 0 deletions app/components/UI/Stake/utils/metaMetrics/withMetaMetrics.test.ts
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);
});
});

0 comments on commit 344e7d0

Please sign in to comment.