1
1
import React from 'react'
2
+ import userEvent from '@testing-library/user-event'
2
3
import { instance , mock } from 'ts-mockito'
3
4
4
- import { fireEvent , render , screen } from 'uiSrc/utils/test-utils'
5
+ import { fireEvent , render , screen , waitFor } from 'uiSrc/utils/test-utils'
6
+ import { sendEventTelemetry , TelemetryEvent } from 'uiSrc/telemetry'
5
7
6
8
import AddKeyReJSON , { Props } from './AddKeyReJSON'
7
9
import AddKeyFooter from '../AddKeyFooter/AddKeyFooter'
@@ -14,6 +16,11 @@ jest.mock('../AddKeyFooter/AddKeyFooter', () => ({
14
16
default : jest . fn ( )
15
17
} ) )
16
18
19
+ jest . mock ( 'uiSrc/telemetry' , ( ) => ( {
20
+ ...jest . requireActual ( 'uiSrc/telemetry' ) ,
21
+ sendEventTelemetry : jest . fn ( ) ,
22
+ } ) )
23
+
17
24
const MockAddKeyFooter = ( props : any ) => (
18
25
< div { ...props } />
19
26
)
@@ -61,4 +68,70 @@ describe('AddKeyReJSON', () => {
61
68
)
62
69
expect ( screen . getByTestId ( 'add-key-json-btn' ) ) . not . toBeDisabled ( )
63
70
} )
71
+
72
+ it ( 'should call proper telemetry events after click Upload' , ( ) => {
73
+ const sendEventTelemetryMock = jest . fn ( )
74
+ sendEventTelemetry . mockImplementation ( ( ) => sendEventTelemetryMock )
75
+
76
+ render ( < AddKeyReJSON { ...instance ( mockedProps ) } /> )
77
+
78
+ fireEvent . click ( screen . getByTestId ( 'upload-input-file' ) )
79
+
80
+ expect ( sendEventTelemetry ) . toBeCalledWith ( {
81
+ event : TelemetryEvent . BROWSER_JSON_VALUE_IMPORT_CLICKED ,
82
+ eventData : {
83
+ databaseId : 'instanceId' ,
84
+ }
85
+ } )
86
+ } )
87
+
88
+ it ( 'should load file' , async ( ) => {
89
+ render ( < AddKeyReJSON { ...instance ( mockedProps ) } keyName = "name" /> )
90
+
91
+ const jsonString = JSON . stringify ( { a : 12 } )
92
+ const blob = new Blob ( [ jsonString ] )
93
+ const file = new File ( [ blob ] , 'empty.json' , {
94
+ type : 'application/JSON' ,
95
+ } )
96
+ const fileInput = screen . getByTestId ( 'upload-input-file' )
97
+
98
+ expect ( fileInput ) . toHaveAttribute ( 'accept' , 'application/json, text/plain' )
99
+ expect ( fileInput . files . length ) . toBe ( 0 )
100
+
101
+ await userEvent . upload ( fileInput , file )
102
+
103
+ expect ( fileInput . files . length ) . toBe ( 1 )
104
+ } )
105
+
106
+ it ( 'should set the value from json file' , async ( ) => {
107
+ render ( < AddKeyReJSON { ...instance ( mockedProps ) } keyName = "name" /> )
108
+
109
+ const jsonString = JSON . stringify ( { a : 12 } )
110
+ const blob = new Blob ( [ jsonString ] )
111
+ const file = new File ( [ blob ] , 'empty.json' , {
112
+ type : 'application/JSON' ,
113
+ } )
114
+ const fileInput = screen . getByTestId ( 'upload-input-file' )
115
+
116
+ expect ( fileInput ) . toHaveAttribute ( 'accept' , 'application/json, text/plain' )
117
+
118
+ await userEvent . upload ( fileInput , file )
119
+
120
+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'json-value' ) ) . toHaveValue ( '{"a":12}' ) )
121
+ } )
122
+
123
+ it ( 'should set the incorrect json value from json file' , async ( ) => {
124
+ render ( < AddKeyReJSON { ...instance ( mockedProps ) } keyName = "name" /> )
125
+
126
+ const jsonString = JSON . stringify ( '{ a: 12' )
127
+ const blob = new Blob ( [ jsonString ] )
128
+ const file = new File ( [ blob ] , 'empty.json' , {
129
+ type : 'application/JSON' ,
130
+ } )
131
+ const fileInput = screen . getByTestId ( 'upload-input-file' )
132
+
133
+ await userEvent . upload ( fileInput , file )
134
+
135
+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'json-value' ) ) . toHaveValue ( '"{ a: 12"' ) )
136
+ } )
64
137
} )
0 commit comments