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

PanelBody refactored into functional component #23065

Closed
wants to merge 3 commits into from
Closed

PanelBody refactored into functional component #23065

wants to merge 3 commits into from

Conversation

jccit
Copy link

@jccit jccit commented Jun 10, 2020

Description

Related to #22890

How has this been tested?

I've tested the panels in the editor and in storybook and it seems to work fine. However the unit tests are failing. From what I can tell this seems to be because the test can't check the state if we're using React hooks

Types of changes

Refactored PanelBody into a functional component and used React hooks.

Checklist:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.
  • I've included developer documentation if appropriate.
  • I've updated all React Native files affected by any refactorings/renamings in this PR.

@jccit
Copy link
Author

jccit commented Jun 10, 2020

I've got 2 more tests to fix before this passes. Not sure how I can get the state from a functional component

@jccit jccit marked this pull request as ready for review June 10, 2020 18:11
@torounit
Copy link
Member

expect( panelBody.state( 'opened' ) ).toBe( true );

This line is a test of the variables inside the function.

@youknowriad I'd be happy to give me your idea.

@youknowriad
Copy link
Contributor

Ideally, we avoid testing the internal state, we can test the DOM output instead maybe?

@jccit
Copy link
Author

jccit commented Jun 11, 2020

I've added a new DOM attribute to expose the open state of the panel.

In the original tests we can test if the panel is open by seeing if the is-opened class has been applied. However 2 of the tests will check to see if the internal open state and the is-opened class are in sync or not, since you can override the opened state by using an opened prop.

Here's an example of a test where the internal state is false, but the component is forced open via a prop.

expect( panelBody.state( 'opened' ) ).toBe( false );
expect( panelBody.hasClass( 'is-opened' ) ).toBe( true );

panelBody now has data-openstate attached to it so I can test for it by using

panelBody.prop( 'data-openstate' )

I had to update some snapshots but this passes the tests now. I'd like some feedback on this approach since I'm not sure if this is the best way to do this

@jccit
Copy link
Author

jccit commented Jun 11, 2020

In hindsight a simpler approach is to remove the state check from the "opened" prop tests since the final state of the panel depends on the opened prop and not the state

@ZebulanStanphill ZebulanStanphill added [Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality labels Jun 14, 2020
Copy link
Member

@ZebulanStanphill ZebulanStanphill left a comment

Choose a reason for hiding this comment

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

Some minor stylistic nitpicks, but otherwise this looks good to go.

Comment on lines +25 to +30
initialOpen,
onToggle,
} ) {
const [ isOpenedState, setOpenedState ] = useState(
initialOpen === undefined ? true : initialOpen
);
Copy link
Member

Choose a reason for hiding this comment

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

This can be simplified using a default parameter.

Suggested change
initialOpen,
onToggle,
} ) {
const [ isOpenedState, setOpenedState ] = useState(
initialOpen === undefined ? true : initialOpen
);
initialOpen = true,
onToggle,
} ) {
const [ isOpenedState, setOpenedState ] = useState( initialOpen );

const [ isOpenedState, setOpenedState ] = useState(
initialOpen === undefined ? true : initialOpen
);
const isOpened = opened === undefined ? isOpenedState : opened;
Copy link
Member

Choose a reason for hiding this comment

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

This can be simplified using nullish coalescing:

Suggested change
const isOpened = opened === undefined ? isOpenedState : opened;
const isOpened = opened ?? isOpenedState;

Comment on lines +43 to 45
if ( onToggle ) {
onToggle();
}
Copy link
Member

Choose a reason for hiding this comment

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

This can be simplified using optional chaining:

Suggested change
if ( onToggle ) {
onToggle();
}
onToggle?.();

@ZebulanStanphill
Copy link
Member

(Also, it needs a rebase.)

@jccit
Copy link
Author

jccit commented Sep 21, 2020

Closing this, seems to have been superseded by #23327

@jccit jccit closed this Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Components /packages/components [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants