Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lodash: Refactor away from _.delay() #42966

Merged
merged 2 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ module.exports = {
'countBy',
'defaults',
'defaultTo',
'delay',
'differenceWith',
'dropRight',
'each',
Expand Down
9 changes: 7 additions & 2 deletions packages/block-editor/src/components/inserter/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { AccessibilityInfo, Platform, Text } from 'react-native';
import { delay } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -80,6 +79,8 @@ const defaultRenderToggle = ( {
};

export class Inserter extends Component {
announcementTimeout;

constructor() {
super( ...arguments );

Expand All @@ -88,6 +89,10 @@ export class Inserter extends Component {
this.renderContent = this.renderContent.bind( this );
}

componentWillUnmount() {
clearTimeout( this.announcementTimeout );
}

getInsertionOptions() {
const addBeforeOption = {
value: 'before',
Expand Down Expand Up @@ -217,7 +222,7 @@ export class Inserter extends Component {
const announcement = isOpen
? __( 'Scrollable block menu opened. Select a block.' )
: __( 'Scrollable block menu closed.' );
delay(
this.announcementTimeout = setTimeout(
() =>
AccessibilityInfo.announceForAccessibility(
announcement
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
*/
import { Platform } from 'react-native';

import { delay } from 'lodash';

import prompt from 'react-native-prompt-android';

/**
Expand Down Expand Up @@ -46,6 +44,8 @@ const URL_MEDIA_SOURCE = 'URL';
const PICKER_OPENING_DELAY = 200;

export class MediaUpload extends Component {
pickerTimeout;

constructor( props ) {
super( props );
this.onPickerPresent = this.onPickerPresent.bind( this );
Expand Down Expand Up @@ -78,6 +78,10 @@ export class MediaUpload extends Component {
}
}

componentWillUnmount() {
clearTimeout( this.pickerTimeout );
}

getAllSources() {
const { onSelectURL } = this.props;

Expand Down Expand Up @@ -189,7 +193,7 @@ export class MediaUpload extends Component {
// the delay below is required because on iOS this action sheet gets dismissed by the close event of the Inserter
// so this delay allows the Inserter to be closed fully before presenting action sheet.
if ( autoOpen && isIOS ) {
delay(
this.pickerTimeout = setTimeout(
() => this.picker.presentPicker(),
PICKER_OPENING_DELAY
);
Expand Down
6 changes: 4 additions & 2 deletions packages/block-library/src/columns/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { View, Dimensions } from 'react-native';
import { times, map, delay } from 'lodash';
import { times, map } from 'lodash';
/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -496,7 +496,9 @@ const ColumnsEdit = ( props ) => {

useEffect( () => {
if ( isSelected && isDefaultColumns ) {
delay( () => setIsVisible( true ), 100 );
const revealTimeout = setTimeout( () => setIsVisible( true ), 100 );

return () => clearTimeout( revealTimeout );
}
}, [] );

Expand Down
16 changes: 6 additions & 10 deletions packages/block-library/src/image/test/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
initializeEditor,
getEditorHtml,
render,
waitFor,
} from 'test/helpers';
import { Image } from 'react-native';
import Clipboard from '@react-native-clipboard/clipboard';
Expand Down Expand Up @@ -40,16 +41,6 @@ sendMediaUpload.mockImplementation( ( payload ) => {
uploadCallBack( payload );
} );

/**
* Immediately invoke delayed functions. A better alternative would be using
* fake timers and test the delay itself. However, fake timers does not work
* with our custom waitFor implementation.
*/
jest.mock( 'lodash', () => {
const actual = jest.requireActual( 'lodash' );
return { ...actual, delay: ( cb ) => cb() };
} );

function mockGetMedia( media ) {
jest.spyOn( select( coreStore ), 'getMedia' ).mockReturnValue( media );
}
Expand Down Expand Up @@ -157,6 +148,9 @@ describe( 'Image Block', () => {
'wordpress.org'
);
fireEvent.press( screen.getByA11yLabel( 'Apply' ) );
await waitFor(
() => new Promise( ( resolve ) => setTimeout( resolve, 100 ) )
);

const expectedHtml = `<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"custom","className":"is-style-default"} -->
<figure class="wp-block-image size-large is-style-default"><a href="http://wordpress.org"><img src="https://cldup.com/cXyG__fTLN.jpg" alt="" class="wp-image-1"/></a><figcaption class="wp-element-caption">Mountain</figcaption></figure>
Expand All @@ -183,6 +177,7 @@ describe( 'Image Block', () => {
);
fireEvent.press( screen.getByText( 'None' ) );
fireEvent.press( screen.getByText( 'Media File' ) );
await waitFor( () => screen.getByText( 'Custom URL' ) );
fireEvent.press( screen.getByText( 'Custom URL' ) );
// Await asynchronous fetch of clipboard
await act( () => clipboardPromise );
Expand All @@ -191,6 +186,7 @@ describe( 'Image Block', () => {
'wordpress.org'
);
fireEvent.press( screen.getByA11yLabel( 'Apply' ) );
await waitFor( () => screen.getByText( 'Custom URL' ) );
fireEvent.press( screen.getByText( 'Custom URL' ) );
// Await asynchronous fetch of clipboard
await act( () => clipboardPromise );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
*/
import { Keyboard } from 'react-native';
import { useNavigation, useRoute } from '@react-navigation/native';
import { delay } from 'lodash';

/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useEffect, useMemo, useRef } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -18,10 +17,12 @@ import { LinkPicker } from './';
const LinkPickerScreen = ( { returnScreenName } ) => {
const navigation = useNavigation();
const route = useRoute();
const navigateToLinkTimeoutRef = useRef( null );
const navigateBackTimeoutRef = useRef( null );

const onLinkPicked = ( { url, title } ) => {
Keyboard.dismiss();
delay( () => {
navigateToLinkTimeoutRef.current = setTimeout( () => {
navigation.navigate( returnScreenName, {
inputValue: url,
text: title,
Expand All @@ -31,11 +32,18 @@ const LinkPickerScreen = ( { returnScreenName } ) => {

const onCancel = () => {
Keyboard.dismiss();
delay( () => {
navigateBackTimeoutRef.current = setTimeout( () => {
navigation.goBack();
}, 100 );
};

useEffect( () => {
return () => {
clearTimeout( navigateToLinkTimeoutRef.current );
clearTimeout( navigateBackTimeoutRef.current );
};
}, [] );

const { inputValue } = route.params;
return useMemo( () => {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
*/
import { Keyboard } from 'react-native';
import { useNavigation, useRoute } from '@react-navigation/native';
import { delay } from 'lodash';

/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { useEffect, useMemo, useRef } from '@wordpress/element';

import { LinkPicker } from '@wordpress/components';

Expand All @@ -19,9 +19,12 @@ import linkSettingsScreens from './screens';
const LinkPickerScreen = () => {
const navigation = useNavigation();
const route = useRoute();
const navigateToLinkTimeoutRef = useRef( null );
const navigateBackTimeoutRef = useRef( null );

const onLinkPicked = ( { url, title } ) => {
Keyboard.dismiss();
delay( () => {
navigateToLinkTimeoutRef.current = setTimeout( () => {
navigation.navigate( linkSettingsScreens.settings, {
inputValue: url,
text: title,
Expand All @@ -31,11 +34,18 @@ const LinkPickerScreen = () => {

const onCancel = () => {
Keyboard.dismiss();
delay( () => {
navigateBackTimeoutRef.current = setTimeout( () => {
navigation.goBack();
}, 100 );
};

useEffect( () => {
return () => {
clearTimeout( navigateToLinkTimeoutRef.current );
clearTimeout( navigateBackTimeoutRef.current );
};
}, [] );

const { inputValue } = route.params;
return useMemo( () => {
return (
Expand Down