Skip to content

Commit

Permalink
[test] Attempt to fix flaky unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasTy committed Sep 11, 2024
1 parent 4f2a244 commit db3cf69
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ const baselineProps: BaselineProps = {
describe('<DataGridPremium /> - Row grouping', () => {
const { render, clock } = createRenderer();

after(() => {
clock.restore();
});

let apiRef: React.MutableRefObject<GridApi>;

function Test(props: Partial<DataGridPremiumProps>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,10 @@ describe('<DataGridPro /> - Cell editing', () => {
describe('with debounceMs > 0', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it('should debounce multiple changes if debounceMs > 0', () => {
render(<TestCase />);
act(() => apiRef.current.startCellEditMode({ id: 0, field: 'currencyPair' }));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,10 @@ describe('<DataGridPro /> - Column pinning', () => {
describe('with fake timers', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it('should not render menu items if the column has `pinnable` equals to false', () => {
render(
<TestCase
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ describe('<DataGridPro /> - Edit components', () => {
describe('with fake timers', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it('should display a indicator while processing the props', async () => {
defaultData.columns[0].preProcessEditCellProps = ({ props }) =>
new Promise((resolve) => {
Expand Down Expand Up @@ -201,6 +205,10 @@ describe('<DataGridPro /> - Edit components', () => {
describe('with fake timers', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it('should display a indicator while processing the props', async () => {
defaultData.columns[0].preProcessEditCellProps = ({ props }) =>
new Promise((resolve) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ describe('<DataGridPro /> - Events params', () => {
describe('onCellClick', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

let eventStack: string[] = [];
const push = (name: string) => () => {
eventStack.push(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ describe('<DataGridPro /> - Print export', () => {
describe('Export toolbar', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it('should display print button by default', () => {
render(<Test slots={{ toolbar: GridToolbar }} />);
fireEvent.click(screen.getByRole('button', { name: 'Export' }));
Expand Down
12 changes: 12 additions & 0 deletions packages/x-data-grid-pro/src/tests/rowEditing.DataGridPro.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,10 @@ describe('<DataGridPro /> - Row editing', () => {
describe('with debounceMs > 0', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it('should debounce multiple changes if debounceMs > 0', () => {
render(<TestCase />);
act(() => apiRef.current.startRowEditMode({ id: 0 }));
Expand Down Expand Up @@ -725,6 +729,10 @@ describe('<DataGridPro /> - Row editing', () => {
describe('with pending value mutation', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it('should run all pending value mutations before calling processRowUpdate', async () => {
const processRowUpdate = spy((newRow) => newRow);
render(<TestCase processRowUpdate={processRowUpdate} />);
Expand Down Expand Up @@ -961,6 +969,10 @@ describe('<DataGridPro /> - Row editing', () => {
describe('by clicking outside the cell', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it(`should publish 'rowEditStop' with reason=rowFocusOut`, () => {
render(<TestCase />);
const listener = spy();
Expand Down
8 changes: 8 additions & 0 deletions packages/x-data-grid/src/tests/layout.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ describe('<DataGrid /> - Layout & warnings', () => {
describe('warnings', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it('should error if the container has no intrinsic height', () => {
expect(() => {
render(
Expand Down Expand Up @@ -232,6 +236,10 @@ describe('<DataGrid /> - Layout & warnings', () => {
describe('swallow warnings', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

beforeEach(() => {
stub(console, 'error');
});
Expand Down
12 changes: 1 addition & 11 deletions packages/x-data-grid/src/tests/quickFiltering.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { getColumnValues, sleep } from 'test/utils/helperFn';
const isJSDOM = /jsdom/.test(window.navigator.userAgent);

describe('<DataGrid /> - Quick filter', () => {
const { render, clock } = createRenderer();
const { render, clock } = createRenderer({ clock: 'fake' });

const baselineProps = {
autoHeight: isJSDOM,
Expand Down Expand Up @@ -61,8 +61,6 @@ describe('<DataGrid /> - Quick filter', () => {
}

describe('component', () => {
clock.withFakeTimers();

it('should apply filter', () => {
render(<TestCase />);

Expand Down Expand Up @@ -164,8 +162,6 @@ describe('<DataGrid /> - Quick filter', () => {
});

describe('quick filter logic', () => {
clock.withFakeTimers();

it('should return rows that match all values by default', () => {
render(<TestCase />);

Expand Down Expand Up @@ -396,8 +392,6 @@ describe('<DataGrid /> - Quick filter', () => {
});

describe('column type: string', () => {
clock.withFakeTimers();

const getRows = ({ quickFilterValues }: Pick<GridFilterModel, 'quickFilterValues'>) => {
const { unmount } = render(
<TestCase
Expand Down Expand Up @@ -506,8 +500,6 @@ describe('<DataGrid /> - Quick filter', () => {
});

describe('column type: number', () => {
clock.withFakeTimers();

const getRows = ({ quickFilterValues }: Pick<GridFilterModel, 'quickFilterValues'>) => {
const { unmount } = render(
<TestCase
Expand Down Expand Up @@ -560,8 +552,6 @@ describe('<DataGrid /> - Quick filter', () => {
});

describe('column type: singleSelect', () => {
clock.withFakeTimers();

const getRows = ({ quickFilterValues }: Pick<GridFilterModel, 'quickFilterValues'>) => {
const { unmount } = render(
<TestCase
Expand Down
4 changes: 4 additions & 0 deletions packages/x-data-grid/src/tests/rowSelection.DataGrid.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,10 @@ describe('<DataGrid /> - Row selection', () => {
describe('ripple', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

it('should keep only one ripple visible when navigating between checkboxes', async function test() {
if (isJSDOM) {
// JSDOM doesn't fire "blur" when .focus is called in another element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ describe('<DateCalendar />', () => {
describe('with fake timers', () => {
clock.withFakeTimers();

after(() => {
clock.restore();
});

// test: https://github.com/mui/mui-x/issues/12373
it('should not reset day to `startOfDay` if value already exists when finding the closest enabled date', () => {
const onChange = spy();
Expand Down

0 comments on commit db3cf69

Please sign in to comment.