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

[WIP] Add a basic test for shortcode transformation #7689

Closed
wants to merge 1 commit into from

Conversation

danielbachhuber
Copy link
Member

@danielbachhuber danielbachhuber commented Jul 3, 2018

The beginnings of the solution to #7030, #4456, etc.

@danielbachhuber danielbachhuber changed the title Add a basic test for shortcode transformation [WIP] Add a basic test for shortcode transformation Jul 3, 2018
@danielbachhuber
Copy link
Member Author

The significant underlying challenge, as documented in the diff, with adding tests around segmentHTMLToShortcodeBlock is its dependency on functions which are in-turn dependent on getBlockTypes() and getBlockType(). Both of the aforementioned functions are dependent on select( 'core/blocks' ), which ultimately exposes the fact that all block types are defined at runtime.

On a related note, it's annoying to need to redefine all of the block types in the test. I suspect this will be very brittle and break as soon as any block type definition changes. Related #6512

As a dirty hack to get things started, I've injected blockTypes into each of the functions that need theme. What's a better approach we could take? Is there some pattern for overloading select( 'core/blocks' )?

innerBlocks: [],
isValid: true,
name: 'core/shortcode',
uuid: 'invalid',
Copy link
Member Author

Choose a reason for hiding this comment

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

uuid being a random value is the cause of the test failure right now. What's the best way of addressing?

Copy link
Member

Choose a reason for hiding this comment

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

Not entirely sure it works in this specific usage, but perhaps expect.any( String ) ?

http://jestjs.io/docs/en/expect#expectanyconstructor

Copy link
Member Author

Choose a reason for hiding this comment

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

Not entirely sure it works in this specific usage

Yeah, I suppose I could make the assertions a bit more verbose and inspect each part of the array.

Another idea is to create a wrapper for the uuid function that lets us inject some state for the purposes of testing.

Copy link
Member

Choose a reason for hiding this comment

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

expect.any( String ) is better, we don't care about what uuid produces in this context.

@aduth
Copy link
Member

aduth commented Jul 3, 2018

Related: #336

@@ -58,16 +54,19 @@ function segmentHTMLToShortcodeBlock( HTML, lastIndex = 0 ) {
( schema ) => schema.shortcode( match.shortcode.attrs, match ),
);

blockType = find( blockTypes, { name: transformation.blockName } );
Copy link
Member

Choose a reason for hiding this comment

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

Noting that getBlockType is O(1) (object lookup by property) and this is O(n)

@danielbachhuber
Copy link
Member Author

@WordPress/gutenberg-core Suggestions on how to move forward with this? My preference is to not modify createBlock and getBlockTransforms, but I don't have a good idea on how to solve the broader global state problem.

I'll need to resolve this PR in order to move forward with #7030, #4456 and others.

@danielbachhuber danielbachhuber added [Status] Blocked Used to indicate that a current effort isn't able to move forward [Feature] Paste [Feature] Shortcodes Related to shortcode functionality labels Jul 5, 2018
@noisysocks
Copy link
Member

Could we call registerCoreBlocks() in the test? It looks like this is what we do to test BlockSwitcher in editor/components/block-switcher/test/index.js.

@gziolo
Copy link
Member

gziolo commented Jul 6, 2018

Could we call registerCoreBlocks() in the test? It looks like this is what we do to test BlockSwitcher in editor/components/block-switcher/test/index.js.

Assuming you want to use existing blocks, that would be the easiest way to go. Otherwise, you can also mock getBlockTypes to return whatever you need.

@danielbachhuber
Copy link
Member Author

Could we call registerCoreBlocks() in the test? It looks like this is what we do to test BlockSwitcher in editor/components/block-switcher/test/index.js.

🎉 That worked way better than I expected. Thanks @noisysocks !

Closing in favor of #7823

@danielbachhuber danielbachhuber deleted the shortcode-convert-test branch July 9, 2018 13:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Paste [Feature] Shortcodes Related to shortcode functionality [Status] Blocked Used to indicate that a current effort isn't able to move forward
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants