Skip to content

Commit 88ffe5f

Browse files
committed
Simplify test
1 parent 7936050 commit 88ffe5f

1 file changed

Lines changed: 12 additions & 35 deletions

File tree

‎packages/dataviews/src/dataviews/test/dataviews.jsdom.test.tsx‎

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { act, render, screen, waitFor, within } from '@testing-library/react';
1+
import { render, screen, waitFor, within } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
44
import { useMemo, useState } from '@wordpress/element';
@@ -754,45 +754,22 @@ describe( 'DataViews component', () => {
754754
it( 'lays the grid out at its real column count on the first render', async () => {
755755
// Without a measurement as the grid attaches, the first render runs
756756
// at width 0 and puts every item in a full-width row of its own.
757-
const offsetWidth = Object.getOwnPropertyDescriptor(
758-
window.HTMLElement.prototype,
759-
'offsetWidth'
760-
);
761-
Object.defineProperty(
757+
vi.spyOn(
762758
window.HTMLElement.prototype,
763759
'offsetWidth',
764-
{ configurable: true, value: 500 }
765-
);
766-
vi.useFakeTimers();
760+
'get'
761+
).mockReturnValue( 500 );
767762

768-
try {
769-
render( <DataViewWrapper view={ { type: 'grid' } } /> );
763+
render( <DataViewWrapper view={ { type: 'grid' } } /> );
770764

771-
// 500px fits two columns at the default 230px preview size, so
772-
// the three items make two rows rather than three.
773-
expect( screen.getAllByRole( 'row' ) ).toHaveLength( 2 );
765+
// 500px fits two columns at the default 230px preview size, so the
766+
// three items make two rows rather than three.
767+
expect( screen.getAllByRole( 'row' ) ).toHaveLength( 2 );
774768

775-
// The observer reports the same width, so nothing changes.
776-
await act( async () => {
777-
vi.runOnlyPendingTimers();
778-
} );
779-
expect( screen.getAllByRole( 'row' ) ).toHaveLength( 2 );
780-
} finally {
781-
vi.useRealTimers();
782-
if ( offsetWidth ) {
783-
Object.defineProperty(
784-
window.HTMLElement.prototype,
785-
'offsetWidth',
786-
offsetWidth
787-
);
788-
} else {
789-
// Nothing to put back, so remove the stub.
790-
Reflect.deleteProperty(
791-
window.HTMLElement.prototype,
792-
'offsetWidth'
793-
);
794-
}
795-
}
769+
// The observer reports the same width, so nothing changes.
770+
await waitFor( () =>
771+
expect( screen.getAllByRole( 'row' ) ).toHaveLength( 2 )
772+
);
796773
} );
797774

798775
it( 'should display the passed in data', async () => {

0 commit comments

Comments
 (0)