|
| 1 | +import * as React from 'react'; |
| 2 | +import { RefObject } from '@mui/x-internals/types'; |
| 3 | +import { act, createRenderer, screen } from '@mui/internal-test-utils'; |
| 4 | +import { spy, stub } from 'sinon'; |
| 5 | +import { expect } from 'chai'; |
| 6 | +import { |
| 7 | + DataGridPremium, |
| 8 | + DataGridPremiumProps, |
| 9 | + GridApi, |
| 10 | + GridRowsProp, |
| 11 | + Toolbar, |
| 12 | + useGridApiRef, |
| 13 | +} from '@mui/x-data-grid-premium'; |
| 14 | +import { describeSkipIf, isJSDOM } from 'test/utils/skipIf'; |
| 15 | +import { |
| 16 | + GridToolbarPromptControl, |
| 17 | + GridToolbarPromptControlProps, |
| 18 | +} from '../components/promptControl/GridToolbarPromptControl'; |
| 19 | + |
| 20 | +interface BaselineProps extends DataGridPremiumProps { |
| 21 | + rows: GridRowsProp; |
| 22 | +} |
| 23 | + |
| 24 | +const rows: GridRowsProp = [ |
| 25 | + { id: 0, category1: 'CatA', category2: 'Cat1' }, |
| 26 | + { id: 1, category1: 'CatA', category2: 'Cat2' }, |
| 27 | + { id: 2, category1: 'CatA', category2: 'Cat2' }, |
| 28 | + { id: 3, category1: 'CatB', category2: 'Cat2' }, |
| 29 | + { id: 4, category1: 'CatB', category2: 'Cat1' }, |
| 30 | +]; |
| 31 | + |
| 32 | +const baselineProps: BaselineProps = { |
| 33 | + rows, |
| 34 | + columns: [ |
| 35 | + { |
| 36 | + field: 'id', |
| 37 | + type: 'number', |
| 38 | + unstable_examples: [10, 20, 30, 40, 50], |
| 39 | + }, |
| 40 | + { |
| 41 | + field: 'category1', |
| 42 | + unstable_examples: ['ExampleA', 'ExampleB', 'ExampleC'], |
| 43 | + }, |
| 44 | + { |
| 45 | + field: 'category2', |
| 46 | + unstable_examples: ['Example1', 'Example2', 'Example3'], |
| 47 | + }, |
| 48 | + ], |
| 49 | +}; |
| 50 | + |
| 51 | +describe('<DataGridPremium /> - Prompt', () => { |
| 52 | + const { render } = createRenderer(); |
| 53 | + |
| 54 | + let apiRef: RefObject<GridApi | null>; |
| 55 | + const promptSpy = stub().resolves({}); |
| 56 | + |
| 57 | + function ToolbarWithPromptInput(props: GridToolbarPromptControlProps) { |
| 58 | + return ( |
| 59 | + <Toolbar> |
| 60 | + <GridToolbarPromptControl {...props} onPrompt={promptSpy} /> |
| 61 | + </Toolbar> |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + function Test(props: Partial<DataGridPremiumProps & { allowDataSampling: boolean }>) { |
| 66 | + apiRef = useGridApiRef(); |
| 67 | + |
| 68 | + return ( |
| 69 | + <div style={{ width: 300, height: 300 }}> |
| 70 | + <DataGridPremium |
| 71 | + {...baselineProps} |
| 72 | + apiRef={apiRef} |
| 73 | + slots={{ |
| 74 | + toolbar: ToolbarWithPromptInput as any, |
| 75 | + }} |
| 76 | + slotProps={{ |
| 77 | + toolbar: { |
| 78 | + allowDataSampling: props.allowDataSampling, |
| 79 | + } as any, |
| 80 | + }} |
| 81 | + showToolbar |
| 82 | + {...props} |
| 83 | + /> |
| 84 | + </div> |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + beforeEach(() => { |
| 89 | + promptSpy.resetHistory(); |
| 90 | + }); |
| 91 | + |
| 92 | + describeSkipIf(isJSDOM)('data sampling', () => { |
| 93 | + it('should use unstable_examples to generate the prompt context', async () => { |
| 94 | + const { user } = render(<Test />); |
| 95 | + |
| 96 | + const input = screen.getByRole('textbox'); |
| 97 | + await user.type(input, 'Do something with the data'); |
| 98 | + |
| 99 | + const sendButton = screen.getByRole('button', { name: /send/i }); |
| 100 | + await user.click(sendButton); |
| 101 | + |
| 102 | + expect(promptSpy.callCount).to.equal(1); |
| 103 | + expect(promptSpy.firstCall.args[0]).contains('Example1'); |
| 104 | + expect(promptSpy.firstCall.args[0]).not.contains('CatA'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should sample rows to generate the prompt context', async () => { |
| 108 | + const { user } = render(<Test allowDataSampling />); |
| 109 | + |
| 110 | + const input = screen.getByRole('textbox'); |
| 111 | + await user.type(input, 'Do something with the data'); |
| 112 | + |
| 113 | + const sendButton = screen.getByRole('button', { name: /send/i }); |
| 114 | + await user.click(sendButton); |
| 115 | + |
| 116 | + expect(promptSpy.callCount).to.equal(1); |
| 117 | + expect(promptSpy.firstCall.args[0]).not.contains('Example1'); |
| 118 | + expect(promptSpy.firstCall.args[0]).contains('CatA'); |
| 119 | + }); |
| 120 | + }); |
| 121 | + |
| 122 | + describe('API', () => { |
| 123 | + it('should allow building the context', () => { |
| 124 | + render(<Test />); |
| 125 | + |
| 126 | + const contextWithColumnExamples = apiRef.current?.unstable_getPromptContext(); |
| 127 | + expect(contextWithColumnExamples).contains('Example'); |
| 128 | + expect(contextWithColumnExamples).not.contains('Cat'); |
| 129 | + |
| 130 | + const contextWithDataSamples = apiRef.current?.unstable_getPromptContext(true); |
| 131 | + expect(contextWithDataSamples).not.contains('Example'); |
| 132 | + expect(contextWithDataSamples).contains('Cat'); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should apply the prompt result', () => { |
| 136 | + const sortChangeSpy = spy(); |
| 137 | + const filterChangeSpy = spy(); |
| 138 | + const aggregationChangeSpy = spy(); |
| 139 | + const rowSelectionChangeSpy = spy(); |
| 140 | + const rowGroupingChangeSpy = spy(); |
| 141 | + |
| 142 | + render( |
| 143 | + <Test |
| 144 | + onSortModelChange={sortChangeSpy} |
| 145 | + onFilterModelChange={filterChangeSpy} |
| 146 | + onAggregationModelChange={aggregationChangeSpy} |
| 147 | + onRowSelectionModelChange={rowSelectionChangeSpy} |
| 148 | + onRowGroupingModelChange={rowGroupingChangeSpy} |
| 149 | + />, |
| 150 | + ); |
| 151 | + |
| 152 | + act(() => |
| 153 | + apiRef.current?.unstable_applyPromptResult({ |
| 154 | + select: 1, |
| 155 | + filters: [ |
| 156 | + { |
| 157 | + column: 'id', |
| 158 | + operator: '>=', |
| 159 | + value: 0, |
| 160 | + }, |
| 161 | + ], |
| 162 | + aggregation: { |
| 163 | + id: 'size', |
| 164 | + }, |
| 165 | + sorting: [ |
| 166 | + { |
| 167 | + column: 'id', |
| 168 | + direction: 'desc', |
| 169 | + }, |
| 170 | + ], |
| 171 | + grouping: [ |
| 172 | + { |
| 173 | + column: 'category1', |
| 174 | + }, |
| 175 | + ], |
| 176 | + error: null, |
| 177 | + }), |
| 178 | + ); |
| 179 | + |
| 180 | + expect(sortChangeSpy.callCount).to.equal(1); |
| 181 | + expect(filterChangeSpy.callCount).to.equal(1); |
| 182 | + expect(aggregationChangeSpy.callCount).to.equal(1); |
| 183 | + expect(rowSelectionChangeSpy.callCount).to.equal(1); |
| 184 | + expect(rowGroupingChangeSpy.callCount).to.equal(1); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
0 commit comments