diff --git a/packages/x-data-grid-premium/src/tests/dataSourceAggregation.DataGridPremium.test.tsx b/packages/x-data-grid-premium/src/tests/dataSourceAggregation.DataGridPremium.test.tsx index 9f6cf86325203..8defca0540cd1 100644 --- a/packages/x-data-grid-premium/src/tests/dataSourceAggregation.DataGridPremium.test.tsx +++ b/packages/x-data-grid-premium/src/tests/dataSourceAggregation.DataGridPremium.test.tsx @@ -105,9 +105,6 @@ describe(' - Data source aggregation', () => { it('should not show aggregation option in the column menu when no aggregation function is defined', async () => { const { user } = render(); - await waitFor(() => { - expect(getRowsSpy.callCount).to.be.greaterThan(0); - }); await user.click(within(getColumnHeaderCell(0)).getByLabelText('Menu')); expect(screen.queryByLabelText('Aggregation')).to.equal(null); }); @@ -120,9 +117,6 @@ describe(' - Data source aggregation', () => { }} />, ); - await waitFor(() => { - expect(getRowsSpy.callCount).to.be.greaterThan(0); - }); expect(getRowsSpy.args[0][0].aggregationModel).to.deep.equal({ id: 'size' }); }); diff --git a/packages/x-data-grid-pro/src/tests/dataSource.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/dataSource.DataGridPro.test.tsx index 2875a9683442a..a3c8b18dbea56 100644 --- a/packages/x-data-grid-pro/src/tests/dataSource.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/dataSource.DataGridPro.test.tsx @@ -3,28 +3,39 @@ import { useMockServer } from '@mui/x-data-grid-generator'; import { act, createRenderer, waitFor } from '@mui/internal-test-utils'; import { expect } from 'chai'; import { RefObject } from '@mui/x-internals/types'; -import useLazyRef from '@mui/utils/useLazyRef'; import { DataGridPro, DataGridProProps, GridApi, GridDataSource, - GridDataSourceCache, GridGetRowsParams, GridGetRowsResponse, useGridApiRef, } from '@mui/x-data-grid-pro'; -import { SinonStub, spy, stub } from 'sinon'; +import { spy } from 'sinon'; import { describeSkipIf, isJSDOM } from 'test/utils/skipIf'; import { getKeyDefault } from '../hooks/features/dataSource/cache'; -const cache = new Map(); +class TestCache { + private cache: Map; -const testCache: GridDataSourceCache = { - set: (key, value) => cache.set(getKeyDefault(key), value), - get: (key) => cache.get(getKeyDefault(key)), - clear: () => cache.clear(), -}; + constructor() { + this.cache = new Map(); + } + + set(key: GridGetRowsParams, value: GridGetRowsResponse) { + this.cache.set(getKeyDefault(key), value); + } + get(key: GridGetRowsParams) { + return this.cache.get(getKeyDefault(key)); + } + size() { + return this.cache.size; + } + clear() { + this.cache.clear(); + } +} const pageSizeOptions = [10, 20]; const serverOptions = { useCursorPagination: false, minDelay: 0, maxDelay: 0, verbose: false }; @@ -32,9 +43,9 @@ const serverOptions = { useCursorPagination: false, minDelay: 0, maxDelay: 0, ve // Needs layout describeSkipIf(isJSDOM)(' - Data source', () => { const { render } = createRenderer(); + const fetchRowsSpy = spy(); let apiRef: RefObject; - let fetchRowsSpy: SinonStub; let mockServer: ReturnType; function TestDataSource(props: Partial & { shouldRequestsFail?: boolean }) { @@ -45,18 +56,11 @@ describeSkipIf(isJSDOM)(' - Data source', () => { props.shouldRequestsFail ?? false, ); - // Reuse the same stub between rerenders to properly count the calls - fetchRowsSpy = useLazyRef(() => stub()).current; + const { fetchRows } = mockServer; - const originalFetchRows = mockServer.fetchRows; - const fetchRows = React.useMemo(() => { + const dataSource: GridDataSource = React.useMemo(() => { fetchRowsSpy.resetHistory(); - fetchRowsSpy.callsFake(originalFetchRows); - return (...args) => fetchRowsSpy(...args); - }, [originalFetchRows]); - - const dataSource: GridDataSource = React.useMemo( - () => ({ + return { getRows: async (params: GridGetRowsParams) => { const urlParams = new URLSearchParams({ filterModel: JSON.stringify(params.filterModel), @@ -65,18 +69,17 @@ describeSkipIf(isJSDOM)(' - Data source', () => { end: `${params.end}`, }); - const getRowsResponse = await fetchRows( - `https://mui.com/x/api/data-grid?${urlParams.toString()}`, - ); + const url = `https://mui.com/x/api/data-grid?${urlParams.toString()}`; + fetchRowsSpy(url); + const getRowsResponse = await fetchRows(url); return { rows: getRowsResponse.rows, rowCount: getRowsResponse.rowCount, }; }, - }), - [fetchRows], - ); + }; + }, [fetchRows]); return (
@@ -94,11 +97,6 @@ describeSkipIf(isJSDOM)(' - Data source', () => { ); } - // eslint-disable-next-line mocha/no-top-level-hooks - beforeEach(() => { - cache.clear(); - }); - it('should fetch the data on initial render', async () => { render(); await waitFor(() => { @@ -186,14 +184,16 @@ describeSkipIf(isJSDOM)(' - Data source', () => { }); it('should cache the data using the custom cache', async () => { + const testCache = new TestCache(); render(); await waitFor(() => { expect(fetchRowsSpy.callCount).to.equal(1); }); - expect(cache.size).to.equal(1); + expect(testCache.size()).to.equal(1); }); it('should cache the data in the chunks defined by the minimum page size', async () => { + const testCache = new TestCache(); render( - Data source', () => { await waitFor(() => { expect(fetchRowsSpy.callCount).to.equal(1); }); - expect(cache.size).to.equal(2); // 2 chunks of 10 rows + expect(testCache.size()).to.equal(2); // 2 chunks of 10 rows }); it('should use the cached data when the same query is made again', async () => { + const testCache = new TestCache(); const pageChangeSpy = spy(); render( - Data source', () => { await waitFor(() => { expect(fetchRowsSpy.callCount).to.equal(1); }); - expect(cache.size).to.equal(1); + expect(testCache.size()).to.equal(1); expect(pageChangeSpy.callCount).to.equal(0); act(() => { @@ -227,7 +228,9 @@ describeSkipIf(isJSDOM)(' - Data source', () => { await waitFor(() => { expect(fetchRowsSpy.callCount).to.equal(2); }); - expect(cache.size).to.equal(2); + await waitFor(() => { + expect(testCache.size()).to.equal(2); + }); expect(pageChangeSpy.callCount).to.equal(1); act(() => { @@ -237,7 +240,7 @@ describeSkipIf(isJSDOM)(' - Data source', () => { await waitFor(() => { expect(fetchRowsSpy.callCount).to.equal(2); }); - expect(cache.size).to.equal(2); + expect(testCache.size()).to.equal(2); expect(pageChangeSpy.callCount).to.equal(2); }); diff --git a/packages/x-data-grid-pro/src/tests/dataSourceLazyLoader.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/dataSourceLazyLoader.DataGridPro.test.tsx index 66c596d1643a9..4f0864c7fb28d 100644 --- a/packages/x-data-grid-pro/src/tests/dataSourceLazyLoader.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/dataSourceLazyLoader.DataGridPro.test.tsx @@ -13,18 +13,17 @@ import { GridGetRowsResponse, useGridApiRef, } from '@mui/x-data-grid-pro'; -import { SinonStub, stub } from 'sinon'; +import { spy } from 'sinon'; import { describeSkipIf, isJSDOM } from 'test/utils/skipIf'; -import useLazyRef from '@mui/utils/useLazyRef'; // Needs layout describeSkipIf(isJSDOM)(' - Data source lazy loader', () => { const { render } = createRenderer(); const defaultTransformGetRowsResponse = (response: GridGetRowsResponse) => response; + const fetchRowsSpy = spy(); let transformGetRowsResponse: (response: GridGetRowsResponse) => GridGetRowsResponse; let apiRef: RefObject; - let fetchRowsSpy: SinonStub; let mockServer: ReturnType; function TestDataSourceLazyLoader(props: Partial) { @@ -34,18 +33,11 @@ describeSkipIf(isJSDOM)(' - Data source lazy loader', () => { { useCursorPagination: false, minDelay: 0, maxDelay: 0, verbose: false }, ); - // Reuse the same stub between rerenders to properly count the calls - fetchRowsSpy = useLazyRef(() => stub()).current; + const { fetchRows } = mockServer; - const originalFetchRows = mockServer.fetchRows; - const fetchRows = React.useMemo(() => { + const dataSource: GridDataSource = React.useMemo(() => { fetchRowsSpy.resetHistory(); - fetchRowsSpy.callsFake(originalFetchRows); - return (...args) => fetchRowsSpy(...args); - }, [originalFetchRows]); - - const dataSource: GridDataSource = React.useMemo( - () => ({ + return { getRows: async (params: GridGetRowsParams) => { const urlParams = new URLSearchParams({ filterModel: JSON.stringify(params.filterModel), @@ -54,9 +46,9 @@ describeSkipIf(isJSDOM)(' - Data source lazy loader', () => { end: `${params.end}`, }); - const getRowsResponse = await fetchRows( - `https://mui.com/x/api/data-grid?${urlParams.toString()}`, - ); + const url = `https://mui.com/x/api/data-grid?${urlParams.toString()}`; + fetchRowsSpy(url); + const getRowsResponse = await fetchRows(url); const response = transformGetRowsResponse(getRowsResponse); return { @@ -64,9 +56,8 @@ describeSkipIf(isJSDOM)(' - Data source lazy loader', () => { rowCount: response.rowCount, }; }, - }), - [fetchRows], - ); + }; + }, [fetchRows]); return (
diff --git a/packages/x-data-grid-pro/src/tests/dataSourceTreeData.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/dataSourceTreeData.DataGridPro.test.tsx index 379e98d6d7638..4bcb6c8de77a1 100644 --- a/packages/x-data-grid-pro/src/tests/dataSourceTreeData.DataGridPro.test.tsx +++ b/packages/x-data-grid-pro/src/tests/dataSourceTreeData.DataGridPro.test.tsx @@ -13,10 +13,9 @@ import { GridGroupNode, useGridApiRef, } from '@mui/x-data-grid-pro'; -import { SinonStub, stub } from 'sinon'; +import { spy } from 'sinon'; import { getCell } from 'test/utils/helperFn'; import { describeSkipIf, isJSDOM } from 'test/utils/skipIf'; -import useLazyRef from '@mui/utils/useLazyRef'; const dataSetOptions = { dataSet: 'Employee' as const, @@ -31,9 +30,9 @@ const serverOptions = { minDelay: 0, maxDelay: 0, verbose: false }; // Needs layout describeSkipIf(isJSDOM)(' - Data source tree data', () => { const { render } = createRenderer(); + const fetchRowsSpy = spy(); let apiRef: RefObject; - let fetchRowsSpy: SinonStub; let mockServer: ReturnType; function TestDataSource(props: Partial & { shouldRequestsFail?: boolean }) { @@ -41,18 +40,11 @@ describeSkipIf(isJSDOM)(' - Data source tree data', () => { mockServer = useMockServer(dataSetOptions, serverOptions, props.shouldRequestsFail ?? false); const { columns } = mockServer; - // Reuse the same stub between rerenders to properly count the calls - fetchRowsSpy = useLazyRef(() => stub()).current; + const { fetchRows } = mockServer; - const originalFetchRows = mockServer.fetchRows; - const fetchRows = React.useMemo(() => { + const dataSource: GridDataSource = React.useMemo(() => { fetchRowsSpy.resetHistory(); - fetchRowsSpy.callsFake(originalFetchRows); - return (...args) => fetchRowsSpy(...args); - }, [originalFetchRows]); - - const dataSource: GridDataSource = React.useMemo( - () => ({ + return { getRows: async (params: GridGetRowsParams) => { const urlParams = new URLSearchParams({ paginationModel: JSON.stringify(params.paginationModel), @@ -61,9 +53,9 @@ describeSkipIf(isJSDOM)(' - Data source tree data', () => { groupKeys: JSON.stringify(params.groupKeys), }); - const getRowsResponse = await fetchRows( - `https://mui.com/x/api/data-grid?${urlParams.toString()}`, - ); + const url = `https://mui.com/x/api/data-grid?${urlParams.toString()}`; + fetchRowsSpy(url); + const getRowsResponse = await fetchRows(url); return { rows: getRowsResponse.rows, @@ -72,9 +64,8 @@ describeSkipIf(isJSDOM)(' - Data source tree data', () => { }, getGroupKey: (row) => row[dataSetOptions.treeData.groupingField], getChildrenCount: (row) => row.descendantCount, - }), - [fetchRows], - ); + }; + }, [fetchRows]); return (