@@ -3,73 +3,102 @@ import { renderWithProviders } from '../../../../utilities/test-utilities/mockSt
33import { mockBKPRAccountEvents , mockAppStore } from '../../../../utilities/test-utilities/mockData' ;
44import AccountEventsGraph from './AccountEventsGraph' ;
55
6+ // Mock ResizeObserver which Recharts uses
7+ global . ResizeObserver = jest . fn ( ) . mockImplementation ( ( ) => ( {
8+ observe : jest . fn ( ) ,
9+ unobserve : jest . fn ( ) ,
10+ disconnect : jest . fn ( ) ,
11+ } ) ) ;
12+
13+ jest . mock ( 'recharts' , ( ) => ( {
14+ ResponsiveContainer : ( { children } : any ) => < div className = "recharts-wrapper" > { children } </ div > ,
15+ BarChart : ( { children, data } : any ) => (
16+ < div data-testid = "bar-chart" data-item-count = { data ?. length } >
17+ { children }
18+ </ div >
19+ ) ,
20+ Bar : ( { dataKey } : any ) => < div data-testid = { `bar-${ dataKey } ` } /> ,
21+ XAxis : ( ) => < div data-testid = "x-axis" /> ,
22+ YAxis : ( ) => < div data-testid = "y-axis" /> ,
23+ CartesianGrid : ( ) => < div data-testid = "cartesian-grid" /> ,
24+ Tooltip : ( ) => < div data-testid = "tooltip" /> ,
25+ Legend : ( ) => < div data-testid = "legend" /> ,
26+ } ) ) ;
27+
628describe ( 'Account Events Graph component' , ( ) => {
7- beforeEach ( ( ) => {
8- class ResizeObserverMock {
9- private callback : ResizeObserverCallback ;
10- constructor ( callback : ResizeObserverCallback ) {
11- this . callback = callback ;
29+ it ( 'should render the graph container' , async ( ) => {
30+ await renderWithProviders (
31+ < AccountEventsGraph periods = { mockBKPRAccountEvents . periods } /> ,
32+ {
33+ preloadedState : mockAppStore ,
34+ initialRoute : [ '/bookkeeper/accountevents' ] ,
35+ useRouter : false ,
1236 }
13- observe = ( target : Element ) => {
14- // Simulate dimensions
15- this . callback (
16- [
17- {
18- target,
19- contentRect : {
20- width : 500 ,
21- height : 400 ,
22- top : 0 ,
23- left : 0 ,
24- bottom : 400 ,
25- right : 500 ,
26- x : 0 ,
27- y : 0 ,
28- toJSON : ( ) => { } ,
29- } ,
30- } as ResizeObserverEntry ,
31- ] ,
32- this
33- ) ;
34- } ;
35- unobserve = jest . fn ( ) ;
36- disconnect = jest . fn ( ) ;
37- }
38- global . ResizeObserver = ResizeObserverMock as unknown as typeof ResizeObserver ;
39- Object . defineProperty ( HTMLElement . prototype , 'clientWidth' , { configurable : true , value : 500 } ) ;
40- Object . defineProperty ( HTMLElement . prototype , 'clientHeight' , { configurable : true , value : 400 } ) ;
37+ ) ;
38+
39+ expect ( screen . getByTestId ( 'account-events-graph' ) ) . toBeInTheDocument ( ) ;
4140 } ) ;
4241
43- it ( 'should render the graph container ' , async ( ) => {
42+ it ( 'should render the chart components ' , async ( ) => {
4443 await renderWithProviders (
4544 < AccountEventsGraph periods = { mockBKPRAccountEvents . periods } /> ,
46- { preloadedState : mockAppStore , initialRoute : [ '/bookkeeper/accountevents' ] }
45+ {
46+ preloadedState : mockAppStore ,
47+ initialRoute : [ '/bookkeeper/accountevents' ] ,
48+ useRouter : false ,
49+ }
4750 ) ;
51+
52+ // Verify the mocked Recharts components are rendered
4853 expect ( screen . getByTestId ( 'account-events-graph' ) ) . toBeInTheDocument ( ) ;
54+ expect ( screen . getByTestId ( 'bar-chart' ) ) . toBeInTheDocument ( ) ;
55+ expect ( screen . getByTestId ( 'x-axis' ) ) . toBeInTheDocument ( ) ;
56+ expect ( screen . getByTestId ( 'y-axis' ) ) . toBeInTheDocument ( ) ;
57+ expect ( screen . getByTestId ( 'cartesian-grid' ) ) . toBeInTheDocument ( ) ;
58+ expect ( screen . getByTestId ( 'tooltip' ) ) . toBeInTheDocument ( ) ;
59+ expect ( screen . getByTestId ( 'legend' ) ) . toBeInTheDocument ( ) ;
4960 } ) ;
5061
51- it ( 'should render the correct number of bars based on account names' , async ( ) => {
52- await renderWithProviders ( < AccountEventsGraph periods = { mockBKPRAccountEvents . periods } /> ,
53- { preloadedState : mockAppStore , initialRoute : [ '/bookkeeper/accountevents' ] }
62+ it ( 'should render bars for each account' , async ( ) => {
63+ await renderWithProviders (
64+ < AccountEventsGraph periods = { mockBKPRAccountEvents . periods } /> ,
65+ {
66+ preloadedState : mockAppStore ,
67+ initialRoute : [ '/bookkeeper/accountevents' ] ,
68+ useRouter : false ,
69+ }
70+ ) ;
71+
72+ // Get unique account names from mock data to verify bars are created
73+ const uniqueAccounts = Array . from (
74+ new Set (
75+ mockBKPRAccountEvents . periods . flatMap ( period =>
76+ period . accounts . map ( account => account . account )
77+ )
78+ )
5479 ) ;
55- const svg = screen . getByTestId ( 'account-events-graph' ) . querySelector ( 'svg' ) ;
56- const bars = svg ?. querySelectorAll ( 'rect' ) ;
57- expect ( bars ?. length ) . toBeGreaterThan ( 0 ) ;
80+
81+ // Verify a bar is rendered for each unique account
82+ uniqueAccounts . forEach ( accountName => {
83+ expect ( screen . getByTestId ( `bar-${ accountName } ` ) ) . toBeInTheDocument ( ) ;
84+ } ) ;
5885 } ) ;
5986
60- it ( 'should render XAxis and YAxis labels formatted correctly ' , async ( ) => {
87+ it ( 'should pass correct data to BarChart ' , async ( ) => {
6188 await renderWithProviders (
6289 < AccountEventsGraph periods = { mockBKPRAccountEvents . periods } /> ,
63- { preloadedState : mockAppStore , initialRoute : [ '/bookkeeper/accountevents' ] }
90+ {
91+ preloadedState : mockAppStore ,
92+ initialRoute : [ '/bookkeeper/accountevents' ] ,
93+ useRouter : false ,
94+ }
6495 ) ;
65- const rechartsWrapper = screen . getByTestId ( 'account-events-graph' ) ;
66- const svg = rechartsWrapper . querySelector ( 'svg' ) ;
67- const ticksGroup = svg ?. querySelector ( 'g.recharts-cartesian-axis-ticks' ) ;
68- expect ( ticksGroup ) . toBeInTheDocument ( ) ;
69- const tickTexts = ticksGroup ?. querySelectorAll ( 'text' ) ;
70- expect ( tickTexts ?. length ) . toBeGreaterThan ( 0 ) ;
71- const tickValues = Array . from ( tickTexts || [ ] ) . map ( tick => tick . textContent ) ;
72- expect ( tickValues ) . toContain ( mockBKPRAccountEvents . periods [ 0 ] . period_key ) ;
73- } ) ;
7496
97+ const barChart = screen . getByTestId ( 'bar-chart' ) ;
98+ expect ( barChart ) . toBeInTheDocument ( ) ;
99+
100+ // Verify data is passed (number of periods)
101+ const itemCount = barChart . getAttribute ( 'data-item-count' ) ;
102+ expect ( itemCount ) . toBe ( String ( mockBKPRAccountEvents . periods . length ) ) ;
103+ } ) ;
75104} ) ;
0 commit comments