-
I have a test and test component. I am trying to Click a button in the test component, but it does not seem to be clicking the instance in the component: Test/Code Here: [Test]
public async Task RowsPerPageChangeValueFromCode()
{
var testComponent = Context.RenderComponent<TablePagerChangeRowsPerPageTest>();
var table = testComponent.FindComponent<MudTable<string>>().Instance;
var buttonComponent = testComponent.FindComponent<MudButton>();
//try to force false, has no effect
buttonComponent.SetParametersAndRender(parameters => parameters.Add(p => p.Disabled, false));
var x = buttonComponent.Instance.Disabled;
testComponent.WaitForAssertion(() => table.RowsPerPage.Should().Be(35));
testComponent.Find("button").Click();//disabled=true still. Why?
testComponent.WaitForAssertion(() => table.RowsPerPage.Should().Be(10));//test fails because value is not toggled
testComponent.Find("button").Click();
testComponent.WaitForAssertion(() => table.RowsPerPage.Should().Be(35));
} Component and razor.cs |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Sorry for the lack of response. I'll try to take a look at it tomorrow. |
Beta Was this translation helpful? Give feedback.
-
I played around with the test, and here is a few observations:
My go to when debugging something like this is to investigate For reference, here is the test as it looks currently on my machine: public void RowsPerPageChangeValueFromCode()
{
var testComponent = RenderComponent<TablePagerChangeRowsPerPageTest>();
var table = testComponent.FindComponent<MudTable<string>>().Instance;
var buttonComponent = testComponent.FindComponent<MudBaseButton>();
var allButtonComponents = testComponent.FindComponents<MudBaseButton>();
var btnElm1 = buttonComponent.Find("button");
var allButtonElms = testComponent.FindAll("button");
var disabledButtons = allButtonElms.Select(x => (x, x.HasAttribute("disabled"))).ToArray();
buttonComponent.SetParametersAndRender(parameters => parameters.Add(p => p.Disabled, false));
table.RowsPerPage.Should().Be(35);
var btnElm2 = testComponent.Find("button");
btnElm2.Click();
table.RowsPerPage.Should().Be(10);
btnElm2.Click();
table.RowsPerPage.Should().Be(35);
} The test doesn't pass, but I hope this helps you get further and figure it out. Ps. this took a good hour to debug, so if you feel this is worth something to you, do consider hitting that sponsor button. |
Beta Was this translation helpful? Give feedback.
-
@egil Thanks! This makes complete sense. I made a few subtle tweaks and now the test is passing: Perform the find on the I changed the test to this: [Test]
public async Task RowsPerPageChangeValueFromCode()
{
var testComponent = Context.RenderComponent<TablePagerChangeRowsPerPageTest>();
var table = testComponent.FindComponent<MudTable<string>>().Instance;
var buttonComponent = testComponent.FindComponent<MudButton>();
testComponent.WaitForAssertion(() => table.RowsPerPage.Should().Be(35));
//Toggle the rows per page value from 35 to 10
buttonComponent.Find("button").Click(); <-search for button element inside the button component! :)
testComponent.WaitForAssertion(() => table.RowsPerPage.Should().Be(10));
//Toggle the rows per page value from 10 back to to 35
buttonComponent.Find("button").Click();
testComponent.WaitForAssertion(() => table.RowsPerPage.Should().Be(35));
} |
Beta Was this translation helpful? Give feedback.
I played around with the test, and here is a few observations:
testComponent.Markup
after the first render reveals that there is five<button>
elements in the markup.Disabed
parameter on,buttonComponent
, doesn't contain the same<button>
element as the one you find when you dotestComponent.Find("button")
, so changing it doesn't change the button you expect. That is why the button you are trying to click is still disabled.testComponent.FindComponents<MudButton>();
show that there is one<MudButton>
in the render tree.testComponent.FindAll("button")
we see there are five buttons, and the two first in the DOM tree is disabled.