|
| 1 | +/** |
| 2 | + * Tests for ObjectChart data fetching & fault tolerance. |
| 3 | + * |
| 4 | + * Verifies that ObjectChart: |
| 5 | + * - Calls dataSource.find() when objectName is set and no bound data |
| 6 | + * - Handles missing/invalid dataSource gracefully |
| 7 | + * - Works without a SchemaRendererProvider |
| 8 | + */ |
| 9 | + |
| 10 | +import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; |
| 11 | +import { render, waitFor } from '@testing-library/react'; |
| 12 | +import React from 'react'; |
| 13 | +import { SchemaRendererProvider } from '@object-ui/react'; |
| 14 | +import { ObjectChart } from '../ObjectChart'; |
| 15 | + |
| 16 | +// Suppress console.error from React error boundary / fetch errors |
| 17 | +const originalConsoleError = console.error; |
| 18 | +beforeEach(() => { |
| 19 | + console.error = vi.fn(); |
| 20 | +}); |
| 21 | +afterEach(() => { |
| 22 | + console.error = originalConsoleError; |
| 23 | +}); |
| 24 | + |
| 25 | +describe('ObjectChart data fetching', () => { |
| 26 | + it('should call dataSource.find when objectName is set and no bind path', async () => { |
| 27 | + const mockFind = vi.fn().mockResolvedValue([ |
| 28 | + { stage: 'Prospect', amount: 100 }, |
| 29 | + { stage: 'Proposal', amount: 200 }, |
| 30 | + ]); |
| 31 | + const dataSource = { find: mockFind }; |
| 32 | + |
| 33 | + render( |
| 34 | + <SchemaRendererProvider dataSource={dataSource}> |
| 35 | + <ObjectChart |
| 36 | + schema={{ |
| 37 | + type: 'object-chart', |
| 38 | + objectName: 'opportunity', |
| 39 | + chartType: 'bar', |
| 40 | + xAxisKey: 'stage', |
| 41 | + series: [{ dataKey: 'amount' }], |
| 42 | + }} |
| 43 | + /> |
| 44 | + </SchemaRendererProvider> |
| 45 | + ); |
| 46 | + |
| 47 | + await waitFor(() => { |
| 48 | + expect(mockFind).toHaveBeenCalledWith('opportunity', { $filter: undefined }); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should NOT call dataSource.find when schema.data is provided', () => { |
| 53 | + const mockFind = vi.fn(); |
| 54 | + const dataSource = { find: mockFind }; |
| 55 | + |
| 56 | + render( |
| 57 | + <SchemaRendererProvider dataSource={dataSource}> |
| 58 | + <ObjectChart |
| 59 | + schema={{ |
| 60 | + type: 'object-chart', |
| 61 | + objectName: 'opportunity', |
| 62 | + chartType: 'bar', |
| 63 | + data: [{ stage: 'A', amount: 100 }], |
| 64 | + xAxisKey: 'stage', |
| 65 | + series: [{ dataKey: 'amount' }], |
| 66 | + }} |
| 67 | + /> |
| 68 | + </SchemaRendererProvider> |
| 69 | + ); |
| 70 | + |
| 71 | + expect(mockFind).not.toHaveBeenCalled(); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should apply aggregation to fetched data', async () => { |
| 75 | + const mockFind = vi.fn().mockResolvedValue([ |
| 76 | + { stage: 'Prospect', amount: 100 }, |
| 77 | + { stage: 'Prospect', amount: 200 }, |
| 78 | + { stage: 'Proposal', amount: 300 }, |
| 79 | + ]); |
| 80 | + const dataSource = { find: mockFind }; |
| 81 | + |
| 82 | + const { container } = render( |
| 83 | + <SchemaRendererProvider dataSource={dataSource}> |
| 84 | + <ObjectChart |
| 85 | + schema={{ |
| 86 | + type: 'object-chart', |
| 87 | + objectName: 'opportunity', |
| 88 | + chartType: 'bar', |
| 89 | + xAxisKey: 'stage', |
| 90 | + series: [{ dataKey: 'amount' }], |
| 91 | + aggregate: { field: 'amount', function: 'sum', groupBy: 'stage' }, |
| 92 | + }} |
| 93 | + /> |
| 94 | + </SchemaRendererProvider> |
| 95 | + ); |
| 96 | + |
| 97 | + await waitFor(() => { |
| 98 | + expect(mockFind).toHaveBeenCalled(); |
| 99 | + }); |
| 100 | + }); |
| 101 | +}); |
| 102 | + |
| 103 | +describe('ObjectChart fault tolerance', () => { |
| 104 | + it('should not crash when dataSource has no find method', () => { |
| 105 | + const { container } = render( |
| 106 | + <SchemaRendererProvider dataSource={{}}> |
| 107 | + <ObjectChart |
| 108 | + schema={{ |
| 109 | + type: 'object-chart', |
| 110 | + objectName: 'opportunity', |
| 111 | + chartType: 'bar', |
| 112 | + xAxisKey: 'stage', |
| 113 | + series: [{ dataKey: 'amount' }], |
| 114 | + }} |
| 115 | + /> |
| 116 | + </SchemaRendererProvider> |
| 117 | + ); |
| 118 | + |
| 119 | + // Should render without crashing |
| 120 | + expect(container).toBeDefined(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should not crash when rendered outside SchemaRendererProvider', () => { |
| 124 | + const { container } = render( |
| 125 | + <ObjectChart |
| 126 | + schema={{ |
| 127 | + type: 'object-chart', |
| 128 | + chartType: 'bar', |
| 129 | + xAxisKey: 'stage', |
| 130 | + series: [{ dataKey: 'amount' }], |
| 131 | + }} |
| 132 | + /> |
| 133 | + ); |
| 134 | + |
| 135 | + // Should render without crashing |
| 136 | + expect(container).toBeDefined(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should show "No data source available" when no dataSource and objectName set', () => { |
| 140 | + const { container } = render( |
| 141 | + <ObjectChart |
| 142 | + schema={{ |
| 143 | + type: 'object-chart', |
| 144 | + objectName: 'opportunity', |
| 145 | + chartType: 'bar', |
| 146 | + xAxisKey: 'stage', |
| 147 | + series: [{ dataKey: 'amount' }], |
| 148 | + }} |
| 149 | + /> |
| 150 | + ); |
| 151 | + |
| 152 | + expect(container.textContent).toContain('No data source available'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('should use dataSource prop over context when both are present', async () => { |
| 156 | + const contextFind = vi.fn().mockResolvedValue([]); |
| 157 | + const propFind = vi.fn().mockResolvedValue([{ stage: 'A', amount: 1 }]); |
| 158 | + |
| 159 | + render( |
| 160 | + <SchemaRendererProvider dataSource={{ find: contextFind }}> |
| 161 | + <ObjectChart |
| 162 | + dataSource={{ find: propFind }} |
| 163 | + schema={{ |
| 164 | + type: 'object-chart', |
| 165 | + objectName: 'opportunity', |
| 166 | + chartType: 'bar', |
| 167 | + xAxisKey: 'stage', |
| 168 | + series: [{ dataKey: 'amount' }], |
| 169 | + }} |
| 170 | + /> |
| 171 | + </SchemaRendererProvider> |
| 172 | + ); |
| 173 | + |
| 174 | + await waitFor(() => { |
| 175 | + expect(propFind).toHaveBeenCalled(); |
| 176 | + }); |
| 177 | + expect(contextFind).not.toHaveBeenCalled(); |
| 178 | + }); |
| 179 | +}); |
0 commit comments