@@ -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 (