Skip to content

Commit

Permalink
Tests: Use real timers where fake timers are unnecessary (#47144)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Jan 16, 2023
1 parent 1dc0b23 commit aab71ed
Show file tree
Hide file tree
Showing 22 changed files with 49 additions and 229 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { useSelect } from '@wordpress/data';
import DownloadableBlockListItem from '../';
import { plugin } from '../../test/fixtures';

jest.useFakeTimers();

jest.mock( '@wordpress/data/src/components/use-select', () => {
// This allows us to tweak the returned value on each test.
const mock = jest.fn();
Expand Down Expand Up @@ -68,9 +66,7 @@ describe( 'DownloadableBlockListItem', () => {
} );

it( 'should try to install the block plugin', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

useSelect.mockImplementation( () => ( {
isInstalling: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { alignLeft, alignCenter } from '@wordpress/icons';
*/
import AlignmentUI from '../ui';

jest.useFakeTimers();

describe( 'AlignmentUI', () => {
const alignment = 'left';
const onChangeSpy = jest.fn();
Expand Down Expand Up @@ -50,9 +48,7 @@ describe( 'AlignmentUI', () => {
} );

test( 'should expand controls when toggled', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

const { unmount } = render(
<AlignmentUI
Expand Down Expand Up @@ -85,9 +81,7 @@ describe( 'AlignmentUI', () => {
} );

test( 'should call on change with undefined when a control is already active', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render(
<AlignmentUI
Expand All @@ -111,9 +105,7 @@ describe( 'AlignmentUI', () => {
} );

test( 'should call on change a new value when the control is not active', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render(
<AlignmentUI
Expand All @@ -136,9 +128,7 @@ describe( 'AlignmentUI', () => {
} );

test( 'should allow custom alignment controls to be specified', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

const { container } = render(
<AlignmentUI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import userEvent from '@testing-library/user-event';
*/
import BlockAlignmentUI from '../ui';

jest.useFakeTimers();

describe( 'BlockAlignmentUI', () => {
const alignment = 'left';
const onChange = jest.fn();
Expand Down Expand Up @@ -45,9 +43,7 @@ describe( 'BlockAlignmentUI', () => {
} );

test( 'should expand controls when toggled', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

const { unmount } = render(
<BlockAlignmentUI
Expand Down Expand Up @@ -80,9 +76,7 @@ describe( 'BlockAlignmentUI', () => {
} );

test( 'should call onChange with undefined, when the control is already active', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render(
<BlockAlignmentUI
Expand All @@ -105,9 +99,7 @@ describe( 'BlockAlignmentUI', () => {
} );

test( 'should call onChange with alignment value when the control is inactive', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render(
<BlockAlignmentUI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import userEvent from '@testing-library/user-event';
*/
import BlockVerticalAlignmentUI from '../ui';

jest.useFakeTimers();

describe( 'BlockVerticalAlignmentUI', () => {
const alignment = 'top';
const onChange = jest.fn();
Expand Down Expand Up @@ -43,9 +41,7 @@ describe( 'BlockVerticalAlignmentUI', () => {
} );

test( 'should expand controls when toggled', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

const { unmount } = render(
<BlockVerticalAlignmentUI
Expand Down Expand Up @@ -78,9 +74,7 @@ describe( 'BlockVerticalAlignmentUI', () => {
} );

it( 'should call onChange with undefined, when the control is already active', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render(
<BlockVerticalAlignmentUI
Expand All @@ -104,9 +98,7 @@ describe( 'BlockVerticalAlignmentUI', () => {

it( 'should call onChange with alignment value when the control is inactive', async () => {
// note "middle" alias for "center"
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render(
<BlockVerticalAlignmentUI
Expand Down
10 changes: 2 additions & 8 deletions packages/block-editor/src/components/colors/test/with-colors.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import userEvent from '@testing-library/user-event';
*/
import { createCustomColorsHOC } from '../with-colors';

jest.useFakeTimers();

describe( 'createCustomColorsHOC', () => {
it( 'provides the wrapped component with color values and setter functions as props', () => {
const withCustomColors = createCustomColorsHOC( [
Expand Down Expand Up @@ -44,9 +42,7 @@ describe( 'createCustomColorsHOC', () => {
} );

it( 'setting the color to a value in the provided custom color array updated the backgroundColor attribute', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
const withCustomColors = createCustomColorsHOC( [
{ name: 'Red', slug: 'red', color: 'ff0000' },
] );
Expand Down Expand Up @@ -76,9 +72,7 @@ describe( 'createCustomColorsHOC', () => {
} );

it( 'setting the color to a value not in the provided custom color array updates customBackgroundColor attribute', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
const withCustomColors = createCustomColorsHOC( [
{ name: 'Red', slug: 'red', color: 'ff0000' },
] );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import userEvent from '@testing-library/user-event';
*/
import { DefaultBlockAppender, ZWNBSP } from '../';

jest.useFakeTimers();

describe( 'DefaultBlockAppender', () => {
it( 'should match snapshot', () => {
const onAppend = jest.fn();
Expand All @@ -23,9 +21,7 @@ describe( 'DefaultBlockAppender', () => {
} );

it( 'should append a default block when input focused', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
const onAppend = jest.fn();

const { container } = render(
Expand All @@ -44,9 +40,7 @@ describe( 'DefaultBlockAppender', () => {
} );

it( 'should optionally show without prompt', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
const onAppend = jest.fn();

const { container } = render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { useState } from '@wordpress/element';
*/
import MediaReplaceFlow from '../';

jest.useFakeTimers();

const noop = () => {};

function TestWrapper() {
Expand Down Expand Up @@ -57,9 +55,7 @@ describe( 'General media replace flow', () => {
} );

it( 'renders replace menu', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render( <TestWrapper /> );

Expand All @@ -81,9 +77,7 @@ describe( 'General media replace flow', () => {
} );

it( 'displays media URL', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render( <TestWrapper /> );

Expand All @@ -106,9 +100,7 @@ describe( 'General media replace flow', () => {
} );

it( 'edits media URL', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render( <TestWrapper /> );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import { SelectControl } from '@wordpress/components';
*/
import ResponsiveBlockControl from '../index';

jest.useFakeTimers();

const inputId = 'input-12345678';

const sizeOptions = [
Expand Down Expand Up @@ -246,9 +244,7 @@ describe( 'Default and Responsive modes', () => {
} );

it( 'should switch between default and responsive modes when interacting with toggle control', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
const ResponsiveBlockControlConsumer = () => {
const [ isResponsive, setIsResponsive ] = useState( false );

Expand Down
18 changes: 4 additions & 14 deletions packages/block-editor/src/components/url-input/test/button.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import userEvent from '@testing-library/user-event';
*/
import URLInputButton from '../button';

jest.useFakeTimers();

describe( 'URLInputButton', () => {
it( 'should render a `Insert link` button and not be pressed when `url` is not provided', () => {
render( <URLInputButton /> );
Expand Down Expand Up @@ -57,9 +55,7 @@ describe( 'URLInputButton', () => {
} );

it( 'should render a form when `Insert link` button is clicked', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
render( <URLInputButton /> );

// Click the button to insert a link.
Expand All @@ -76,9 +72,7 @@ describe( 'URLInputButton', () => {
} );

it( 'should call `onChange` function once per each value change', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
const onChangeMock = jest.fn();

render( <URLInputButton onChange={ onChangeMock } /> );
Expand All @@ -104,9 +98,7 @@ describe( 'URLInputButton', () => {
} );

it( 'should close the form when the user clicks the `Close` button', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render( <URLInputButton /> );

Expand Down Expand Up @@ -135,9 +127,7 @@ describe( 'URLInputButton', () => {
} );

it( 'should close the form when user submits it', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
const onChangeMock = jest.fn();

render( <URLInputButton onChange={ onChangeMock } /> );
Expand Down
6 changes: 1 addition & 5 deletions packages/block-editor/src/components/warning/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import userEvent from '@testing-library/user-event';
*/
import Warning from '../index';

jest.useFakeTimers();

describe( 'Warning', () => {
it( 'should match snapshot', () => {
const { container } = render( <Warning>error</Warning> );
Expand All @@ -33,9 +31,7 @@ describe( 'Warning', () => {
} );

it( 'should show hidden secondary actions', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();

render(
<Warning secondaryActions={ [ { title: 'test', onClick: null } ] }>
Expand Down
6 changes: 1 addition & 5 deletions packages/compose/src/higher-order/pure/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ import { Component } from '@wordpress/element';
*/
import pure from '../';

jest.useFakeTimers();

describe( 'pure', () => {
it( 'functional component should rerender only when props change', () => {
let i = 0;
Expand All @@ -42,9 +40,7 @@ describe( 'pure', () => {
} );

it( 'class component should rerender if the props or state change', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
let i = 0;
const MyComp = pure(
class extends Component {
Expand Down
6 changes: 1 addition & 5 deletions packages/compose/src/higher-order/with-state/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ import userEvent from '@testing-library/user-event';
*/
import withState from '../';

jest.useFakeTimers();

describe( 'withState', () => {
it( 'should pass initial state and allow updates', async () => {
const user = userEvent.setup( {
advanceTimers: jest.advanceTimersByTime,
} );
const user = userEvent.setup();
const EnhancedComponent = withState( {
count: 0,
} )( ( { count, setState } ) => (
Expand Down
Loading

0 comments on commit aab71ed

Please sign in to comment.