Skip to content

Various components: Avoid re-renderings from interaction events#8281

Merged
henon merged 19 commits intoMudBlazor:devfrom
danielchalmers:rerender
Mar 12, 2024
Merged

Various components: Avoid re-renderings from interaction events#8281
henon merged 19 commits intoMudBlazor:devfrom
danielchalmers:rerender

Conversation

@danielchalmers
Copy link
Member

@danielchalmers danielchalmers commented Mar 5, 2024

Description

Follows up on dotnet/aspnetcore#8203 by making click and touch events non-rendering for other key controls in order to work around the problem described in dotnet/aspnetcore#8151 and dotnet/maui#31944.

How Has This Been Tested?

Existing unit tests were used

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist:

  • The PR is submitted to the correct branch (dev).
  • My code follows the code style of this project.
  • I've added relevant tests.

@danielchalmers danielchalmers marked this pull request as draft March 5, 2024 04:29
@github-actions github-actions bot added bug Unexpected behavior or functionality not working as intended PR: needs review labels Mar 5, 2024
@danielchalmers
Copy link
Member Author

@ScarletKuro Thanks for identifying the fix for MudButton in dotnet/aspnetcore#8203! Seemed to do the trick as seen below:

Video2.mp4

This PR will expand that effort to other controls. I use MudIconButton and MudMenuItem specifically.

It hasn't been tested at all and pretty sure it doesn't compile, but I'm waiting to see if the original fix has no side effects, as you mentioned, and if you're interested in seeing the effort continued :)

danielchalmers added a commit to danielchalmers/JournalApp that referenced this pull request Mar 5, 2024
Issue described in https://github.com/dotnet/aspnetcore/issues/53863 and for MudBlazor at MudBlazor/MudBlazor#8151.

The MudBlazor part was remedied for MudButton with MudBlazor/MudBlazor#8203 in 6.17.0 but MudIconButton and MudMenuItem were not impacted and are awaiting MudBlazor/MudBlazor#8281
@henon
Copy link
Contributor

henon commented Mar 5, 2024

@mikes-gh I think you reviewed the commit which Daniel made on his own repo/app ;)

@danielchalmers
Copy link
Member Author

@ScarletKuro test is failing with:

A total of 1 test files matched the specified pattern.
  Failed AddAndRemoveEventListenerWhenChangingColorPickerView [45 ms]
  Error Message:
   Bunit.Rendering.UnknownEventHandlerIdException : There is no event handler with ID '12' associated with the 'onclick' event in the current render tree.

This can happen, for example, when using `cut.FindAll()`, and calling event trigger methods on the found elements after a re-render of the render tree. The workaround is to use re-issue the `cut.FindAll()` call after each render of a component, this ensures you have the latest version of the render tree and DOM tree available in your test code.

which could be from the icon buttons?

<MudPickerToolbar DisableToolbar="@DisableToolbar" Class="mud-picker-color-toolbar">
@if(PickerVariant != PickerVariant.Static)
{
<MudIconButton Class="pa-1 mud-close-picker-button" Size="Size.Small" Color="Color.Inherit" Icon="@CloseIcon" OnClick="@GetEventCallback()" />
}
<MudSpacer />
<MudIconButton Class="pa-1" Size="Size.Small" Color="GetButtonColor(ColorPickerView.Spectrum)" Icon="@SpectrumIcon" OnClick="(() => ChangeView(ColorPickerView.Spectrum))" />
<MudIconButton Class="pa-1 mx-1" Size="Size.Small" Color="GetButtonColor(ColorPickerView.Grid)" Icon="@GridIcon" OnClick="(() => ChangeView(ColorPickerView.Grid))" />
<MudIconButton Class="pa-1" Size="Size.Small" Color="GetButtonColor(ColorPickerView.Palette)" Icon="@PaletteIcon" OnClick="(() => ChangeView(ColorPickerView.Palette))" />
</MudPickerToolbar>

@henon
Copy link
Contributor

henon commented Mar 5, 2024

Yes, we unfortunately have a lot of these stale references in the tests.

