| 
									
										
										
										
											2020-09-25 19:40:03 +08:00
										 |  |  | import React from 'react'; | 
					
						
							|  |  |  | import { render, screen } from '@testing-library/react'; | 
					
						
							|  |  |  | import userEvent from '@testing-library/user-event'; | 
					
						
							|  |  |  | import { DataSourceApi } from '@grafana/data'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import { Props, QueryVariableEditorUnConnected } from './QueryVariableEditor'; | 
					
						
							|  |  |  | import { initialQueryVariableModelState } from './reducer'; | 
					
						
							|  |  |  | import { initialVariableEditorState } from '../editor/reducer'; | 
					
						
							|  |  |  | import { describe, expect } from '../../../../test/lib/common'; | 
					
						
							|  |  |  | import { NEW_VARIABLE_ID } from '../state/types'; | 
					
						
							| 
									
										
										
										
											2020-11-18 22:10:32 +08:00
										 |  |  | import { LegacyVariableQueryEditor } from '../editor/LegacyVariableQueryEditor'; | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  | import { mockDataSource } from 'app/features/alerting/unified/mocks'; | 
					
						
							|  |  |  | import { DataSourceType } from 'app/features/alerting/unified/utils/datasource'; | 
					
						
							| 
									
										
										
										
											2020-09-25 19:40:03 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | const setupTestContext = (options: Partial<Props>) => { | 
					
						
							|  |  |  |   const defaults: Props = { | 
					
						
							| 
									
										
										
										
											2021-04-27 11:57:25 +08:00
										 |  |  |     variable: { ...initialQueryVariableModelState }, | 
					
						
							| 
									
										
										
										
											2020-09-25 19:40:03 +08:00
										 |  |  |     initQueryVariableEditor: jest.fn(), | 
					
						
							|  |  |  |     changeQueryVariableDataSource: jest.fn(), | 
					
						
							|  |  |  |     changeQueryVariableQuery: jest.fn(), | 
					
						
							|  |  |  |     changeVariableMultiValue: jest.fn(), | 
					
						
							|  |  |  |     editor: { | 
					
						
							|  |  |  |       ...initialVariableEditorState, | 
					
						
							|  |  |  |       extended: { | 
					
						
							| 
									
										
										
										
											2020-11-18 22:10:32 +08:00
										 |  |  |         VariableQueryEditor: LegacyVariableQueryEditor, | 
					
						
							| 
									
										
										
										
											2020-09-25 19:40:03 +08:00
										 |  |  |         dataSource: ({} as unknown) as DataSourceApi, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     onPropChange: jest.fn(), | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const props: Props & Record<string, any> = { ...defaults, ...options }; | 
					
						
							|  |  |  |   const { rerender } = render(<QueryVariableEditorUnConnected {...props} />); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return { rerender, props }; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-30 01:57:24 +08:00
										 |  |  | const mockDS = mockDataSource({ | 
					
						
							|  |  |  |   name: 'CloudManager', | 
					
						
							|  |  |  |   type: DataSourceType.Alertmanager, | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | jest.mock('@grafana/runtime/src/services/dataSourceSrv', () => { | 
					
						
							|  |  |  |   return { | 
					
						
							|  |  |  |     getDataSourceSrv: () => ({ | 
					
						
							|  |  |  |       get: () => Promise.resolve(mockDS), | 
					
						
							|  |  |  |       getList: () => [mockDS], | 
					
						
							|  |  |  |       getInstanceSettings: () => mockDS, | 
					
						
							|  |  |  |     }), | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2020-12-04 21:24:55 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-25 19:40:03 +08:00
										 |  |  | describe('QueryVariableEditor', () => { | 
					
						
							|  |  |  |   describe('when the component is mounted', () => { | 
					
						
							|  |  |  |     it('then it should call initQueryVariableEditor', () => { | 
					
						
							|  |  |  |       const { props } = setupTestContext({}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(props.initQueryVariableEditor).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  |       expect(props.initQueryVariableEditor).toHaveBeenCalledWith({ type: 'query', id: NEW_VARIABLE_ID }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('when the user changes', () => { | 
					
						
							|  |  |  |     it.each`
 | 
					
						
							| 
									
										
										
										
											2021-04-27 11:57:25 +08:00
										 |  |  |       fieldName  | propName                      | expectedArgs | 
					
						
							|  |  |  |       ${'query'} | ${'changeQueryVariableQuery'} | ${[{ type: 'query', id: NEW_VARIABLE_ID }, 't', 't']} | 
					
						
							| 
									
										
										
										
											2021-04-28 20:40:01 +08:00
										 |  |  |       ${'regex'} | ${'onPropChange'}             | ${[{ propName: 'regex', propValue: 't', updateOptions: true }]} | 
					
						
							| 
									
										
										
										
											2020-09-25 19:40:03 +08:00
										 |  |  |     `(
 | 
					
						
							|  |  |  |       '$fieldName field and tabs away then $propName should be called with correct args', | 
					
						
							|  |  |  |       ({ fieldName, propName, expectedArgs }) => { | 
					
						
							|  |  |  |         const { props } = setupTestContext({}); | 
					
						
							|  |  |  |         const propUnderTest = props[propName]; | 
					
						
							|  |  |  |         const fieldAccessor = fieldAccessors[fieldName]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         userEvent.type(fieldAccessor(), 't'); | 
					
						
							|  |  |  |         userEvent.tab(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(propUnderTest).toHaveBeenCalledTimes(1); | 
					
						
							|  |  |  |         expect(propUnderTest).toHaveBeenCalledWith(...expectedArgs); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('when the user changes', () => { | 
					
						
							|  |  |  |     it.each`
 | 
					
						
							| 
									
										
										
										
											2021-04-27 11:57:25 +08:00
										 |  |  |       fieldName  | propName | 
					
						
							|  |  |  |       ${'query'} | ${'changeQueryVariableQuery'} | 
					
						
							|  |  |  |       ${'regex'} | ${'onPropChange'} | 
					
						
							| 
									
										
										
										
											2020-09-25 19:40:03 +08:00
										 |  |  |     `(
 | 
					
						
							|  |  |  |       '$fieldName field but reverts the change and tabs away then $propName should not be called', | 
					
						
							|  |  |  |       ({ fieldName, propName }) => { | 
					
						
							|  |  |  |         const { props } = setupTestContext({}); | 
					
						
							|  |  |  |         const propUnderTest = props[propName]; | 
					
						
							|  |  |  |         const fieldAccessor = fieldAccessors[fieldName]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         userEvent.type(fieldAccessor(), 't'); | 
					
						
							|  |  |  |         userEvent.type(fieldAccessor(), '{backspace}'); | 
					
						
							|  |  |  |         userEvent.tab(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(propUnderTest).not.toHaveBeenCalled(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     ); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const getQueryField = () => | 
					
						
							|  |  |  |   screen.getByRole('textbox', { name: /variable editor form default variable query editor textarea/i }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const getRegExField = () => screen.getByRole('textbox', { name: /variable editor form query regex field/i }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const fieldAccessors: Record<string, () => HTMLElement> = { | 
					
						
							|  |  |  |   query: getQueryField, | 
					
						
							|  |  |  |   regex: getRegExField, | 
					
						
							|  |  |  | }; |