[Fact]
public void ShouldSetHasNavIconBeenClickedToTrue()
{
// Arrange
_renderedComponent = RenderComponent<Class>(parameters => parameters
.Add(p => p.HasNavIconBeenClicked, true));
// Act
var hasNavIconBeenClicked = _renderedComponent.Instance.HasNavIconBeenClicked;
// Assert
Assert.True(hasNavIconBeenClicked);
}
[Fact]
public async Task ShouldOpenNavMenuEventCallBack()
{
// Arrange
bool callbackInvoked = false;
var callback = EventCallback.Factory.Create(this, () => callbackInvoked = true);
_renderedComponent = RenderComponent<Class>(parameters => parameters
.Add(p => p.HasNavIconBeenClicked, true)
.Add(p => p.OpenNavMenuEventCallBack, callback));
// Act
await _renderedComponent.Instance.OpenNavMenuEventCallBack.InvokeAsync();
// Assert
Assert.True(callbackInvoked);
}
[Fact]
public void ShouldDismissEditWorkspaceIconButton()
{
// Arrange
var menuButton = _renderedComponent.Find("#MenuButton");
// Check Actions Menu button clickable in context
// Act
menuButton.Click();
// Assert
var editTitleComponent = _renderedComponent.Find("#EditWorkspaceTitleButton");
Assert.NotNull(editTitleComponent);
editTitleComponent.Click();
var dismissEditWorkspaceIconButton = _renderedComponent.Find("#DismissEditWorkspaceIcon");
Assert.NotNull(dismissEditWorkspaceIconButton);
}
[Fact]
public void ShouldDismissEditWorkspaceIconButton2()
{
// Arrange
var actionsMenuButton = _renderedComponent.Find("#ActionsMenuButton");
// Check Actions Menu button clickable in context
// Act
actionsMenuButton.Click();
// Assert
var editTitleComponent = _renderedComponent.Find("#EditWorkspaceTitleButton");
Assert.NotNull(editTitleComponent);
editTitleComponent.Click();
var dismissEditWorkspaceIconButton = _renderedComponent.Find("#DismissEditWorkspaceIcon");
Assert.NotNull(dismissEditWorkspaceIconButton);
dismissEditWorkspaceIconButton.Click();
// Verify the edit workspace popup is dismissed
Assert.Throws<ElementNotFoundException>(() => _renderedComponent.Find("#EditWorkspacePopup"));
}
.............
@code {
[Parameter]
public string? workareaTitle { get; set; }
[Parameter]
public string? UsersDefaultWorkAreaId { get; set; }
[Parameter]
public bool HasNavIconBeenClicked { get; set; }
[Parameter]
public EventCallback OpenNavMenuEventCallBack { get; set; }
[Parameter]
public EventCallback<string> OnDefaultWorkAreaChangedEventCallBack { get; set; }
[Parameter]
public EventCallback<string> OnWorkAreaTitleUpdateEventCallback { get; set; }
private string WorkAreaTitle = "New work area";
private bool shouldDisplayNavIcon = false;
private bool hasNavIconBeenClicked = false;
.............
protected override void OnParametersSet()
{
if (!string.IsNullOrEmpty(WorkAreaTitle))
{
workareaTitle = WorkAreaTitle;
}
hasNavIconBeenClicked = HasNavIconBeenClicked;
if (UsersDefaultWorkAreaId == _sessionState.WorkAreaId.ToString())
{
shouldShowMakeDefaultWorkAreaButton = false;
}
}
.............
}
bUnit Test Code
Something that can be useful when asserting a throw error and checking the error is handled therefore detecting in bUnit. (I stumbled upon the PluralSight on exception handing in unit testing)
In the same example code above, I have added ShouldDismissEditWorkspaceIconButton2() which is more effective than the ShouldDismissEditWorkspaceIconButton() test method for testing error handling.
In particular the code below does an assert throw and checks that the Exception ElementNotFoundException is picked up after trying to find the element #EditWorkspacePopup in the rendered component and is unable to find it.
Assert.Throws<ElementNotFoundException>(() => _renderedComponent.Find("#EditWorkspacePopup"));