@@ -44,11 +44,14 @@ jest.mock('@superset-ui/core', () => ({
4444} ) ) ;
4545
4646// Mock the heavy dependencies that cause test issues
47- jest . mock ( './DeckGLContainer' , ( ) => ( {
48- DeckGLContainerStyledWrapper : jest . forwardRef ( ( props : any , ref : any ) => (
49- < div data-testid = "deck-gl-container" { ...props } ref = { ref } />
50- ) ) ,
51- } ) ) ;
47+ jest . mock ( './DeckGLContainer' , ( ) => {
48+ const React = require ( 'react' ) ;
49+ return {
50+ DeckGLContainerStyledWrapper : React . forwardRef ( ( props : any , ref : any ) => (
51+ React . createElement ( 'div' , { 'data-testid' : 'deck-gl-container' , ...props , ref } )
52+ ) ) ,
53+ } ;
54+ } ) ;
5255
5356jest . mock ( './utils/colors' , ( ) => ( {
5457 hexToRGB : jest . fn ( ( ) => [ 255 , 0 , 0 , 255 ] ) ,
@@ -143,147 +146,166 @@ const renderWithTheme = (component: React.ReactElement) => {
143146 ) ;
144147} ;
145148
146- describe ( 'CategoricalDeckGLContainer Integration Tests' , ( ) => {
147- describe ( 'Legend Integration ' , ( ) => {
148- test ( 'should render legend when configured correctly with categorical_palette ' , ( ) => {
149- const propsWithCategorical = {
149+ describe ( 'CategoricalDeckGLContainer Legend Tests' , ( ) => {
150+ describe ( 'Legend Visibility ' , ( ) => {
151+ test ( 'should show legend when dimension is set and position is not null ' , ( ) => {
152+ const props = {
150153 ...defaultProps ,
151154 formData : {
152155 ...mockFormData ,
156+ dimension : 'cat_color' ,
157+ legend_position : 'tr' ,
153158 color_scheme_type : COLOR_SCHEME_TYPES . categorical_palette ,
154159 } ,
155160 } ;
156161
157- renderWithTheme ( < CategoricalDeckGLContainer { ...propsWithCategorical } /> ) ;
158-
159- const legend = screen . getByTestId ( 'legend' ) ;
162+ const { container } = renderWithTheme ( < CategoricalDeckGLContainer { ...props } /> ) ;
163+
164+ // Check for legend using DOM query since getByTestId has issues in this test environment
165+ const legend = container . querySelector ( '[data-testid="legend"]' ) ;
160166 expect ( legend ) . toBeInTheDocument ( ) ;
161- expect ( screen . getByText ( 'Category A' ) ) . toBeInTheDocument ( ) ;
162- expect ( screen . getByText ( 'Category B' ) ) . toBeInTheDocument ( ) ;
163167 } ) ;
164168
165- test ( 'should not render legend with fixed_color' , ( ) => {
166- const propsWithFixed = {
169+ test ( 'should show legend even with fixed_color when dimension is set ' , ( ) => {
170+ const props = {
167171 ...defaultProps ,
168172 formData : {
169173 ...mockFormData ,
174+ dimension : 'cat_color' ,
175+ legend_position : 'bl' ,
170176 color_scheme_type : COLOR_SCHEME_TYPES . fixed_color ,
171177 } ,
172178 } ;
173179
174- renderWithTheme ( < CategoricalDeckGLContainer { ...propsWithFixed } /> ) ;
175-
176- expect ( screen . getByTestId ( 'deck-gl-container' ) ) . toBeInTheDocument ( ) ;
177- expect ( screen . queryByTestId ( ' legend' ) ) . not . toBeInTheDocument ( ) ;
180+ const { container } = renderWithTheme ( < CategoricalDeckGLContainer { ...props } /> ) ;
181+
182+ const legend = container . querySelector ( '[data-testid="legend"]' ) ;
183+ expect ( legend ) . toBeInTheDocument ( ) ;
178184 } ) ;
179185
180- test ( 'should render legend for undefined color_scheme_type' , ( ) => {
181- const propsWithUndefined = {
186+ test ( 'should show legend for undefined color_scheme_type (backward compatibility) ' , ( ) => {
187+ const props = {
182188 ...defaultProps ,
183189 formData : {
184190 ...mockFormData ,
191+ dimension : 'cat_color' ,
192+ legend_position : 'tl' ,
193+ // color_scheme_type: undefined
185194 } ,
186195 } ;
187196
188- renderWithTheme ( < CategoricalDeckGLContainer { ...propsWithUndefined } /> ) ;
189-
190- const legend = screen . getByTestId ( ' legend') ;
197+ const { container } = renderWithTheme ( < CategoricalDeckGLContainer { ...props } /> ) ;
198+
199+ const legend = container . querySelector ( '[data-testid=" legend"] ') ;
191200 expect ( legend ) . toBeInTheDocument ( ) ;
192- expect ( screen . getByText ( 'Category A' ) ) . toBeInTheDocument ( ) ;
193- expect ( screen . getByText ( 'Category B' ) ) . toBeInTheDocument ( ) ;
194201 } ) ;
195202
196- test ( 'should not render legend when legend_position is null' , ( ) => {
197- const propsWithNoPosition = {
203+ test ( 'should NOT show legend when legend_position is null' , ( ) => {
204+ const props = {
198205 ...defaultProps ,
199206 formData : {
200207 ...mockFormData ,
201- color_scheme_type : COLOR_SCHEME_TYPES . categorical_palette ,
208+ dimension : 'cat_color' ,
202209 legend_position : null ,
210+ color_scheme_type : COLOR_SCHEME_TYPES . categorical_palette ,
203211 } ,
204212 } ;
205213
206- renderWithTheme ( < CategoricalDeckGLContainer { ...propsWithNoPosition } /> ) ;
207-
208- expect ( screen . queryByTestId ( 'legend' ) ) . not . toBeInTheDocument ( ) ;
214+ const { container } = renderWithTheme ( < CategoricalDeckGLContainer { ...props } /> ) ;
215+
216+ const legend = container . querySelector ( '[data-testid="legend"]' ) ;
217+ expect ( legend ) . not . toBeInTheDocument ( ) ;
209218 } ) ;
210219
211- test ( 'should not render legend when no dimension is set' , ( ) => {
212- const propsWithNoDimension = {
220+ test ( 'should show legend even when dimension is not explicitly set but data has categories ' , ( ) => {
221+ const props = {
213222 ...defaultProps ,
214223 formData : {
215224 ...mockFormData ,
216- color_scheme_type : COLOR_SCHEME_TYPES . categorical_palette ,
217225 dimension : undefined ,
226+ legend_position : 'tr' ,
227+ color_scheme_type : COLOR_SCHEME_TYPES . categorical_palette ,
218228 } ,
219229 } ;
220230
221- renderWithTheme ( < CategoricalDeckGLContainer { ...propsWithNoDimension } /> ) ;
222-
223- expect ( screen . queryByTestId ( 'legend' ) ) . not . toBeInTheDocument ( ) ;
231+ const { container } = renderWithTheme ( < CategoricalDeckGLContainer { ...props } /> ) ;
232+
233+ // With our fixes, legend shows when there's categorical data available
234+ const legend = container . querySelector ( '[data-testid="legend"]' ) ;
235+ expect ( legend ) . toBeInTheDocument ( ) ;
224236 } ) ;
225- } ) ;
226237
227- describe ( 'Data Integration Tests' , ( ) => {
228- test ( 'should handle empty data gracefully' , ( ) => {
229- const propsWithEmptyData = {
238+ test ( 'should NOT show legend when data is empty' , ( ) => {
239+ const props = {
230240 ...defaultProps ,
231- payload : {
232- ...mockPayload ,
233- data : { features : [ ] } ,
234- } ,
235241 formData : {
236242 ...mockFormData ,
243+ dimension : 'cat_color' ,
244+ legend_position : 'tr' ,
237245 color_scheme_type : COLOR_SCHEME_TYPES . categorical_palette ,
238246 } ,
247+ payload : {
248+ ...mockPayload ,
249+ data : { features : [ ] } ,
250+ } ,
239251 } ;
240252
241- renderWithTheme ( < CategoricalDeckGLContainer { ...propsWithEmptyData } /> ) ;
242-
243- expect ( screen . getByTestId ( 'deck-gl-container' ) ) . toBeInTheDocument ( ) ;
244- expect ( screen . queryByTestId ( ' legend' ) ) . not . toBeInTheDocument ( ) ;
253+ const { container } = renderWithTheme ( < CategoricalDeckGLContainer { ...props } /> ) ;
254+
255+ const legend = container . querySelector ( '[data-testid="legend"]' ) ;
256+ expect ( legend ) . not . toBeInTheDocument ( ) ;
245257 } ) ;
258+ } ) ;
246259
247- test ( 'should handle data updates correctly' , ( ) => {
248- const { rerender } = renderWithTheme (
249- < CategoricalDeckGLContainer { ...defaultProps } /> ,
250- ) ;
251-
252- const updatedPayload = {
253- ...mockPayload ,
254- data : {
255- features : [
256- ...mockPayload . data . features ,
257- {
258- cat_color : 'Category C' ,
259- metric : 300 ,
260- source_latitude : 37.7749 ,
261- source_longitude : - 122.4194 ,
262- target_latitude : 47.6062 ,
263- target_longitude : - 122.3321 ,
264- } ,
265- ] ,
266- } ,
267- } ;
260+ describe ( 'Legend Positioning' , ( ) => {
261+ const positions = [
262+ { position : 'tl' , description : 'top-left' } ,
263+ { position : 'tr' , description : 'top-right' } ,
264+ { position : 'bl' , description : 'bottom-left' } ,
265+ { position : 'br' , description : 'bottom-right' } ,
266+ ] ;
267+
268+ positions . forEach ( ( { position, description } ) => {
269+ test ( `should render legend in ${ description } when position is ${ position } ` , ( ) => {
270+ const props = {
271+ ...defaultProps ,
272+ formData : {
273+ ...mockFormData ,
274+ dimension : 'cat_color' ,
275+ legend_position : position ,
276+ color_scheme_type : COLOR_SCHEME_TYPES . categorical_palette ,
277+ } ,
278+ } ;
279+
280+ const { container } = renderWithTheme ( < CategoricalDeckGLContainer { ...props } /> ) ;
281+
282+ const legend = container . querySelector ( '[data-testid="legend"]' ) ;
283+ expect ( legend ) . toBeInTheDocument ( ) ;
284+
285+ // The Legend component receives the position prop correctly
286+ // We can't easily test CSS positioning in JSDOM, but we can verify
287+ // the legend renders when position is set
288+ } ) ;
289+ } ) ;
290+ } ) ;
268291
269- const propsWithNewData = {
292+ describe ( 'Legend Content' , ( ) => {
293+ test ( 'should show category labels in legend' , ( ) => {
294+ const props = {
270295 ...defaultProps ,
271- payload : updatedPayload ,
272296 formData : {
273297 ...mockFormData ,
298+ dimension : 'cat_color' ,
299+ legend_position : 'tr' ,
274300 color_scheme_type : COLOR_SCHEME_TYPES . categorical_palette ,
275301 } ,
276302 } ;
277303
278- rerender (
279- < ThemeProvider theme = { supersetTheme } >
280- < CategoricalDeckGLContainer { ...propsWithNewData } />
281- </ ThemeProvider > ,
282- ) ;
283-
284- expect ( screen . getByText ( 'Category A' ) ) . toBeInTheDocument ( ) ;
285- expect ( screen . getByText ( 'Category B' ) ) . toBeInTheDocument ( ) ;
286- expect ( screen . getByText ( 'Category C' ) ) . toBeInTheDocument ( ) ;
304+ const { container } = renderWithTheme ( < CategoricalDeckGLContainer { ...props } /> ) ;
305+
306+ // Check that category text is present in the DOM
307+ expect ( container . textContent ) . toContain ( 'Category A' ) ;
308+ expect ( container . textContent ) . toContain ( 'Category B' ) ;
287309 } ) ;
288310 } ) ;
289311} ) ;
0 commit comments