Skip to content
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
2 changes: 1 addition & 1 deletion packages/block-library/src/spacer/controls.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import { MIN_SPACER_SIZE } from './edit';
import { MIN_SPACER_SIZE } from './edit.js';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hmm, can we rethink the implementation here? I think it's highly unintuitive to use the extension-specific form here to load the constant from the web variant of the file and I think can easily lead to hard to find bugs.

Instead, can we define the constant in a constants.js file? that file won't be forked unless it needs to and define the value for both the web and native mobile to use. WDYT @fluiddot ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Hmm, can we rethink the implementation here? I think it's highly unintuitive to use the extension-specific form here to load the constant from the web variant of the file and I think can easily lead to hard to find bugs.

Instead, can we define the constant in a constants.js file? that file won't be forked unless it needs to and define the value for both the web and native mobile to use. WDYT @fluiddot ?

Good point and I totally agree, I'll open a new PR with that change 👍 .

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

FYI in this PR we'll add the constants file.

import styles from './style.scss';

const DEFAULT_VALUES = { px: 100, em: 10, rem: 10, vw: 10, vh: 25 };
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Spacer block decrements height 1`] = `
"<!-- wp:spacer {\\"height\\":\\"99px\\"} -->
<div style=\\"height:99px\\" aria-hidden=\\"true\\" class=\\"wp-block-spacer\\"></div>
<!-- /wp:spacer -->"
`;

exports[`Spacer block increments height 1`] = `
"<!-- wp:spacer {\\"height\\":\\"101px\\"} -->
<div style=\\"height:101px\\" aria-hidden=\\"true\\" class=\\"wp-block-spacer\\"></div>
<!-- /wp:spacer -->"
`;

exports[`Spacer block inserts block 1`] = `
"<!-- wp:spacer -->
<div style=\\"height:100px\\" aria-hidden=\\"true\\" class=\\"wp-block-spacer\\"></div>
<!-- /wp:spacer -->"
`;

exports[`Spacer block updates height to 25vh 1`] = `
"<!-- wp:spacer {\\"height\\":\\"25vh\\"} -->
<div style=\\"height:25vh\\" aria-hidden=\\"true\\" class=\\"wp-block-spacer\\"></div>
<!-- /wp:spacer -->"
`;

exports[`Spacer block updates height to 50px 1`] = `
"<!-- wp:spacer {\\"height\\":\\"50px\\"} -->
<div style=\\"height:50px\\" aria-hidden=\\"true\\" class=\\"wp-block-spacer\\"></div>
<!-- /wp:spacer -->"
`;
180 changes: 180 additions & 0 deletions packages/block-library/src/spacer/test/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/**
* External dependencies
*/
import {
fireEvent,
getEditorHtml,
initializeEditor,
waitFor,
} from 'test/helpers';

/**
* WordPress dependencies
*/
import { getBlockTypes, unregisterBlockType } from '@wordpress/blocks';
import { registerCoreBlocks } from '@wordpress/block-library';

beforeAll( () => {
// Register all core blocks
registerCoreBlocks();
} );

afterAll( () => {
// Clean up registered blocks
getBlockTypes().forEach( ( block ) => {
unregisterBlockType( block.name );
} );
} );

describe( 'Spacer block', () => {
it( 'inserts block', async () => {
const {
getByA11yLabel,
getByTestId,
getByText,
} = await initializeEditor();

fireEvent.press( getByA11yLabel( 'Add block' ) );

const blockList = getByTestId( 'InserterUI-Blocks' );
// onScroll event used to force the FlatList to render all items
fireEvent.scroll( blockList, {
nativeEvent: {
contentOffset: { y: 0, x: 0 },
contentSize: { width: 100, height: 100 },
layoutMeasurement: { width: 100, height: 100 },
},
} );

fireEvent.press( await waitFor( () => getByText( 'Spacer' ) ) );

expect( getByA11yLabel( /Spacer Block\. Row 1/ ) ).toBeVisible();
expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'updates height to 50px', async () => {
const initialHtml = `<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->`;
const {
getByA11yLabel,
getByDisplayValue,
getByTestId,
getByText,
} = await initializeEditor( {
initialHtml,
} );

// Select Spacer block
const spacerBlock = getByA11yLabel( /Spacer Block\. Row 1/ );
fireEvent.press( spacerBlock );

// Open block settings
fireEvent.press( getByA11yLabel( 'Open Settings' ) );
await waitFor(
() => getByTestId( 'block-settings-modal' ).props.isVisible
);

// Update height attribute
fireEvent.press( getByText( '100' ) );
const heightTextInput = getByDisplayValue( '100' );
fireEvent.changeText( heightTextInput, '50' );

expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'updates height to 25vh', async () => {
const initialHtml = `<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->`;
const {
getByA11yLabel,
getByDisplayValue,
getByTestId,
getByText,
} = await initializeEditor( {
initialHtml,
} );

// Select Spacer block
const spacerBlock = getByA11yLabel( /Spacer Block\. Row 1/ );
fireEvent.press( spacerBlock );

// Open block settings
fireEvent.press( getByA11yLabel( 'Open Settings' ) );
await waitFor(
() => getByTestId( 'block-settings-modal' ).props.isVisible
);

// Set vh unit
fireEvent.press( getByText( 'px' ) );
fireEvent.press( getByText( 'Viewport height (vh)' ) );

// Update height attribute
fireEvent.press( getByText( '100' ) );
const heightTextInput = getByDisplayValue( '100' );
fireEvent.changeText( heightTextInput, '25' );

expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'increments height', async () => {
const initialHtml = `<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->`;
const { getByA11yLabel, getByTestId } = await initializeEditor( {
initialHtml,
} );

// Select Spacer block
const spacerBlock = getByA11yLabel( /Spacer Block\. Row 1/ );
fireEvent.press( spacerBlock );

// Open block settings
fireEvent.press( getByA11yLabel( 'Open Settings' ) );
await waitFor(
() => getByTestId( 'block-settings-modal' ).props.isVisible
);

// Increment height
fireEvent(
getByA11yLabel( /Height\. Value is 100 Pixels \(px\)/ ),
'accessibilityAction',
{
nativeEvent: { actionName: 'increment' },
}
);

expect( getEditorHtml() ).toMatchSnapshot();
} );

it( 'decrements height', async () => {
const initialHtml = `<!-- wp:spacer -->
<div style="height:100px" aria-hidden="true" class="wp-block-spacer"></div>
<!-- /wp:spacer -->`;
const { getByA11yLabel, getByTestId } = await initializeEditor( {
initialHtml,
} );

// Select Spacer block
const spacerBlock = getByA11yLabel( /Spacer Block\. Row 1/ );
fireEvent.press( spacerBlock );

// Open block settings
fireEvent.press( getByA11yLabel( 'Open Settings' ) );
await waitFor(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not a blocker and something to consider for a follow-up PR, could we introduce a helper that would make this wait be used like:

await waitForTestId( 'block-settings-modal' );

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sure, it would be great if we had more helpers to reduce code in tests and make it easier to read. Actually, some time ago I created this PR #38619 where I added several helpers, however, I haven't spared enough time to push it. For this case, we could use the helper openBlockSettings which will wait for the block settings modal to be opened.

Regarding the helper you mentioned (i.e. waitForTestId), I'm not sure if it would fit the requirement here as we're not only waiting for a component with a test Id but also for the modal to be visible. The way I see the implementation of that helper would be:

const waitForTestId = ( testId ) => {
	return waitFor( () => getByTestId( testId) );
};

However, in this case, we're also looking for the prop isVisible to be true:

getByTestId( 'block-settings-modal' ).props.isVisible

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry for not making it clear before, I think the helper should include the truth-check for isVisible. Do you perhaps mean that sometimes we want to check it and others not?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry for not making it clear before, I think the helper should include the truth-check for isVisible.

Oh ok, that makes sense, although not every component has the isVisible prop. I think that in this case, the helper would be specific to modals or another type of component that its visibility is determined by a prop (although can't think of another component 🤔 ). Hence, we could name it differently like waitForModal or waitForModalByTestId instead, wdyt?

Do you perhaps mean that sometimes we want to check it and others not?

Yep, in some cases, usually where state updates occur, we need to wait for a component to be rendered via the waitFor. That's why I thought that the helper waitForTestId would behave as I outlined in this comment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Hence, we could name it differently like waitForModal or waitForModalByTestId instead, wdyt?

Sounds good to me. In the end, my main goal with the comment thread here is to see if we can reduce the verbosity of the code, so anything towards that end I'm 👍

() => getByTestId( 'block-settings-modal' ).props.isVisible
);

// Increment height
fireEvent(
getByA11yLabel( /Height\. Value is 100 Pixels \(px\)/ ),
'accessibilityAction',
{
nativeEvent: { actionName: 'decrement' },
}
);

expect( getEditorHtml() ).toMatchSnapshot();
} );
} );