1
1
import React from 'react'
2
2
import { instance , mock } from 'ts-mockito'
3
3
import { MOCK_ANALYSIS_REPORT_DATA } from 'uiSrc/mocks/data/analysis'
4
+ import { INSTANCE_ID_MOCK } from 'uiSrc/mocks/handlers/analytics/clusterDetailsHandlers'
5
+ import { sendEventTelemetry , TelemetryEvent } from 'uiSrc/telemetry'
4
6
import { formatBytes , getGroupTypeDisplay } from 'uiSrc/utils'
5
7
import { numberWithSpaces } from 'uiSrc/utils/numbers'
6
8
import { fireEvent , render , screen , within } from 'uiSrc/utils/test-utils'
7
9
8
10
import AnalysisDataView , { Props } from './AnalysisDataView'
9
11
10
- const mockedProps = mock < Props > ( )
12
+ jest . mock ( 'uiSrc/telemetry' , ( ) => ( {
13
+ ...jest . requireActual ( 'uiSrc/telemetry' ) ,
14
+ sendEventTelemetry : jest . fn ( ) ,
15
+ } ) )
11
16
17
+ const mockedProps = mock < Props > ( )
12
18
const mockReports = [
13
19
{
14
20
id : MOCK_ANALYSIS_REPORT_DATA . id ,
@@ -20,6 +26,11 @@ const mockReports = [
20
26
}
21
27
]
22
28
29
+ const summaryContainerId = 'summary-per-data'
30
+ const analyticsTTLContainerId = 'analysis-ttl'
31
+ const topNameSpacesContainerId = 'top-namespaces'
32
+ const extrapolateResultsId = 'extrapolate-results'
33
+
23
34
describe ( 'AnalysisDataView' , ( ) => {
24
35
it ( 'should render' , ( ) => {
25
36
expect ( render ( < AnalysisDataView { ...instance ( mockedProps ) } /> ) ) . toBeTruthy ( )
@@ -104,7 +115,7 @@ describe('AnalysisDataView', () => {
104
115
< AnalysisDataView { ...instance ( mockedProps ) } reports = { mockReports } data = { mockedData } />
105
116
)
106
117
107
- fireEvent . click ( within ( screen . getByTestId ( 'summary-per-data' ) ) . getByTestId ( 'extrapolate-results' ) )
118
+ fireEvent . click ( within ( screen . getByTestId ( summaryContainerId ) ) . getByTestId ( extrapolateResultsId ) )
108
119
109
120
expect ( screen . getByTestId ( 'total-memory-value' ) ) . toHaveTextContent ( `${ formatBytes ( mockedData . totalMemory . total , 3 ) } ` )
110
121
expect ( screen . getByTestId ( 'total-keys-value' ) ) . toHaveTextContent ( `${ numberWithSpaces ( mockedData . totalKeys . total ) } ` )
@@ -157,7 +168,7 @@ describe('AnalysisDataView', () => {
157
168
render (
158
169
< AnalysisDataView { ...instance ( mockedProps ) } reports = { mockReports } data = { mockedData } />
159
170
)
160
- fireEvent . click ( within ( screen . getByTestId ( 'analysis-ttl' ) ) . getByTestId ( 'extrapolate-results' ) )
171
+ fireEvent . click ( within ( screen . getByTestId ( analyticsTTLContainerId ) ) . getByTestId ( extrapolateResultsId ) )
161
172
162
173
const expirationGroup = mockedData . expirationGroups [ 1 ]
163
174
@@ -198,7 +209,7 @@ describe('AnalysisDataView', () => {
198
209
render (
199
210
< AnalysisDataView { ...instance ( mockedProps ) } reports = { mockReports } data = { mockedData } />
200
211
)
201
- fireEvent . click ( within ( screen . getByTestId ( 'top-namespaces' ) ) . getByTestId ( 'extrapolate-results' ) )
212
+ fireEvent . click ( within ( screen . getByTestId ( topNameSpacesContainerId ) ) . getByTestId ( extrapolateResultsId ) )
202
213
203
214
const nspTopKeyItem = mockedData . topKeysNsp [ 0 ]
204
215
expect ( screen . getByTestId ( `nsp-usedMemory-value=${ nspTopKeyItem . memory } ` ) )
@@ -221,7 +232,7 @@ describe('AnalysisDataView', () => {
221
232
< AnalysisDataView { ...instance ( mockedProps ) } reports = { mockReports } data = { mockedData } />
222
233
)
223
234
224
- expect ( screen . queryByTestId ( 'extrapolate-results' ) ) . not . toBeInTheDocument ( )
235
+ expect ( screen . queryByTestId ( extrapolateResultsId ) ) . not . toBeInTheDocument ( )
225
236
226
237
expect ( screen . getByTestId ( 'total-memory-value' ) ) . toHaveTextContent ( `${ formatBytes ( mockedData . totalMemory . total , 3 ) } ` )
227
238
expect ( screen . getByTestId ( 'total-keys-value' ) ) . toHaveTextContent ( `${ numberWithSpaces ( mockedData . totalKeys . total ) } ` )
@@ -238,4 +249,40 @@ describe('AnalysisDataView', () => {
238
249
expect ( screen . getAllByTestId ( `keys-value-${ nspTopKeyItem . keys } ` ) [ 0 ] )
239
250
. toHaveTextContent ( `${ numberWithSpaces ( nspTopKeyItem . keys ) } ` )
240
251
} )
252
+
253
+ it ( 'should call proper telemetry events after click extrapolation' , ( ) => {
254
+ const mockedData = {
255
+ ...MOCK_ANALYSIS_REPORT_DATA ,
256
+ progress : {
257
+ total : 80 ,
258
+ scanned : 10000 ,
259
+ processed : 40
260
+ }
261
+ }
262
+ const sendEventTelemetryMock = jest . fn ( )
263
+ sendEventTelemetry . mockImplementation ( ( ) => sendEventTelemetryMock )
264
+
265
+ render (
266
+ < AnalysisDataView { ...instance ( mockedProps ) } reports = { mockReports } data = { mockedData } />
267
+ )
268
+
269
+ const clickAndCheckTelemetry = ( el : HTMLInputElement ) => {
270
+ fireEvent . click ( el )
271
+ expect ( sendEventTelemetry ) . toBeCalledWith ( {
272
+ event : TelemetryEvent . DATABASE_ANALYSIS_EXTRAPOLATION_CHANGED ,
273
+ eventData : {
274
+ databaseId : INSTANCE_ID_MOCK ,
275
+ from : ! el . checked ,
276
+ to : el . checked
277
+ }
278
+ } )
279
+ sendEventTelemetry . mockRestore ( )
280
+ }
281
+
282
+ [ summaryContainerId , analyticsTTLContainerId , topNameSpacesContainerId ] . forEach ( ( id ) => {
283
+ const extrapolateSwitch = within ( screen . getByTestId ( id ) ) . getByTestId ( extrapolateResultsId )
284
+ clickAndCheckTelemetry ( extrapolateSwitch as HTMLInputElement )
285
+ clickAndCheckTelemetry ( extrapolateSwitch as HTMLInputElement )
286
+ } )
287
+ } )
241
288
} )
0 commit comments