@ScarletKuro
Copy link
Member

ScarletKuro commented Mar 5, 2024

Yes, this is why I had to do change the ToggleGroupTests.cs
I mentioned this issue here #8151 (reply in thread) and as @henon said its incorrectly made unit tests.
I believe you can use the same trick as I did with the func.

basically just dialing things back and staying focused on fixing key stale references; less proactive to avoid disrupting the codebase
@danielchalmers
Copy link
Member Author

danielchalmers commented Mar 5, 2024

@ScarletKuro @henon Should be ready except for the last failing test:

// MenuTests
public async Task OnClickErrorContentCaughtException()
{
    var comp = Context.RenderComponent<MenuErrorContenCaughtException>();
    await comp.FindAll("button.mud-button-root")[0].ClickAsync(new MouseEventArgs());
    comp.FindAll("div.mud-popover-open").Count.Should().Be(1);
    comp.FindAll("div.mud-list-item").Count.Should().Be(1);
    await comp.FindAll("div.mud-list-item")[0].ClickAsync(new MouseEventArgs());
    var mudAlert = comp.FindComponent<MudAlert>();
    var text = mudAlert.Find("div.mud-alert-message");
    text.InnerHtml.Should().Be("Oh my! We caught an error and handled it!");
}
@* MenuErrorContenCaughtException *@
@namespace MudBlazor.UnitTests.TestComponents
<ErrorBoundary>
    <ChildContent>
        <MudPopoverProvider></MudPopoverProvider>
        <MudMenu Label="Open Menu">
            <ChildContent>
                <MudMenuItem @onclick="@HandleClickAsync">Throw error</MudMenuItem>
            </ChildContent>
        </MudMenu>
        <br/>
    </ChildContent>
    <ErrorContent>
        <MudAlert Severity="Severity.Error">Oh my! We caught an error and handled it!</MudAlert>
    </ErrorContent>
</ErrorBoundary>

@code {
    public async Task HandleClickAsync()
    {
        await Task.Delay(100);
        throw new Exception("Something went wrong...");
    }
}
 OnClickErrorContentCaughtException
   Source: MenuTests.cs line 242
   Duration: 111 ms

  Message: 
Bunit.Rendering.ComponentNotFoundException : A component of type MudAlert was not found in the render tree.

I don't think it's capturing the exception properly due to the async click handler but I could be wrong. Adding a Comp.Render() before searching for the Alert will give the "Something went wrong..." exception instead.

@danielchalmers danielchalmers changed the title Fix more re-renderings Fix more re-renderings in components and stale references in tests Mar 5, 2024
mostly just the buttons

MenuTests.OnClickErrorContentCaughtException is the only one remaining
@danielchalmers danielchalmers changed the title Fix more re-renderings in components and stale references in tests Fix more re-renderings from component click events Mar 6, 2024
@danielchalmers danielchalmers marked this pull request as ready for review March 6, 2024 20:05
@ScarletKuro
Copy link
Member

I'm slightly confused why are we modifying the examples from Docs, they are not related to the Unit test area.
If it's only for testing purpose then they shouldn't be pushed to remote.
Otherwise I would refrain from bloating the docs, like the alert with button count has no value, same as the added snackbar to the badge click.

Maybe I'm wrong, @henon can correct me.

@ScarletKuro @henon Should be ready except for the last failing test:

I will try to look on weekends when I get time, for now I'm busy with other PRs.

@danielchalmers
Copy link
Member Author

danielchalmers commented Mar 6, 2024

Sure, no problem. Badge, Alert, FAB are gone. Chip, ListItem, and NavMenu remain - let me know which you want gone or all of them.

makes all changes to SelectTests obsolete and revertable

also fixes the elusive OnClickErrorContentCaughtException

trims down the PR big time. if necessary we can work on list item afterwards but it's better to get the majority through smoothly
@codecov
Copy link

codecov bot commented Mar 7, 2024

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes are missing coverage. Please review.

Project coverage is 88.64%. Comparing base (8d49119) to head (e1e590d).
Report is 23 commits behind head on dev.

