Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* External dependencies
*/
import { uniqueId } from 'lodash';
export const uniqueId = () =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if the replacement function is needed - the usage of uniqueId in these tests could just be replaced with hard-coded id values.

lodash uniqueId is just a counter that counts up from 1 rather than a random number:
Screen Shot 2022-06-20 at 2 44 22 pm

So I'd expect every test run to produced the same ids, the first call to uniqueId() would return '1', the second '2' etc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Happy to simplify 👍 Done in 8c4d486

// eslint-disable-next-line no-restricted-syntax
Math.floor( Math.random() * Number.MAX_SAFE_INTEGER );

export const fauxEntitySuggestions = [
{
Expand Down
117 changes: 52 additions & 65 deletions packages/block-editor/src/components/link-control/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { render, unmountComponentAtNode } from 'react-dom';
import { act, Simulate } from 'react-dom/test-utils';
import { queryByText, queryByRole } from '@testing-library/react';
import { default as lodash, first, last, nth, uniqueId } from 'lodash';
import { default as lodash } from 'lodash';
/**
* WordPress dependencies
*/
Expand All @@ -18,7 +18,11 @@ import { useSelect } from '@wordpress/data';
* Internal dependencies
*/
import LinkControl from '../';
import { fauxEntitySuggestions, fetchFauxEntitySuggestions } from './fixtures';
import {
fauxEntitySuggestions,
fetchFauxEntitySuggestions,
uniqueId,
} from './fixtures';

// Mock debounce() so that it runs instantly.
lodash.debounce = jest.fn( ( callback ) => {
Expand Down Expand Up @@ -357,7 +361,7 @@ describe( 'Searching for a link', () => {

it( 'should display only search suggestions when current input value is not URL-like', async () => {
const searchTerm = 'Hello world';
const firstFauxSuggestion = first( fauxEntitySuggestions );
const firstFauxSuggestion = fauxEntitySuggestions[ 0 ];

act( () => {
render( <LinkControl />, container );
Expand All @@ -377,9 +381,9 @@ describe( 'Searching for a link', () => {

const searchResultElements = getSearchResults();

const firstSearchResultItemHTML =
first( searchResultElements ).innerHTML;
const lastSearchResultItemHTML = last( searchResultElements ).innerHTML;
const firstSearchResultItemHTML = searchResultElements[ 0 ].innerHTML;
const lastSearchResultItemHTML =
searchResultElements[ searchResultElements.length - 1 ].innerHTML;

expect( searchResultElements ).toHaveLength(
fauxEntitySuggestions.length
Expand Down Expand Up @@ -502,7 +506,8 @@ describe( 'Searching for a link', () => {
const searchResultElements = getSearchResults();

const lastSearchResultItemHTML =
last( searchResultElements ).innerHTML;
searchResultElements[ searchResultElements.length - 1 ]
.innerHTML;
const additionalDefaultFallbackURLSuggestionLength = 1;

// We should see a search result for each of the expect search suggestions
Expand Down Expand Up @@ -974,11 +979,9 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
'[role="listbox"] [role="option"]'
);

const createButton = first(
Array.from( searchResultElements ).filter( ( result ) =>
result.innerHTML.includes( 'Create:' )
)
);
const createButton = Array.from( searchResultElements ).filter(
( result ) => result.innerHTML.includes( 'Create:' )
)[ 0 ];

expect( createButton ).not.toBeNull();
expect( createButton.innerHTML ).toEqual(
Expand Down Expand Up @@ -1071,11 +1074,9 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
'[role="listbox"] [role="option"]'
);

const createButton = first(
Array.from( searchResultElements ).filter( ( result ) =>
result.innerHTML.includes( 'Create:' )
)
);
const createButton = Array.from( searchResultElements ).filter(
( result ) => result.innerHTML.includes( 'Create:' )
)[ 0 ];

await act( async () => {
Simulate.click( createButton );
Expand Down Expand Up @@ -1143,11 +1144,9 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
const searchResultElements = container.querySelectorAll(
'[role="listbox"] [role="option"]'
);
const createButton = first(
Array.from( searchResultElements ).filter( ( result ) =>
result.innerHTML.includes( 'Create:' )
)
);
const createButton = Array.from( searchResultElements ).filter(
( result ) => result.innerHTML.includes( 'Create:' )
)[ 0 ];

// Step down into the search results, highlighting the first result item.
act( () => {
Expand Down Expand Up @@ -1210,11 +1209,9 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
'[role="listbox"] [role="option"]'
);

const createButton = first(
Array.from( searchResultElements ).filter( ( result ) =>
result.innerHTML.includes( 'Custom suggestion text' )
)
);
const createButton = Array.from( searchResultElements ).filter(
( result ) => result.innerHTML.includes( 'Custom suggestion text' )
)[ 0 ];

expect( createButton ).not.toBeNull();
} );
Expand All @@ -1241,11 +1238,9 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
const searchResultElements = container.querySelectorAll(
'[role="listbox"] [role="option"]'
);
const createButton = first(
Array.from( searchResultElements ).filter( ( result ) =>
result.innerHTML.includes( 'Create:' )
)
);
const createButton = Array.from( searchResultElements ).filter(
( result ) => result.innerHTML.includes( 'Create:' )
)[ 0 ];

// Verify input has no value.
expect( searchInput.value ).toBe( '' );
Expand Down Expand Up @@ -1275,11 +1270,9 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
const searchResultElements = container.querySelectorAll(
'[role="listbox"] [role="option"]'
);
const createButton = first(
Array.from( searchResultElements ).filter( ( result ) =>
result.innerHTML.includes( 'New page' )
)
);
const createButton = Array.from( searchResultElements ).filter(
( result ) => result.innerHTML.includes( 'New page' )
)[ 0 ];

// Verify input has no value.
expect( searchInput.value ).toBe( '' );
Expand Down Expand Up @@ -1321,11 +1314,9 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
'[role="listbox"] [role="option"]'
);

const createButton = first(
Array.from( searchResultElements ).filter( ( result ) =>
result.innerHTML.includes( 'New page' )
)
);
const createButton = Array.from( searchResultElements ).filter(
( result ) => result.innerHTML.includes( 'New page' )
)[ 0 ];

expect( createButton ).toBeFalsy(); // Shouldn't exist!
}
Expand Down Expand Up @@ -1366,11 +1357,9 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
let searchResultElements = container.querySelectorAll(
'[role="listbox"] [role="option"]'
);
let createButton = first(
Array.from( searchResultElements ).filter( ( result ) =>
result.innerHTML.includes( 'Create:' )
)
);
let createButton = Array.from( searchResultElements ).filter(
( result ) => result.innerHTML.includes( 'Create:' )
)[ 0 ];

await act( async () => {
Simulate.click( createButton );
Expand Down Expand Up @@ -1407,18 +1396,16 @@ describe( 'Creating Entities (eg: Posts, Pages)', () => {
searchResultElements = container.querySelectorAll(
'[role="listbox"] [role="option"]'
);
createButton = first(
Array.from( searchResultElements ).filter( ( result ) =>
result.innerHTML.includes( 'New page' )
)
);
createButton = Array.from( searchResultElements ).filter(
( result ) => result.innerHTML.includes( 'New page' )
)[ 0 ];
} );
} );
} );

describe( 'Selecting links', () => {
it( 'should display a selected link corresponding to the provided "currentLink" prop', () => {
const selectedLink = first( fauxEntitySuggestions );
const selectedLink = fauxEntitySuggestions[ 0 ];

const LinkControlConsumer = () => {
const [ link ] = useState( selectedLink );
Expand Down Expand Up @@ -1447,7 +1434,7 @@ describe( 'Selecting links', () => {
} );

it( 'should hide "selected" link UI and display search UI prepopulated with previously selected link title when "Change" button is clicked', () => {
const selectedLink = first( fauxEntitySuggestions );
const selectedLink = fauxEntitySuggestions[ 0 ];

const LinkControlConsumer = () => {
const [ link, setLink ] = useState( selectedLink );
Expand Down Expand Up @@ -1484,7 +1471,7 @@ describe( 'Selecting links', () => {

describe( 'Selection using mouse click', () => {
it.each( [
[ 'entity', 'hello world', first( fauxEntitySuggestions ) ], // Entity search.
[ 'entity', 'hello world', fauxEntitySuggestions[ 0 ] ], // Entity search.
[
'url',
'https://www.wordpress.org',
Expand Down Expand Up @@ -1528,7 +1515,7 @@ describe( 'Selecting links', () => {

const searchResultElements = getSearchResults();

const firstSearchSuggestion = first( searchResultElements );
const firstSearchSuggestion = searchResultElements[ 0 ];

// Simulate selecting the first of the search suggestions.
act( () => {
Expand Down Expand Up @@ -1557,7 +1544,7 @@ describe( 'Selecting links', () => {

describe( 'Selection using keyboard', () => {
it.each( [
[ 'entity', 'hello world', first( fauxEntitySuggestions ) ], // Entity search.
[ 'entity', 'hello world', fauxEntitySuggestions[ 0 ] ], // Entity search.
[
'url',
'https://www.wordpress.org',
Expand Down Expand Up @@ -1607,8 +1594,8 @@ describe( 'Selecting links', () => {

const searchResultElements = getSearchResults();

const firstSearchSuggestion = first( searchResultElements );
const secondSearchSuggestion = nth( searchResultElements, 1 );
const firstSearchSuggestion = searchResultElements[ 0 ];
const secondSearchSuggestion = searchResultElements[ 1 ];

let selectedSearchResultElement = container.querySelector(
'[role="option"][aria-selected="true"]'
Expand Down Expand Up @@ -1711,8 +1698,8 @@ describe( 'Selecting links', () => {

const searchResultElements = getSearchResults();

const firstSearchSuggestion = first( searchResultElements );
const secondSearchSuggestion = nth( searchResultElements, 1 );
const firstSearchSuggestion = searchResultElements[ 0 ];
const secondSearchSuggestion = searchResultElements[ 1 ];

let selectedSearchResultElement = container.querySelector(
'[role="option"][aria-selected="true"]'
Expand Down Expand Up @@ -1758,7 +1745,7 @@ describe( 'Selecting links', () => {

describe( 'Addition Settings UI', () => {
it( 'should display "New Tab" setting (in "off" mode) by default when a link is selected', async () => {
const selectedLink = first( fauxEntitySuggestions );
const selectedLink = fauxEntitySuggestions[ 0 ];
const expectedSettingText = 'Open in new tab';

const LinkControlConsumer = () => {
Expand Down Expand Up @@ -1791,7 +1778,7 @@ describe( 'Addition Settings UI', () => {
} );

it( 'should display a setting control with correct default state for each of the custom settings provided', async () => {
const selectedLink = first( fauxEntitySuggestions );
const selectedLink = fauxEntitySuggestions[ 0 ];

const customSettings = [
{
Expand Down Expand Up @@ -2266,7 +2253,7 @@ describe( 'Rich link previews', () => {
} );

describe( 'Controlling link title text', () => {
const selectedLink = first( fauxEntitySuggestions );
const selectedLink = fauxEntitySuggestions[ 0 ];

it( 'should not show a means to alter the link title text by default', async () => {
act( () => {
Expand All @@ -2285,7 +2272,7 @@ describe( 'Controlling link title text', () => {
'should not show the link title text input when the URL is `%s`',
async ( urlValue ) => {
const selectedLinkWithoutURL = {
...first( fauxEntitySuggestions ),
...fauxEntitySuggestions[ 0 ],
url: urlValue,
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { render, unmountComponentAtNode } from 'react-dom';
import { act, Simulate } from 'react-dom/test-utils';
import { uniqueId } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -31,7 +30,7 @@ afterEach( () => {
container = null;
} );

const inputId = uniqueId();
const inputId = 'input-12345678';

const sizeOptions = [
{
Expand Down
24 changes: 14 additions & 10 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { values, omit } from 'lodash';
import { omit } from 'lodash';
import deepFreeze from 'deep-freeze';

/**
Expand Down Expand Up @@ -666,7 +666,7 @@ describe( 'state', () => {
} );

expect( Object.keys( state.byClientId ) ).toHaveLength( 1 );
expect( values( state.byClientId )[ 0 ].clientId ).toBe(
expect( Object.values( state.byClientId )[ 0 ].clientId ).toBe(
'bananas'
);
expect( state.order ).toEqual( {
Expand Down Expand Up @@ -729,7 +729,9 @@ describe( 'state', () => {
} );

expect( Object.keys( state.byClientId ) ).toHaveLength( 2 );
expect( values( state.byClientId )[ 1 ].clientId ).toBe( 'ribs' );
expect( Object.values( state.byClientId )[ 1 ].clientId ).toBe(
'ribs'
);
expect( state.order ).toEqual( {
'': [ 'chicken', 'ribs' ],
chicken: [],
Expand Down Expand Up @@ -773,10 +775,12 @@ describe( 'state', () => {
} );

expect( Object.keys( state.byClientId ) ).toHaveLength( 1 );
expect( values( state.byClientId )[ 0 ].name ).toBe(
expect( Object.values( state.byClientId )[ 0 ].name ).toBe(
'core/freeform'
);
expect( values( state.byClientId )[ 0 ].clientId ).toBe( 'wings' );
expect( Object.values( state.byClientId )[ 0 ].clientId ).toBe(
'wings'
);
expect( state.order ).toEqual( {
'': [ 'wings' ],
wings: [],
Expand Down Expand Up @@ -930,15 +934,15 @@ describe( 'state', () => {
} );

expect( Object.keys( replacedState.byClientId ) ).toHaveLength( 1 );
expect( values( originalState.byClientId )[ 0 ].name ).toBe(
expect( Object.values( originalState.byClientId )[ 0 ].name ).toBe(
'core/test-block'
);
expect( values( replacedState.byClientId )[ 0 ].name ).toBe(
expect( Object.values( replacedState.byClientId )[ 0 ].name ).toBe(
'core/freeform'
);
expect( values( replacedState.byClientId )[ 0 ].clientId ).toBe(
'chicken'
);
expect(
Object.values( replacedState.byClientId )[ 0 ].clientId
).toBe( 'chicken' );
expect( replacedState.order ).toEqual( {
'': [ 'chicken' ],
chicken: [],
Expand Down
Loading