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

make sure stepCount doesn't count false / null or undefined child items #106

Merged
merged 2 commits into from
Feb 15, 2022

Conversation

martinbroos
Copy link
Contributor

@martinbroos martinbroos commented Feb 14, 2022

When using optional steps in your Wizard the new stepCount property didn't match.
This is because React.Children.count() will also count the null or false values in jsx.

When converting to children toArray only valid JSX is allowed so we get the actual child count.

For more info on this: https://blog.agney.dev/react-children-count/

@devrnt
Copy link
Owner

devrnt commented Feb 15, 2022

Thanks for this PR, really appreciate it!

@devrnt devrnt merged commit 228b850 into devrnt:main Feb 15, 2022
@devrnt
Copy link
Owner

devrnt commented Feb 15, 2022

2.1.2

@ulken
Copy link

ulken commented Feb 25, 2022

Hah, just came to report this as I got hit by this behavior and had read up on the differences between .count() and .toArray(). When I implemented the feature, I wasn't aware of the behavior differences, as you can tell 😛

TIL 😀

Nice to see it's already been fixed — thank you!

@@ -37,6 +38,20 @@ describe('useWizard', () => {
expect(result.current.stepCount).toBe(2);
});

test('should set step count to one when using falsy step', () => {
Copy link

Choose a reason for hiding this comment

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

@devrnt Had a quick review and spot two things:

  1. Isn't the behavior tested twice now? An optional step is already added in renderUseWizardHook(). Hence, the test above ('should set step count to two') should already capture this?
  2. If you still prefer to have a separate (personally, I would just remove this test), more explicit test, then the name of the test doesn't reflect the implementation of the test.
    2.1 It should either say set step count to _two_ or one step should be removed.
    2.2 Extreme nitpick, but "falsy" isn't entirely true. E.g. "" and 0 would render. I would call it "non-rendered" or something.

Copy link
Owner

Choose a reason for hiding this comment

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

You're absolutely right, "falsy" is wrong here, don't know why I put it there :)

Anyway I changed the test case, if you're interested you can check it out

Copy link

Choose a reason for hiding this comment

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

All right, that's better 👍

I'd still argue the optional step isn't needed in the default render hook test wrapper, but no biggie.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants