Skip to content

Commit

Permalink
Fix lazy loading tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arminmeh committed Jan 29, 2025
1 parent 18c0dca commit 8d65cfd
Showing 1 changed file with 58 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';
import { useMockServer } from '@mui/x-data-grid-generator';
import { createRenderer, waitFor } from '@mui/internal-test-utils';
import { act, createRenderer, waitFor } from '@mui/internal-test-utils';
import { getRow } from 'test/utils/helperFn';
import { expect } from 'chai';
import { RefObject } from '@mui/x-internals/types';
Expand Down Expand Up @@ -75,7 +75,7 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
columns={mockServer.columns}
unstable_dataSource={dataSource}
unstable_lazyLoading
initialState={{ pagination: { paginationModel: { page: 0, pageSize: 10 } } }}
initialState={{ pagination: { paginationModel: { page: 0, pageSize: 10 }, rowCount: 0 } }}
disableVirtualization
{...props}
/>
Expand Down Expand Up @@ -129,7 +129,9 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
// reset the spy call count
fetchRowsSpy.resetHistory();

apiRef.current?.scrollToIndexes({ rowIndex: 10 });
act(() => {
apiRef.current?.scrollToIndexes({ rowIndex: 50 });
});

await waitFor(() => {
expect(fetchRowsSpy.callCount).to.equal(1);
Expand All @@ -144,7 +146,9 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
const initialSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams;
expect(initialSearchParams.get('end')).to.equal('9');

apiRef.current?.scrollToIndexes({ rowIndex: 10 });
act(() => {
apiRef.current?.scrollToIndexes({ rowIndex: 10 });
});

await waitFor(() => {
expect(fetchRowsSpy.callCount).to.equal(2);
Expand All @@ -153,7 +157,9 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
const beforeSortSearchParams = new URL(fetchRowsSpy.lastCall.args[0]).searchParams;
expect(beforeSortSearchParams.get('end')).to.not.equal('9');

apiRef.current?.sortColumn(mockServer.columns[0].field, 'asc');
act(() => {
apiRef.current?.sortColumn(mockServer.columns[0].field, 'asc');
});

await waitFor(() => {
expect(fetchRowsSpy.callCount).to.equal(3);
Expand All @@ -168,7 +174,9 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
// wait until the rows are rendered
await waitFor(() => expect(getRow(0)).not.to.be.undefined);

apiRef.current?.scrollToIndexes({ rowIndex: 10 });
act(() => {
apiRef.current?.scrollToIndexes({ rowIndex: 10 });
});

await waitFor(() => {
expect(fetchRowsSpy.callCount).to.equal(2);
Expand All @@ -178,14 +186,16 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
// first row is not the first page anymore
expect(beforeFilteringSearchParams.get('start')).to.not.equal('0');

apiRef.current?.setFilterModel({
items: [
{
field: mockServer.columns[0].field,
value: '0',
operator: 'contains',
},
],
act(() => {
apiRef.current?.setFilterModel({
items: [
{
field: mockServer.columns[0].field,
value: '0',
operator: 'contains',
},
],
});
});

await waitFor(() => {
Expand Down Expand Up @@ -222,8 +232,12 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
fetchRowsSpy.resetHistory();

// make one small and one big scroll that makes sure that the bottom of the grid window is reached
apiRef.current?.scrollToIndexes({ rowIndex: 1 });
apiRef.current?.scrollToIndexes({ rowIndex: 9 });
act(() => {
apiRef.current?.scrollToIndexes({ rowIndex: 1 });
});
act(() => {
apiRef.current?.scrollToIndexes({ rowIndex: 9 });
});

// Only one additional fetch should have been made
await waitFor(() => {
Expand All @@ -236,7 +250,9 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
// wait until the rows are rendered
await waitFor(() => expect(getRow(0)).not.to.be.undefined);

apiRef.current?.scrollToIndexes({ rowIndex: 9 });
act(() => {
apiRef.current?.scrollToIndexes({ rowIndex: 9 });
});

// wait until the debounced fetch
await waitFor(() => {
Expand All @@ -250,7 +266,9 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
// last row is not the first page anymore
expect(beforeSortingSearchParams.get('end')).to.not.equal('9');

apiRef.current?.sortColumn(mockServer.columns[0].field, 'asc');
act(() => {
apiRef.current?.sortColumn(mockServer.columns[0].field, 'asc');
});

await waitFor(() => {
expect(fetchRowsSpy.callCount).to.equal(3);
Expand All @@ -266,7 +284,9 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
// wait until the rows are rendered
await waitFor(() => expect(getRow(0)).not.to.be.undefined);

apiRef.current?.scrollToIndexes({ rowIndex: 9 });
act(() => {
apiRef.current?.scrollToIndexes({ rowIndex: 9 });
});

// wait until the debounced fetch
await waitFor(() => {
Expand All @@ -280,14 +300,16 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
// last row is not the first page anymore
expect(beforeFilteringSearchParams.get('end')).to.not.equal('9');

apiRef.current?.setFilterModel({
items: [
{
field: mockServer.columns[0].field,
value: '0',
operator: 'contains',
},
],
act(() => {
apiRef.current?.setFilterModel({
items: [
{
field: mockServer.columns[0].field,
value: '0',
operator: 'contains',
},
],
});
});

await waitFor(() => {
Expand Down Expand Up @@ -350,11 +372,15 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
fetchRowsSpy.resetHistory();

// reduce the rowCount to be more than the number of rows
apiRef.current?.setRowCount(80);
act(() => {
apiRef.current?.setRowCount(80);
});
expect(fetchRowsSpy.callCount).to.equal(0);

// reduce the rowCount once more, but now to be less than the number of rows
apiRef.current?.setRowCount(20);
act(() => {
apiRef.current?.setRowCount(20);
});
await waitFor(() => expect(fetchRowsSpy.callCount).to.equal(1));
});

Expand All @@ -369,7 +395,9 @@ describeSkipIf(isJSDOM)('<DataGridPro /> - Data source lazy loader', () => {
expect(() => getRow(10)).to.throw();

// set the rowCount via API
apiRef.current?.setRowCount(100);
act(() => {
apiRef.current?.setRowCount(100);
});

// wait until the rows are added
await waitFor(() => expect(getRow(10)).not.to.be.undefined);
Expand Down

0 comments on commit 8d65cfd

Please sign in to comment.