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
UnitControl: rewrite tests in TypeScript #40697
Merged
Merged
Changes from 12 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8075dad
Rename file extensions
ciampo 67fae8b
Rewrite `ControlledSyncUnits` part
ciampo f6c9d62
Use diff snapshot to test classnames
ciampo 6ff8fd1
Use `toBeInTheDocument`
ciampo 46bb12e
Type internal test state setters
ciampo 70cdfee
Use `testing-library` semantic queries instead of `document.querySele…
ciampo 7d1eaec
Tweak optional parameter for some utilities
ciampo bcbf186
Add type guard to `hasUnits`
ciampo 8317e71
Fix TS errors in utils tests
ciampo 3c90c08
Add types to user-event wrapper function
ciampo 048222a
CHANGELOG
ciampo ecae33e
Add comment about failing test
ciampo 54379dc
ts-expect-error instead of ts-ignore
ciampo 8929d0b
Fix broken test by passing the missing `value`
ciampo a237da4
Add temporary stories to check single unit behavior
ciampo File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
packages/components/src/unit-control/test/__snapshots__/index.tsx.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`UnitControl Basic rendering should render custom className 1`] = ` | ||
Snapshot Diff: | ||
- First value | ||
+ Second value | ||
|
||
@@ -1,10 +1,10 @@ | ||
<div | ||
class="components-unit-control-wrapper css-aa2xc3-Root e1bagdl33" | ||
> | ||
<div | ||
- class="components-flex components-input-control components-number-control components-unit-control e1bagdl32 ep48uk90 em5sgkm7 css-18wzek1-View-Flex-sx-Base-sx-Items-ItemsColumn-Root-rootLabelPositionStyles-Input-ValueInput-arrowStyles-paddingStyles em57xhy0" | ||
+ class="components-flex components-input-control components-number-control components-unit-control hello e1bagdl32 ep48uk90 em5sgkm7 css-18wzek1-View-Flex-sx-Base-sx-Items-ItemsColumn-Root-rootLabelPositionStyles-Input-ValueInput-arrowStyles-paddingStyles em57xhy0" | ||
data-wp-c16t="true" | ||
data-wp-component="Flex" | ||
> | ||
<div | ||
class="components-flex-item em5sgkm3 css-1fmchc6-View-Item-sx-Base-LabelWrapper em57xhy0" | ||
@@ -15,11 +15,11 @@ | ||
class="components-input-control__container css-1sy20aj-Container-containerDisabledStyles-containerMarginStyles-containerWidthStyles em5sgkm6" | ||
> | ||
<input | ||
autocomplete="off" | ||
class="components-input-control__input css-gzm6eu-Input-dragStyles-fontSizeStyles-sizeStyles em5sgkm5" | ||
- id="inspector-input-control-1" | ||
+ id="inspector-input-control-2" | ||
inputmode="numeric" | ||
max="Infinity" | ||
min="-Infinity" | ||
step="1" | ||
type="number" | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently this line is failing in unit tests — that's because we switched from just checking that an element with class
components-unit-control__unit-label
exists, to actually looking for the unit label being displayed (testing-library
FTW!)I'm not sure if we should call this a bug, though. Technically we're not passing any
value
toUnitControl
, and therefore the component internally has notvalue
to parse a unit from. If we passedvalue="30%"
, then this test would pass.If our expectations are that
UnitControl
would display the%
unit, for how the component is coded right now, it would mean that effectively the selected unit is%
(which would also be reflected in the new components'svalue
and probably fired through thechange
callbacks). Would that be a correct behavior to expect? I'm personally not so sure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From how I'd interpret it, the purpose of this test is exactly what it says: "should render label if single units". The fact that it's rendered with no value defined is just an oversight, and I think the original PR confirms that (#33634).
I think we should just render it with
value="30%"
like you suggest.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I went ahead and did this in 8929d0b (which should fix the tests for now).
I still decided to look into this a bit more, and therefore I decided to add a couple of Storybook examples for this scenario:
UnitControl
with just one unit)UnitControl
is controlled, but its starting value isundefined
(still with just one unit)In the controlled case, the unit is not visible when
value
is set toundefined
(its starting value). The unit then becomes visible when a number is entered, and stays visible even if those numbers are deleted (likely because thevalue
becomes an empty string rather thanundefined
):unit-control-single-unit-controlled.mp4
In the uncontrolled case, the unit is never shown even after the user enters a
value
:unit-control-single-unit-uncontrolled.mp4
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, good investigation! I would expect that when there is only one unit, the unit would be visible at mount and stay visible no matter what. This kind of seems like a common use case so I'd say it's worth fixing if it isn't too hard. What do you think?