site-kit-wp icon indicating copy to clipboard operation
site-kit-wp copied to clipboard

React console error appearing on module pages

Open johnPhillips opened this issue 5 years ago • 7 comments

Bug Description

It looks like this commit exposed an issue: https://github.com/google/site-kit-wp/commit/9baf1d0ba8ed7c4007a3bf29f355661192842d9e

Header now renders on every cycle (which is important), but this means that the ModuleIcon it contains is undefined while the getModuleIcon selector is resolving.

https://github.com/google/site-kit-wp/blob/9fcd85ae5db9313317a6bae808c42df40f8b65a7/assets/js/components/module/ModuleHeader.js#L41

This means that the following error is now appearing in the console on module pages:

react.development.js:167 Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined. You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.
Check the render method of `ModuleHeader`.
    in ModuleHeader (created by getModuleHeader)
    in getModuleHeader (created by WidgetContextRenderer)
    in div (created by Cell)
    in Cell (created by WidgetContextRenderer)
    in div (created by Row)
    in Row (created by WidgetContextRenderer)
    in div (created by Grid)
...

It seems like a possible fix might be to default ModuleIcon to Null:

+    import Null from '../../components/Null';
// ...
+    const ModuleIcon = useSelect( ( select ) => select( CORE_MODULES ).getModuleIcon( moduleSlug ) ) || Null;

Note: Care should be taken while addressing this issue to make sure no regressions occur on the page dashboard, since this was the reason for the changes in https://github.com/google/site-kit-wp/commit/9baf1d0ba8ed7c4007a3bf29f355661192842d9e in the first place (see issue https://github.com/google/site-kit-wp/issues/3493).

Steps to reproduce

  1. Enable the widgets.moduleScreens feature
  2. Browse to a module page, e.g. /wp-admin/admin.php?page=googlesitekit-module-analytics
  3. See the error in the console.

Additional Context

  • Plugin Version: 1.34.0

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

Acceptance criteria

  • No React errors or warnings should be displayed when rendering assets/js/components/module/ModuleHeader.js including the following cases:
    • Module does not exist for the given moduleSlug
    • Module has not been fully loaded/resolved yet

Implementation Brief

In assets/js/components/module/ModuleHeader.js,

  • Refactor the getModule call so that it returns the whole module object, rather than destructuring name straight away. If the returned module is undefined, return null early. This should catch scenarios where the module doesn't exist or has not fully loaded yet. If the module is defined, proceed to destructure name as before.
  • Conditionally include the ModuleIcon prop if it resolves. Not all modules are required to provide an icon, so one should only be included in the PageHeader if it exists / has loaded. This should fix the console error where React is trying to render an undefined component.

Test Coverage

  • None needed

Visual Regression Changes

  • VRT images are unlikely to need updating.

QA Brief

Set up

  • Enable the widgets.pageDashboard and widgets.moduleScreens features

Console error

  • Navigate to a module page, e.g. /admin.php?page=googlesitekit-module-search-console
  • Verify that the error no longer appears in the console

Non-existant modules / modules without a ModuleIcon

  • Edit ModuleApp.js so that the value of moduleSlug is hard-coded to something that doesn't exist, e.g. :
function ModuleApp(/* { moduleSlug } */) {	
+  const moduleSlug = 'non-existant';
  // ...
  • Visit a module page again and verify that nothing is rendered.
  • Now revert your changes and edit ModuleHeader so that ModuleIcon is undefined.
  • Visit a module page and verify that it renders correctly without a module icon by the title.

Check page dashboard for regressions

We need to check that there are no regressions for issue https://github.com/google/site-kit-wp/issues/3493

  • Visit a working page dashboard and verify there are no regressions.
  • Visit a page dashboard for a page that doesn't exist (e.g. /wp-admin/admin.php?page=googlesitekit-dashboard&permaLink=non-existant) and verify that there are no regressions.

Changelog entry

  • Fix a React console error when viewing widget-based module screens.

johnPhillips avatar Jun 11 '21 08:06 johnPhillips

@aaemnnosttv Can I just check my understanding of the reasoning behind this AC:

Module does not exist for the given moduleSlug

Was that to do with https://github.com/google/site-kit-wp/issues/3493? What scenario should I be covering here? Thanks.

johnPhillips avatar Jun 11 '21 09:06 johnPhillips

@johnPhillips it's more/less the same as unresolved but basically <ModuleHeader moduleSlug="non-existent" />.

aaemnnosttv avatar Jun 11 '21 09:06 aaemnnosttv

return null early if either name or ModuleIcon are undefined

@johnPhillips this is not quite correct as modules are not currently required to provide an Icon so we can't assume that the selector will return one (even though in our case for now it should). See here for an example.

I think we can reduce what needs to be done into the following:

  • Don't immediately destructure name from the returned module returned by getModule
    • If ! module return early (rather than checking for empty name)
  • Don't assume getModuleIcon will return a component (it's not required by all modules to have an icon) so maybe we just need to add a ModuleIcon && <ModuleIcon when passing the element to the icon prop of PageHeader

aaemnnosttv avatar Jun 11 '21 18:06 aaemnnosttv

I understand now - thanks @aaemnnosttv IB and PR updated 👍

johnPhillips avatar Jun 14 '21 08:06 johnPhillips

IB ✅

aaemnnosttv avatar Jun 14 '21 14:06 aaemnnosttv

@johnPhillips would you please add a QAB?

aaemnnosttv avatar Jun 15 '21 07:06 aaemnnosttv

** QA Eng ✅ ** Followed the QA brief and results were as expected.

Hazlitte avatar Jun 15 '21 13:06 Hazlitte