Files Patch % Lines
src/MudBlazor/Components/Alert/MudAlert.razor.cs 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    dotnet/aspnetcore#8281      +/-   ##
==========================================
+ Coverage   88.54%   88.64%   +0.10%     
==========================================
  Files         400      407       +7     
  Lines       12086    12178      +92     
  Branches     2426     2430       +4     
==========================================
+ Hits        10701    10795      +94     
+ Misses        858      855       -3     
- Partials      527      528       +1     

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

@danielchalmers
Copy link
Member Author

danielchalmers commented Mar 7, 2024

List item was reverted because the expand toggle wasn't working. Could be looked into further but I might not need it for what I'm doing and seems to have wide-reaching consequences. Fixes the failing test and simplifies the PR through SelectTests not needing the changes made.

Overlay was reverted as well bc I'm not comfortable with the consequences of that considering a Render had to be added to the test to make it work. Similar for SwipeArea.


@ScarletKuro PR has been trimmed down and all tests are passing 🎉
Two docs added: NavMenuOnClickExample and ChipClickableExample; Will delete if that's preferred!

@danielchalmers danielchalmers changed the title Fix more re-renderings from component click events Avoid more re-renderings from interaction events Mar 7, 2024
danielchalmers added a commit to danielchalmers/JournalApp that referenced this pull request Mar 7, 2024
Issue described in https://github.com/dotnet/aspnetcore/issues/53863 and for MudBlazor at MudBlazor/MudBlazor#8151.

The MudBlazor part was remedied for MudButton with MudBlazor/MudBlazor#8203 in 6.17.0 but MudIconButton and MudMenuItem were not impacted and are awaiting MudBlazor/MudBlazor#8281
@henon
Copy link
Contributor

henon commented Mar 7, 2024

@danielchalmers can you please post screenshots of the new examples?

@danielchalmers
Copy link
Member Author

@danielchalmers can you please post screenshots of the new examples?

@henon

The chip has a distinct style when clickable so could be worthy of a showcase.

Video2.mp4

NavMenuLink now has an OnClick section like Link

Video2.mp4

You can see a bug in the video where if you click the About link while Settings is collapsed, Settings will open. This is not a regression.

@henon
Copy link
Contributor

henon commented Mar 7, 2024

OK, I think we can keep these examples.

@ScarletKuro
Copy link
Member

ScarletKuro commented Mar 12, 2024

Did some cosmetics, because this construction:

SomeType someName()=> returnRandomObject();

Is same if you did this:

SomeType someName()
{
    return returnRandomObject();
}

Therefore, the method / function name should start with capital letters (even if it's nested inside other function) as the style warring suggests:
name

Copy link
Member

@ScarletKuro ScarletKuro left a comment

Choose a reason for hiding this comment

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

@henon this is ready to merge.

@henon henon changed the title Avoid more re-renderings from interaction events Various components: Avoid re-renderings from interaction events Mar 12, 2024
@henon henon merged commit 6445d8f into MudBlazor:dev Mar 12, 2024
@henon
Copy link
Contributor

henon commented Mar 12, 2024

Thanks @danielchalmers

@danielchalmers danielchalmers deleted the rerender branch March 12, 2024 23:28
danielchalmers added a commit to danielchalmers/MudBlazor that referenced this pull request Mar 13, 2024
…lazor#8281)

* Fix extra re-render for more components
* Click docs
* Avoid stale element references in tests
---------

Co-authored-by: Artyom M <[email protected]>
danielchalmers added a commit to danielchalmers/MudBlazor that referenced this pull request Mar 13, 2024
…lazor#8281)

* Fix extra re-render for more components
* Click docs
* Avoid stale element references in tests
---------

Co-authored-by: Artyom M <[email protected]>
biegehydra pushed a commit to biegehydra/MudBlazor that referenced this pull request Apr 26, 2024
…lazor#8281)

* Fix extra re-render for more components
* Click docs
* Avoid stale element references in tests
---------

Co-authored-by: Artyom M <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Unexpected behavior or functionality not working as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants