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

Create test utility to provide widget registrations #7264

Open
nfmohit opened this issue Jul 6, 2023 · 0 comments
Open

Create test utility to provide widget registrations #7264

nfmohit opened this issue Jul 6, 2023 · 0 comments
Labels
Type: Infrastructure Engineering infrastructure & tooling

Comments

@nfmohit
Copy link
Collaborator

nfmohit commented Jul 6, 2023

Feature Description

While working on #6265, we felt a necessity to provide widget registrations to the test registry. Thus, we had come up with something like the following:

/**
* Provides widget registration data to the given registry.
*
* @since n.e.x.t
*
* @param {Object} registry The registry to set up.
* @param {string} areaSlug Widget area slug.
* @param {string} areaTitle Widget area title.
* @param {string|Array} contextSlugs Widget context slug(s).
* @param {Object} widgets Object containing options mapped to widget slugs.
*/
export const provideWidgetRegistrations = (
registry,
areaSlug,
areaTitle,
contextSlugs = null,
widgets
) => {
registry.dispatch( CORE_WIDGETS ).registerWidgetArea( areaSlug, {
title: areaTitle,
} );
if ( contextSlugs ) {
registry
.dispatch( CORE_WIDGETS )
.assignWidgetArea( areaSlug, contextSlugs );
}
Object.keys( widgets ).forEach( ( slug ) => {
registry.dispatch( CORE_WIDGETS ).registerWidget( slug, {
Component: () => <div>Hello test.</div>,
...widgets[ slug ],
} );
registry.dispatch( CORE_WIDGETS ).assignWidget( slug, areaSlug );
} );
};

However, the above is a bit different than the convention shared by the other provide* utilities which all allow for easily providing something with sensible defaults that can be merged/overridden with customizations, but the utility itself should be useful without any (e.g. provideWidgetRegistrations( registry )).

This is a bit tricky for widgets since we don't support removing them (similar to modules). For a general widget utility like this, I'd imagine it would work very similar to the utility for module registrations (provideModuleRegistrations) which essentially iterates over all the core module JS-modules, and calls its registerModule export if it has one. This requires kind of decorating the module registration API call to allow for merging the extra data but the same should be possible here. We have the same kind of API for modules that have widgets as well via the registerWidgets export.

The difference with widgets is the hierarchical relationships between contexts, areas, and widgets which makes things a bit more complicated but still doable, however this is complicated and it seems out of scope to try and squeeze this much in at this point.

This issue is created with an aim to explore creating such an utility with the above considerations.

Read more here.


Do not alter or remove anything below. The following sections will be managed by moderators only.

Acceptance criteria

Implementation Brief

Test Coverage

QA Brief

Changelog entry

@mxbclang mxbclang added Type: Enhancement Improvement of an existing feature Type: Infrastructure Engineering infrastructure & tooling and removed Type: Enhancement Improvement of an existing feature labels Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Infrastructure Engineering infrastructure & tooling
Projects
None yet
Development

No branches or pull requests

2 participants