@@ -2,19 +2,38 @@ import React from 'react';
22import { render , screen , fireEvent } from '@testing-library/react' ;
33import { HttpHeadersConfigV2 } from './HttpHeadersConfigV2' ;
44import { selectors } from 'selectors' ;
5+ import { Protocol } from 'types/config' ;
6+ import { createTestProps } from './helpers' ;
57
68describe ( 'HttpHeadersConfigV2' , ( ) => {
79 const onHttpHeadersChange = jest . fn ( ) ;
8- const onForwardGrafanaHeadersChange = jest . fn ( ) ;
10+ const onOptionsChangeMock = jest . fn ( ) ;
911 let consoleSpy : jest . SpyInstance ;
1012
13+ const defaultProps = createTestProps ( {
14+ options : {
15+ jsonData : {
16+ host : '' ,
17+ secure : false ,
18+ protocol : Protocol . Native ,
19+ port : undefined ,
20+ pdcInjected : false ,
21+ } ,
22+ secureJsonData : { } ,
23+ secureJsonFields : { } ,
24+ } ,
25+ mocks : {
26+ onOptionsChange : onOptionsChangeMock ,
27+ } ,
28+ } ) ;
29+
1130 const renderWith = ( overrides ?: Partial < React . ComponentProps < typeof HttpHeadersConfigV2 > > ) => {
1231 const props : React . ComponentProps < typeof HttpHeadersConfigV2 > = {
32+ ...defaultProps ,
1333 headers : [ ] ,
1434 forwardGrafanaHeaders : false ,
1535 secureFields : { } ,
1636 onHttpHeadersChange,
17- onForwardGrafanaHeadersChange,
1837 ...( overrides || { } ) ,
1938 } ;
2039 return render ( < HttpHeadersConfigV2 { ...props } /> ) ;
@@ -89,6 +108,123 @@ describe('HttpHeadersConfigV2', () => {
89108 const forwardCb = screen . getByLabelText ( / f o r w a r d g r a f a n a h t t p h e a d e r s t o d a t a s o u r c e / i) as HTMLInputElement ;
90109 fireEvent . click ( forwardCb ) ;
91110
92- expect ( onForwardGrafanaHeadersChange ) . toHaveBeenCalledWith ( true ) ;
111+ expect ( onOptionsChangeMock ) . toHaveBeenCalled ( ) ;
112+ expect ( onOptionsChangeMock ) . toHaveBeenLastCalledWith (
113+ expect . objectContaining ( {
114+ jsonData : expect . objectContaining ( { forwardGrafanaHeaders : true } ) ,
115+ } )
116+ ) ;
117+ } ) ;
118+
119+ describe ( 'HttpHeadersConfigV2' , ( ) => {
120+ const onHttpHeadersChange = jest . fn ( ) ;
121+ const onOptionsChangeMock = jest . fn ( ) ;
122+ let consoleSpy : jest . SpyInstance ;
123+
124+ const defaultProps = createTestProps ( {
125+ options : {
126+ jsonData : {
127+ host : '' ,
128+ secure : false ,
129+ protocol : Protocol . Native ,
130+ port : undefined ,
131+ pdcInjected : false ,
132+ } ,
133+ secureJsonData : { } ,
134+ secureJsonFields : { } ,
135+ } ,
136+ mocks : {
137+ onOptionsChange : onOptionsChangeMock ,
138+ } ,
139+ } ) ;
140+
141+ const renderWith = ( overrides ?: Partial < React . ComponentProps < typeof HttpHeadersConfigV2 > > ) => {
142+ const props : React . ComponentProps < typeof HttpHeadersConfigV2 > = {
143+ ...defaultProps ,
144+ headers : [ ] ,
145+ forwardGrafanaHeaders : false ,
146+ secureFields : { } ,
147+ onHttpHeadersChange,
148+ ...( overrides || { } ) ,
149+ } ;
150+ return render ( < HttpHeadersConfigV2 { ...props } /> ) ;
151+ } ;
152+
153+ beforeEach ( ( ) => {
154+ // Mock console.error to suppress React act() warnings
155+ consoleSpy = jest . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
156+ jest . clearAllMocks ( ) ;
157+ } ) ;
158+
159+ afterEach ( ( ) => {
160+ consoleSpy . mockRestore ( ) ;
161+ } ) ;
162+
163+ it ( 'renders top label, Add header button, and forward checkbox' , ( ) => {
164+ renderWith ( ) ;
165+
166+ expect ( screen . getByText ( / c u s t o m h t t p h e a d e r s / i) ) . toBeInTheDocument ( ) ;
167+ expect ( screen . getByRole ( 'button' , { name : / a d d h e a d e r / i } ) ) . toBeInTheDocument ( ) ;
168+ expect ( screen . getByLabelText ( / f o r w a r d g r a f a n a h t t p h e a d e r s t o d a t a s o u r c e / i) ) . toBeInTheDocument ( ) ;
169+ } ) ;
170+
171+ it ( 'adds a new header editor when Add header is clicked' , ( ) => {
172+ renderWith ( ) ;
173+
174+ const before = screen . queryAllByTestId ( selectors . components . Config . HttpHeaderConfig . headerEditor ) . length ;
175+ fireEvent . click ( screen . getByTestId ( selectors . components . Config . HttpHeaderConfig . addHeaderButton ) ) ;
176+ const after = screen . getAllByTestId ( selectors . components . Config . HttpHeaderConfig . headerEditor ) . length ;
177+
178+ expect ( after ) . toBe ( before + 1 ) ;
179+ expect ( onHttpHeadersChange ) . not . toHaveBeenCalled ( ) ;
180+ } ) ;
181+
182+ it ( 'renders any initial headers passed in' , ( ) => {
183+ renderWith ( {
184+ headers : [
185+ { name : 'X-Auth' , value : 'abc' , secure : false } ,
186+ { name : 'Foo' , value : 'bar' , secure : true } ,
187+ ] ,
188+ } ) ;
189+
190+ const editors = screen . getAllByTestId ( selectors . components . Config . HttpHeaderConfig . headerEditor ) ;
191+ expect ( editors . length ) . toBe ( 2 ) ;
192+ expect ( screen . getAllByTestId ( selectors . components . Config . HttpHeaderConfig . headerNameInput ) [ 0 ] ) . toHaveValue (
193+ 'X-Auth'
194+ ) ;
195+ expect ( screen . getAllByTestId ( selectors . components . Config . HttpHeaderConfig . headerNameInput ) [ 1 ] ) . toHaveValue ( 'Foo' ) ;
196+ } ) ;
197+
198+ it ( 'removes a header and calls onHttpHeadersChange when Remove is clicked' , ( ) => {
199+ renderWith ( {
200+ headers : [
201+ { name : 'A' , value : '1' , secure : false } ,
202+ { name : 'B' , value : '2' , secure : false } ,
203+ ] ,
204+ } ) ;
205+
206+ const before = screen . getAllByTestId ( selectors . components . Config . HttpHeaderConfig . headerEditor ) . length ;
207+ const removeButtons = screen . getAllByTestId ( 'trash-alt' ) ;
208+ fireEvent . click ( removeButtons [ 0 ] ) ;
209+
210+ expect ( onHttpHeadersChange ) . toHaveBeenCalled ( ) ;
211+ const next = onHttpHeadersChange . mock . lastCall ?. [ 0 ] ;
212+ expect ( next . length ) . toBe ( before - 1 ) ;
213+ expect ( next . find ( ( h : any ) => h . name === 'A' ) ) . toBeUndefined ( ) ;
214+ } ) ;
215+
216+ it ( 'toggles "Forward Grafana headers" and updated forwardGrafanaHeaders value to true' , ( ) => {
217+ renderWith ( { forwardGrafanaHeaders : false } ) ;
218+
219+ const forwardCb = screen . getByLabelText ( / f o r w a r d g r a f a n a h t t p h e a d e r s t o d a t a s o u r c e / i) as HTMLInputElement ;
220+ fireEvent . click ( forwardCb ) ;
221+
222+ expect ( onOptionsChangeMock ) . toHaveBeenCalled ( ) ;
223+ expect ( onOptionsChangeMock ) . toHaveBeenLastCalledWith (
224+ expect . objectContaining ( {
225+ jsonData : expect . objectContaining ( { forwardGrafanaHeaders : true } ) ,
226+ } )
227+ ) ;
228+ } ) ;
93229 } ) ;
94230} ) ;
0 commit comments