|
1 | | -import { act, render, screen, waitFor, within } from '@testing-library/react'; |
| 1 | +import { render, screen, waitFor, within } from '@testing-library/react'; |
2 | 2 | import userEvent from '@testing-library/user-event'; |
3 | 3 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
4 | 4 | import { useMemo, useState } from '@wordpress/element'; |
@@ -754,45 +754,22 @@ describe( 'DataViews component', () => { |
754 | 754 | it( 'lays the grid out at its real column count on the first render', async () => { |
755 | 755 | // Without a measurement as the grid attaches, the first render runs |
756 | 756 | // 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( |
762 | 758 | window.HTMLElement.prototype, |
763 | 759 | 'offsetWidth', |
764 | | - { configurable: true, value: 500 } |
765 | | - ); |
766 | | - vi.useFakeTimers(); |
| 760 | + 'get' |
| 761 | + ).mockReturnValue( 500 ); |
767 | 762 |
|
768 | | - try { |
769 | | - render( <DataViewWrapper view={ { type: 'grid' } } /> ); |
| 763 | + render( <DataViewWrapper view={ { type: 'grid' } } /> ); |
770 | 764 |
|
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 ); |
774 | 768 |
|
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 | + ); |
796 | 773 | } ); |
797 | 774 |
|
798 | 775 | it( 'should display the passed in data', async () => { |
|
0 commit comments