-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathMusdCalculatorView.test.tsx
More file actions
98 lines (79 loc) · 2.88 KB
/
MusdCalculatorView.test.tsx
File metadata and controls
98 lines (79 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import React from 'react';
import { render, fireEvent } from '@testing-library/react-native';
import MusdCalculatorView from './MusdCalculatorView';
import { useAnalytics } from '../../../hooks/useAnalytics/useAnalytics';
import {
createMockUseAnalyticsHook,
createMockEventBuilder,
} from '../../../../util/test/analyticsMock';
import { MetaMetricsEvents } from '../../../../core/Analytics';
const mockGoBack = jest.fn();
jest.mock('@react-navigation/native', () => ({
useNavigation: () => ({ goBack: mockGoBack }),
}));
jest.mock('@metamask/design-system-twrnc-preset', () => ({
useTailwind: () => {
const tw = () => ({});
tw.style = (..._args: unknown[]) => ({});
return tw;
},
}));
jest.mock('../../../hooks/useAnalytics/useAnalytics');
jest.mock('../../../Views/ErrorBoundary', () => ({
__esModule: true,
default: ({ children }: { children: React.ReactNode }) => children,
}));
jest.mock('../components/Tabs/MusdCalculatorTab/MusdCalculatorTab', () => {
const ReactActual = jest.requireActual('react');
const { View } = jest.requireActual('react-native');
return {
__esModule: true,
default: () =>
ReactActual.createElement(View, { testID: 'musd-calculator-tab' }),
};
});
jest.mock('../../../../../locales/i18n', () => ({
strings: (key: string) => key,
}));
const mockTrackEvent = jest.fn();
const mockCreateEventBuilder = jest.fn(() => createMockEventBuilder());
describe('MusdCalculatorView', () => {
beforeEach(() => {
jest.clearAllMocks();
jest.mocked(useAnalytics).mockReturnValue(
createMockUseAnalyticsHook({
trackEvent: mockTrackEvent,
createEventBuilder: mockCreateEventBuilder,
}),
);
});
it('renders without crashing', () => {
expect(() => render(<MusdCalculatorView />)).not.toThrow();
});
it('renders the calculator tab', () => {
const { getByTestId } = render(<MusdCalculatorView />);
expect(getByTestId('musd-calculator-tab')).toBeOnTheScreen();
});
it('tracks REWARDS_PAGE_VIEWED on mount with page_type musd_calculator', () => {
render(<MusdCalculatorView />);
expect(mockCreateEventBuilder).toHaveBeenCalledWith(
MetaMetricsEvents.REWARDS_PAGE_VIEWED,
);
const builder = mockCreateEventBuilder.mock.results[0].value;
expect(builder.addProperties).toHaveBeenCalledWith({
page_type: 'musd_calculator',
});
expect(mockTrackEvent).toHaveBeenCalledTimes(1);
});
it('tracks the event only once across re-renders', () => {
const { rerender } = render(<MusdCalculatorView />);
rerender(<MusdCalculatorView />);
rerender(<MusdCalculatorView />);
expect(mockTrackEvent).toHaveBeenCalledTimes(1);
});
it('navigates back when the back button is pressed', () => {
const { getByTestId } = render(<MusdCalculatorView />);
fireEvent.press(getByTestId('header-back-button'));
expect(mockGoBack).toHaveBeenCalledTimes(1);
});
});