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

Fix Array Knob deserialization #2217

Merged
merged 6 commits into from
Nov 3, 2017

Conversation

rylnd
Copy link
Contributor

@rylnd rylnd commented Nov 2, 2017

Issue: Array Knob data retrieved from the URL is stored as an Object

What I did

Updated the Array knob's deserialize method to handle Objects.

How to test

Without these changes, if you reload the page with a component using the array knob and expecting an Array; you'll get the error

warning.js:33 Warning: Failed prop type: Invalid prop `knob.value` of type `object` supplied to `ArrayType`, expected `array`.

Is this testable with jest or storyshots?

Yes; unit tests have been added for Array's deserialize.

Does this need a new example in the kitchen sink apps?

No.

Does this need an update to the documentation?

No.

Kevin Altman and Ryland Herrick added 2 commits November 2, 2017 13:01
When an Array knob's value is serialized from the URL into the store, it
is an Object.

This ensures that when a user refreshes the page, the Array knob will
properly deserialize the Object into an Array.
@codecov
Copy link

codecov bot commented Nov 2, 2017

Codecov Report

Merging #2217 into master will increase coverage by 0.04%.
The diff coverage is 100%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2217      +/-   ##
==========================================
+ Coverage   21.44%   21.48%   +0.04%     
==========================================
  Files         263      263              
  Lines        5801     5804       +3     
  Branches      705      698       -7     
==========================================
+ Hits         1244     1247       +3     
- Misses       4016     4030      +14     
+ Partials      541      527      -14
Impacted Files Coverage Δ
addons/knobs/src/components/types/Array.js 84.61% <100%> (+4.61%) ⬆️
app/react/src/server/config/babel.js 0% <0%> (-100%) ⬇️
app/react/src/server/babel_config.js 0% <0%> (-77.42%) ⬇️
addons/info/src/components/Props.js 37.2% <0%> (ø) ⬆️
app/react/src/client/preview/element_check.js 41.17% <0%> (ø) ⬆️
lib/ui/src/modules/ui/containers/layout.js 12.5% <0%> (ø) ⬆️
lib/ui/src/modules/ui/libs/filters.js 47.36% <0%> (ø) ⬆️
lib/ui/src/modules/ui/libs/hierarchy.js 48.8% <0%> (ø) ⬆️
addons/knobs/src/components/types/Number.js 8.06% <0%> (ø) ⬆️
app/react-native/src/bin/storybook-start.js 0% <0%> (ø) ⬆️
... and 27 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b0b2344...09c597b. Read the comment docs.

@danielduan
Copy link
Member

Thanks for the PR and the test!

ArrayType.deserialize = value => {
if (Array.isArray(value)) return value;

return Object.keys(value).reduce((array, key) => [...array, value[key]], []);
Copy link
Member

@Hypnosphi Hypnosphi Nov 3, 2017

Choose a reason for hiding this comment

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

Does this rely on Object.keys being sorted? They are actually not guaranteed to be:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

The Object.keys() method returns an array of a given object's own enumerable properties, in the same order as that provided by a for...in loop

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in#Array_iteration_and_for...in

There is no guarantee that for...in will return the indexes in any particular order

Please sort the keys before reducing

Copy link
Contributor Author

@rylnd rylnd Nov 3, 2017

Choose a reason for hiding this comment

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

@Hypnosphi you are correct, good catch. Will amend now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hypnosphi and others added 4 commits November 3, 2017 03:34
Object.keys does not guarantee any particular order, which could result
in an out-of-order array if we did not sort said keys.
The remaining expectations implicitly check whether the objects are
arrays.
@Hypnosphi Hypnosphi added the bug label Nov 3, 2017
@Hypnosphi Hypnosphi self-assigned this Nov 3, 2017
@Hypnosphi Hypnosphi merged commit c4f56c9 into storybookjs:master Nov 3, 2017
@rylnd rylnd deleted the fix-array-deserialize branch November 30, 2017 09:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants