11import React from 'react' ;
2- import { render , screen , waitFor } from '@testing-library/react' ;
2+ import { render , screen , waitFor , fireEvent } from '@testing-library/react' ;
33import { describe , it , expect , vi } from 'vitest' ;
44import { ObjectMap } from './ObjectMap' ;
55import { DataSource } from '@object-ui/types' ;
66
7+ // Mock react-map-gl/maplibre components to check they are receiving the right props/children
8+ // without relying on real WebGL canvas rendering.
9+ vi . mock ( 'react-map-gl/maplibre' , ( ) => ( {
10+ default : ( { children } : any ) => < div aria-label = "Map" > { children } </ div > ,
11+ Map : ( { children } : any ) => < div aria-label = "Map" > { children } </ div > ,
12+ NavigationControl : ( ) => < div data-testid = "nav-control" /> ,
13+ Marker : ( { children, longitude, latitude, onClick } : any ) => (
14+ < div
15+ data-testid = "map-marker"
16+ data-lat = { latitude }
17+ data-lng = { longitude }
18+ onClick = { onClick }
19+ >
20+ { children }
21+ </ div >
22+ ) ,
23+ Popup : ( { children } : any ) => < div data-testid = "map-popup" > { children } </ div > ,
24+ } ) ) ;
25+
726const mockData = [
827 { id : '1' , name : 'Loc 1' , latitude : 40 , longitude : - 74 } ,
928 { id : '2' , name : 'Loc 2' , latitude : 41 , longitude : - 75 } ,
@@ -34,10 +53,26 @@ describe('ObjectMap', () => {
3453 } ;
3554
3655 render ( < ObjectMap schema = { schema } /> ) ;
56+
57+ // Wait for loading to finish
58+ await waitFor ( ( ) => {
59+ expect ( screen . queryByText ( 'Loading map...' ) ) . toBeNull ( ) ;
60+ } ) ;
61+
62+ // Check if map is rendered
63+ expect ( screen . getByLabelText ( 'Map' ) ) . toBeDefined ( ) ;
64+
65+ // Check if markers are rendered using testid from our mock
3766 await waitFor ( ( ) => {
38- expect ( screen . getByText ( / L o c a t i o n s \( 2 \) / ) ) . toBeDefined ( ) ;
67+ const markers = screen . getAllByTestId ( 'map-marker' ) ;
68+ expect ( markers ) . toHaveLength ( 2 ) ;
69+ // Verify coordinates of first marker
70+ expect ( markers [ 0 ] ) . toHaveAttribute ( 'data-lat' , '40' ) ;
71+ expect ( markers [ 0 ] ) . toHaveAttribute ( 'data-lng' , '-74' ) ;
3972 } ) ;
40- expect ( screen . getByText ( 'Loc 1' ) ) . toBeDefined ( ) ;
73+
74+ // Check content inside marker (the emoji)
75+ expect ( screen . getAllByText ( '📍' ) ) . toHaveLength ( 2 ) ;
4176 } ) ;
4277
4378 it ( 'fetches data with object provider' , async ( ) => {
@@ -58,10 +93,18 @@ describe('ObjectMap', () => {
5893
5994 render ( < ObjectMap schema = { schema } dataSource = { mockDataSource } /> ) ;
6095
96+ // Wait for loading to finish
6197 await waitFor ( ( ) => {
62- expect ( mockDataSource . find ) . toHaveBeenCalled ( ) ;
98+ expect ( screen . queryByText ( 'Loading map...' ) ) . toBeNull ( ) ;
6399 } ) ;
64100
65- expect ( screen . getByText ( / L o c a t i o n s \( 2 \) / ) ) . toBeDefined ( ) ;
101+ // Verify dataSource.find was called
102+ expect ( mockDataSource . find ) . toHaveBeenCalledWith ( 'locations' , expect . anything ( ) ) ;
103+
104+ // Check markers
105+ await waitFor ( ( ) => {
106+ const markers = screen . getAllByTestId ( 'map-marker' ) ;
107+ expect ( markers ) . toHaveLength ( 2 ) ;
108+ } ) ;
66109 } ) ;
67110} ) ;
0 commit comments