-
Is there a way to change the option of HTML select element using bUnit? I'm testing the following component:
The test looks like:
I can't get it to work... |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 6 replies
-
Hi @dawidr There is. The trick is to use the right event trigger method (https://bunit.egilhansen.com/docs/interaction/trigger-event-handlers) that comes with bUnit. For select elements, I am pretty sure the one you need is public class PageSizeTests: TestContext
{
[Fact]
public void ComponentShouldChangePageSize()
{
int pageSize = 0;
var cut = RenderComponent<PageSize>(parameters => parameters
.Add(p => p.SizeChanged, selectedPageSize => pageSize = selectedPageSize)
);
cut.Find("#pageSize").Change(10);
Assert.Equal(10, pageSize);
}
} I made a few changes to your example:
Hope this helps. |
Beta Was this translation helpful? Give feedback.
-
@egil Thanks for a quick response! As far as I understand there is no difference between:
and your code:
I'm I, right? Besides this, your example also doesn't work for me. Take a look, the |
Beta Was this translation helpful? Give feedback.
-
OK, I can confirm that this works: CUT: <select id="pageSize" @bind="Size">
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
@code
{
private int _size = 10;
[Parameter]
public int Size
{
get
{
return _size;
}
set
{
if (_size != value)
{
_size = value;
SizeChanged.InvokeAsync(value);
}
}
}
[Parameter]
public EventCallback<int> SizeChanged { get; set; }
} Test: [Fact]
public void ComponentShouldChangePageSize()
{
int actual = 0;
int expected = 50;
var cut = RenderComponent<Selector>(parameters => parameters
.Add(p => p.Size, 20)
.Add(p => p.SizeChanged, selectedPageSize => actual = selectedPageSize)
);
cut.Find("#pageSize").Change(expected.ToString());
Assert.Equal(expected, cut.Instance.Size);
Assert.Equal(expected, actual);
} The key is unfortunately to convert expected to a string before passing it to |
Beta Was this translation helpful? Give feedback.
OK, I can confirm that this works:
CUT:
Test: