1- import { render , screen , fireEvent , waitForElementToBeRemoved } from '@testing-library/react' ;
1+ import { render , screen , fireEvent , waitForElementToBeRemoved , waitFor } from '@testing-library/react' ;
22import { Provider } from 'react-redux' ;
33
44import * as reducers from 'app/percona/shared/core/reducers' ;
@@ -10,8 +10,48 @@ import { Advanced } from './Advanced';
1010
1111jest . mock ( 'app/percona/settings/Settings.service' ) ;
1212
13+ const updateSettingsSpy = jest . spyOn ( reducers , 'updateSettingsAction' ) ;
14+
15+ const setup = ( pmmMonitoringEnabled = true ) =>
16+ render (
17+ < Provider
18+ store = { configureStore ( {
19+ percona : {
20+ user : { isAuthorized : true } ,
21+ settings : {
22+ loading : false ,
23+ result : {
24+ advisorRunIntervals : {
25+ rareInterval : '280800s' ,
26+ standardInterval : '86400s' ,
27+ frequentInterval : '14400s' ,
28+ } ,
29+ dataRetention : '2592000s' ,
30+ telemetryEnabled : true ,
31+ telemetrySummaries : [ 'summary1' , 'summary2' ] ,
32+ updatesEnabled : false ,
33+ backupEnabled : false ,
34+ advisorEnabled : true ,
35+ azureDiscoverEnabled : true ,
36+ publicAddress : 'localhost' ,
37+ alertingEnabled : true ,
38+ enableInternalPgQan : pmmMonitoringEnabled ,
39+ } ,
40+ } ,
41+ } ,
42+ navIndex : { } ,
43+ } as StoreState ) }
44+ >
45+ { wrapWithGrafanaContextMock ( < Advanced /> ) }
46+ </ Provider >
47+ ) ;
48+
1349describe ( 'Advanced::' , ( ) => {
14- it ( 'Renders correctly with props' , ( ) => {
50+ beforeEach ( ( ) => {
51+ updateSettingsSpy . mockClear ( ) ;
52+ } ) ;
53+
54+ it ( 'renders correctly with props' , async ( ) => {
1555 render (
1656 < Provider
1757 store = { configureStore ( {
@@ -37,18 +77,17 @@ describe('Advanced::', () => {
3777 } ,
3878 } ,
3979 } ,
40- } as StoreState ) }
80+ } as unknown as StoreState ) }
4181 >
4282 { wrapWithGrafanaContextMock ( < Advanced /> ) }
4383 </ Provider >
4484 ) ;
4585
46- expect ( screen . getByTestId ( 'retention-number-input' ) ) . toHaveValue ( 30 ) ;
47- expect ( screen . getByTestId ( 'publicAddress-text-input' ) ) . toHaveValue ( 'localhost' ) ;
86+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'retention-number-input' ) ) . toHaveValue ( 30 ) ) ;
87+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'publicAddress-text-input' ) ) . toHaveValue ( 'localhost' ) ) ;
4888 } ) ;
4989
50- it ( 'Calls apply changes' , async ( ) => {
51- const spy = jest . spyOn ( reducers , 'updateSettingsAction' ) ;
90+ it ( 'calls apply changes' , async ( ) => {
5291 render (
5392 < Provider
5493 store = { configureStore ( {
@@ -79,14 +118,16 @@ describe('Advanced::', () => {
79118 { wrapWithGrafanaContextMock ( < Advanced /> ) }
80119 </ Provider >
81120 ) ;
121+
82122 fireEvent . change ( screen . getByTestId ( 'retention-number-input' ) , { target : { value : 70 } } ) ;
83123 fireEvent . submit ( screen . getByTestId ( 'advanced-button' ) ) ;
124+
84125 await waitForElementToBeRemoved ( ( ) => screen . getByTestId ( 'Spinner' ) ) ;
85126
86- expect ( spy ) . toHaveBeenCalled ( ) ;
127+ expect ( updateSettingsSpy ) . toHaveBeenCalled ( ) ;
87128 } ) ;
88129
89- it ( 'Sets correct URL from browser' , async ( ) => {
130+ it ( 'sets correct URL from browser' , async ( ) => {
90131 const location = {
91132 ...window . location ,
92133 host : 'pmmtest.percona.com' ,
@@ -129,12 +170,11 @@ describe('Advanced::', () => {
129170 ) ;
130171
131172 fireEvent . click ( screen . getByTestId ( 'public-address-button' ) ) ;
132- expect ( screen . getByTestId ( 'publicAddress-text-input' ) ) . toHaveValue ( 'pmmtest.percona.com' ) ;
133- } ) ;
134173
135- it ( 'Does not include STT check intervals in the change request if STT checks are disabled' , async ( ) => {
136- const spy = jest . spyOn ( reducers , 'updateSettingsAction' ) ;
174+ await waitFor ( ( ) => expect ( screen . getByTestId ( 'publicAddress-text-input' ) ) . toHaveValue ( 'pmmtest.percona.com' ) ) ;
175+ } ) ;
137176
177+ it ( 'does not include STT check intervals in the change request if STT checks are disabled' , async ( ) => {
138178 render (
139179 < Provider
140180 store = { configureStore ( {
@@ -168,10 +208,10 @@ describe('Advanced::', () => {
168208
169209 fireEvent . change ( screen . getByTestId ( 'retention-number-input' ) , { target : { value : 70 } } ) ;
170210 fireEvent . submit ( screen . getByTestId ( 'advanced-button' ) ) ;
211+
171212 await waitForElementToBeRemoved ( ( ) => screen . getByTestId ( 'Spinner' ) ) ;
172213
173- // expect(spy.calls.mostRecent().args[0].body.stt_check_intervals).toBeUndefined();
174- expect ( spy ) . toHaveBeenLastCalledWith (
214+ expect ( updateSettingsSpy ) . toHaveBeenLastCalledWith (
175215 expect . objectContaining ( {
176216 body : expect . objectContaining ( {
177217 advisor_run_intervals : undefined ,
@@ -181,8 +221,6 @@ describe('Advanced::', () => {
181221 } ) ;
182222
183223 it ( 'Includes STT check intervals in the change request if STT checks are enabled' , async ( ) => {
184- const spy = jest . spyOn ( reducers , 'updateSettingsAction' ) ;
185-
186224 render (
187225 < Provider
188226 store = { configureStore ( {
@@ -220,7 +258,7 @@ describe('Advanced::', () => {
220258 await waitForElementToBeRemoved ( ( ) => screen . getByTestId ( 'Spinner' ) ) ;
221259
222260 // expect(spy.calls.mostRecent().args[0].body.stt_check_intervals).toBeDefined();
223- expect ( spy ) . toHaveBeenLastCalledWith (
261+ expect ( updateSettingsSpy ) . toHaveBeenLastCalledWith (
224262 expect . objectContaining ( {
225263 body : expect . objectContaining ( {
226264 advisor_run_intervals : {
@@ -232,4 +270,68 @@ describe('Advanced::', () => {
232270 } )
233271 ) ;
234272 } ) ;
273+
274+ it ( 'updates internal monitoring when pmm server monitoring is turned on' , async ( ) => {
275+ const { container } = setup ( ) ;
276+
277+ const monitoringSwitch = container . querySelector (
278+ '[data-testid="enable-internal-pg-qan"] [name="enableInternalPgQan"]'
279+ ) ;
280+
281+ expect ( monitoringSwitch ) . toBeInTheDocument ( ) ;
282+
283+ fireEvent . click ( monitoringSwitch ! ) ;
284+
285+ fireEvent . submit ( screen . getByTestId ( 'advanced-button' ) ) ;
286+
287+ await waitForElementToBeRemoved ( ( ) => screen . getByTestId ( 'Spinner' ) ) ;
288+
289+ expect ( updateSettingsSpy ) . toHaveBeenCalledWith (
290+ expect . objectContaining ( {
291+ body : expect . objectContaining ( {
292+ enable_internal_pg_qan : false ,
293+ } ) ,
294+ } )
295+ ) ;
296+ } ) ;
297+
298+ it ( 'updates internal monitoring when pmm server monitoring is turned off' , async ( ) => {
299+ const { container } = setup ( false ) ;
300+
301+ const monitoringSwitch = container . querySelector (
302+ '[data-testid="enable-internal-pg-qan"] [name="enableInternalPgQan"]'
303+ ) ;
304+
305+ expect ( monitoringSwitch ) . toBeInTheDocument ( ) ;
306+
307+ fireEvent . click ( monitoringSwitch ! ) ;
308+
309+ fireEvent . submit ( screen . getByTestId ( 'advanced-button' ) ) ;
310+
311+ await waitForElementToBeRemoved ( ( ) => screen . getByTestId ( 'Spinner' ) ) ;
312+
313+ expect ( updateSettingsSpy ) . toHaveBeenCalledWith (
314+ expect . objectContaining ( {
315+ body : expect . objectContaining ( {
316+ enable_internal_pg_qan : true ,
317+ } ) ,
318+ } )
319+ ) ;
320+ } ) ;
321+
322+ it ( "doesn't update internal monitoring when pmm server monitoring doesn't change" , async ( ) => {
323+ setup ( ) ;
324+
325+ fireEvent . submit ( screen . getByTestId ( 'advanced-button' ) ) ;
326+
327+ await waitForElementToBeRemoved ( ( ) => screen . getByTestId ( 'Spinner' ) ) ;
328+
329+ expect ( updateSettingsSpy ) . toHaveBeenCalledWith (
330+ expect . objectContaining ( {
331+ body : expect . objectContaining ( {
332+ enable_internal_pg_qan : true ,
333+ } ) ,
334+ } )
335+ ) ;
336+ } ) ;
235337} ) ;
0 commit comments