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

feat(slider, range-slider): add pin feature (VIV-1611) #1736

Merged
merged 17 commits into from
Jul 24, 2024

Conversation

RichardHelm
Copy link
Contributor

No description provided.

Copy link

codecov bot commented Jun 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (d61b119) to head (a0ebb90).
Report is 1034 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff             @@
##              main     #1736     +/-   ##
===========================================
  Coverage   100.00%   100.00%             
===========================================
  Files          123       342    +219     
  Lines         1562      6195   +4633     
  Branches       108       772    +664     
===========================================
+ Hits          1562      6195   +4633     
Flag Coverage Δ
unittests 100.00% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

@YonatanKra YonatanKra left a comment

Choose a reason for hiding this comment

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

Nice!
Thanks for this split and work well done.
Because now I understand that popup is our "facade" for floatingUI it makes much more sense.
3 comments and we're done here :)

Comment on lines 291 to 301
it('should not continuously update position with requestAnimationFrame when false', async () => {
element.anchor = anchor;
await elementUpdated(element);
jest.spyOn(window, 'requestAnimationFrame');

element.open = true;

expect(window.requestAnimationFrame).toHaveBeenCalledTimes(
RAF_CALLS_FROM_SETTING_ATTRIBUTE
);
});
Copy link
Contributor

Choose a reason for hiding this comment

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

It is unclear to me what we are testing.
In the first test we call element.open = true; and it uses rAF once.
In the 2nd test, we call element.open = true;and it uses rAF twice.
Question is - why should it change only twice?
Looking at the floatingUI code it seems like what it does is keep updating the position as long as it keeps changing. Which seems to be the way things are going. So IMO the tests should be:

  • Mock rAF implementation to change the position on every call 3 or 4 times and then stabilize it
  • Show that rAF is called once when the prop is false and 3 or 4 times when the prop is true
  • Show that rAF is called once when the position doesn't change even when the prop is true
    WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With animationFrame it should continuously update the position with rAF.
After some thought, I think this test is kind of bad either way. Popup shouldn't really test how many times rAF is called or whether floatingUI works correctly.
All I really care about is that it passes the argument to floatingUI, which has it's own tests.
So maybe we should change it back to the original?

Copy link
Contributor

Choose a reason for hiding this comment

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

I do not believe so.
We do care about the amount of times rAF is called.
Floating UI is an implementation detail. You can implement this on your own or switch library - the tests should still pass :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, maybe, but unit tests shouldn't test dependencies for correctness like that.
I really don't care how many times rAF gets called, only that the flag gets passed.
If we replace floatingUI with something, this code needs updating which I'm fine with

Also, all the other tests in this file do not test floatingUI either. Either all them should or none

Copy link
Contributor

Choose a reason for hiding this comment

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

We are not testing dependencies for correctness. The fact you are using Floating UI is irrelevant. It is an implementation detail. We are testing the outcome of this - so if we removed floating UI or the corresponding code that implements this API, the test should fail.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've updated the test now to test that it continuously calls rAF. However the test is still bad and doesn't really test anything at all

If I change the test further to test that it actually updates position I end up testing floatingUI through popup...

Testing the outcome is not always what you want to do. E.g. when you have some code that sends an email, you don't want to test that it sends an actual email. Just that it calls some other code that is responsible for sending emails.

I'd argue that it's the same here. It's not popups job to calculate positions and keep them up to date, so we just verify that calls the code that is responsible for that

@@ -61,7 +93,7 @@ export class Popup extends FoundationElement {
open = false;
openChanged(_: boolean, newValue: boolean): void {
newValue ? this.$emit('vwc-popup:open') : this.$emit('vwc-popup:close');
this.#updateAutoUpdate();
DOM.queueUpdate(() => this.#updateAutoUpdate());
Copy link
Contributor

Choose a reason for hiding this comment

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

Now I see that the reason we have 1 rAF in the case animationFrame is false.
I gather a few things from this:

  1. If we delete the DOM.queueupdate and leave just the autoUpdate all tests are passing. It is either irrelevant or it should be tested in the open tests. should update position in the next animation frame or something like that.
  2. This explains why when we set animationFrame to false, we get one rAF call. So I'd might change the animationFrame property's name to something more meaningful like softPositionAnimation or smoothPositionAnimation or gradualPositionAnimation. WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, this change has nothing to do with animationFrame or its tests
If you delete it, the ui tests will fail. The reason is that it is wrong to call autoUpdate before open has changed in the the DOM, because floatingUI requires the floating element to be measurable. Which it isn't if open is false (display: none)

Copy link
Contributor

Choose a reason for hiding this comment

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

Then why not add a small unit test for it?
It should autoUpdate after open changed (e.g. after elementUpdated)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, I added some tests for open

* @internal
*/
animationFrameChanged() {
this.#updateAutoUpdate();
Copy link
Contributor

Choose a reason for hiding this comment

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

updateAutoUpdate is not a descriptive name ;)
Wouldn't updatePopupParmeters be more descriptive?
I'm open for other names as well :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Mhm, I don't think the name is that bad. Anyway, I would say that is out of scope of this PR

Copy link
Contributor

Choose a reason for hiding this comment

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

Well... it made me read the function in order to just understand what it does, which makes it a bad name (always take the weak link as your test case - and I'm the weak link here ;) ).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, maybe there is a better name but this review is already getting very big so I want to focus only the things actually in the PR

Copy link
Contributor

Choose a reason for hiding this comment

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

This review has only 4 comments ;)

Copy link
Contributor

@rachelbt rachelbt left a comment

Choose a reason for hiding this comment

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

I haven't assigned myself and saw that @YonatanKra reviewed. Adding only one comment

libs/components/src/lib/popup/README.md Show resolved Hide resolved
YonatanKra
YonatanKra previously approved these changes Jun 19, 2024
* feat(slider): add pin feature (VIV-1611)

* Update libs/components/src/lib/slider/slider.spec.ts

* Rename internals

* Add test for thumb mouse down

* Move dragging into thumb

* Refactor visible popup tests

* Add test for placement strategy

* Add test clicking on track while disabled

* Clarify code

* Refactor tests

* Split tests

* Add pin feature to range slider
@RichardHelm RichardHelm changed the title feat(popup): support placement strategy and animation frame options (VIV-1611) feat(slider, range-slider): add pin feature (VIV-1611) Jul 23, 2024
@RichardHelm RichardHelm merged commit 74944b4 into main Jul 24, 2024
15 checks passed
@RichardHelm RichardHelm deleted the VIV-1611-add-popup-options branch July 24, 2024 11:57
@github-actions github-actions bot mentioned this pull request Jul 24, 2024
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.

4 participants