1
1
import React from 'react' ;
2
- import { fireEvent } from '@testing-library/react' ;
2
+ import { fireEvent , waitFor } from '@testing-library/react' ;
3
3
import configureMockStore from 'redux-mock-store' ;
4
4
import thunk from 'redux-thunk' ;
5
5
import mockState from '../../../../test/data/mock-state.json' ;
6
6
import { renderWithProvider } from '../../../../test/lib/render-helpers' ;
7
+ import { exportAsFile } from '../../../helpers/utils/export-utils' ;
7
8
import AdvancedTab from '.' ;
8
9
9
10
const mockSetAutoLockTimeLimit = jest . fn ( ) . mockReturnValue ( { type : 'TYPE' } ) ;
10
11
const mockSetShowTestNetworks = jest . fn ( ) ;
11
12
const mockSetShowFiatConversionOnTestnetsPreference = jest . fn ( ) ;
12
13
const mockSetStxPrefEnabled = jest . fn ( ) ;
14
+ const mockDisplayErrorInSettings = jest . fn ( ) ;
13
15
14
16
jest . mock ( '../../../store/actions.ts' , ( ) => {
15
17
return {
@@ -21,6 +23,32 @@ jest.mock('../../../store/actions.ts', () => {
21
23
} ;
22
24
} ) ;
23
25
26
+ jest . mock ( '../../../ducks/app/app.ts' , ( ) => ( {
27
+ displayErrorInSettings : ( ) => mockDisplayErrorInSettings ,
28
+ hideErrorInSettings : ( ) => jest . fn ( ) ,
29
+ } ) ) ;
30
+
31
+ jest . mock ( '../../../helpers/utils/export-utils' , ( ) => ( {
32
+ ...jest . requireActual ( '../../../helpers/utils/export-utils' ) ,
33
+ exportAsFile : jest
34
+ . fn ( )
35
+ . mockResolvedValueOnce ( { } )
36
+ . mockImplementationOnce ( new Error ( 'state file error' ) ) ,
37
+ } ) ) ;
38
+
39
+ jest . mock ( 'webextension-polyfill' , ( ) => ( {
40
+ runtime : {
41
+ getPlatformInfo : jest . fn ( ) . mockResolvedValue ( 'mac' ) ,
42
+ } ,
43
+ } ) ) ;
44
+
45
+ Object . defineProperty ( window , 'stateHooks' , {
46
+ value : {
47
+ getCleanAppState : ( ) => mockState ,
48
+ getLogs : ( ) => [ ] ,
49
+ } ,
50
+ } ) ;
51
+
24
52
describe ( 'AdvancedTab Component' , ( ) => {
25
53
const mockStore = configureMockStore ( [ thunk ] ) ( mockState ) ;
26
54
@@ -105,4 +133,33 @@ describe('AdvancedTab Component', () => {
105
133
expect ( mockSetStxPrefEnabled ) . toHaveBeenCalled ( ) ;
106
134
} ) ;
107
135
} ) ;
136
+
137
+ describe ( 'renderStateLogs' , ( ) => {
138
+ it ( 'should render the toggle button for state log download' , ( ) => {
139
+ const { queryByTestId } = renderWithProvider ( < AdvancedTab /> , mockStore ) ;
140
+ const stateLogButton = queryByTestId ( 'advanced-setting-state-logs' ) ;
141
+ expect ( stateLogButton ) . toBeInTheDocument ( ) ;
142
+ } ) ;
143
+
144
+ it ( 'should call exportAsFile when the toggle button is clicked' , async ( ) => {
145
+ const { queryByTestId } = renderWithProvider ( < AdvancedTab /> , mockStore ) ;
146
+ const stateLogButton = queryByTestId (
147
+ 'advanced-setting-state-logs-button' ,
148
+ ) ;
149
+ fireEvent . click ( stateLogButton ) ;
150
+ await waitFor ( ( ) => {
151
+ expect ( exportAsFile ) . toHaveBeenCalledTimes ( 1 ) ;
152
+ } ) ;
153
+ } ) ;
154
+ it ( 'should call displayErrorInSettings when the state file download fails' , async ( ) => {
155
+ const { queryByTestId } = renderWithProvider ( < AdvancedTab /> , mockStore ) ;
156
+ const stateLogButton = queryByTestId (
157
+ 'advanced-setting-state-logs-button' ,
158
+ ) ;
159
+ fireEvent . click ( stateLogButton ) ;
160
+ await waitFor ( ( ) => {
161
+ expect ( mockDisplayErrorInSettings ) . toHaveBeenCalledTimes ( 1 ) ;
162
+ } ) ;
163
+ } ) ;
164
+ } ) ;
108
165
} ) ;
0 commit comments