bunit mock'ing a select list selection

115 views Asked by At

I'm new to bUnit. In the normal run of things, I populate a dropdown and as long as a selection is not made, then when the user presses a cancel button, it will cancel, as soon as a user makes any kind of selection, then navigation manager kicks in to ensure you meant to cancel. So now I'm trying to write a test for this functionality.

so far I have:

var navMan = ctx.Services.GetRequiredService<FakeNavigationManager>();
var component = ctx.RenderComponent<NewBooking>();


var buttons = component.FindAll("button").ToList();
var button = buttons.Where(x => x.TextContent.Contains("Cancel")).First();

mock.When("api/Address/A1234").RespondJson(new AddressesViewModel { AddressList = new List<AddressDTO>
                                                             { new AddressDTO { AddressLine1 = "add1", AddressLine2 = "add2", Id = Guid.NewGuid(), CountryId = 4, AddressLine3 = "add3", AddressTypeId = 0  } },
                                                    ResponseMessage = new HttpResponseMessage(){ } });
                                                                       


 var dropdown = component.FindAll("select").ToList().First();


//Act
button.Click();

       
//Assert
var navigationHistory = navMan.History.Single();
Assert.AreEqual(NavigationState.Prevented, navigationHistory.State);

I know var dropdown is the correct component as I can see the placeholder text in there. the mock should be populating the dropdown with my dummy value, but it appears not to so because nothing was selected, my assertion fails as navigation was not prevented.

has anybody an example of how I should be mocking the data into the dropdown.

0

There are 0 answers