Unit Testing - Effective Tips With bUnit Testing Continued (Part 4)

Posted by Hon Lee on January 30, 2025
[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"));

}





.............



[Fact]

public void ShouldSaveScrollPosition()

{

// Arrange

var expectedScrollPosition = "100";

MenuDetails.scrollbarSessionState.scrollTop = expectedScrollPosition;

_sessionState.scrollTop = expectedScrollPosition;

// Act & Assert

Assert.Equal(expectedScrollPosition, _sessionState.scrollTop);

Assert.True(MenuDetails.scrollbarSessionState.scrollTop == _sessionState.scrollTop);

}

[Fact]

public async Task GetScreenWidth_ShouldReturnScreenWidth()

{

// Arrange

var expectedWidth = 1920;

_jsRuntime.InvokeAsync<int>("getScreenWidth").Returns(expectedWidth);

// Act

var result = await GetScreenWidth();

// Assert

Assert.Equal(expectedWidth, result);

}

private async Task<int> GetScreenWidth()

{

return await _jsRuntime.InvokeAsync<int>("getScreenWidth");

}





@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

Hey guys, going back to the unit testing, I am here to explain a useful piece.

Imagine you have a scroll bar and you are saving the scrollbar in a session state for the use of whenever the need to refresh the screen or clicking a button, you want to retain the scroll bar position (midway or wherever) when the page refreshes, you can do an assert to test this using a public property from a Razor component to test that it is saving the value and checking this against an expected sample value as seen here. This is so not only you are checking against the sample value but you are checking aginst the Razor component and the session state itself, so its a double assertion check at the end (its quite a simple neat check!)

var expectedScrollPosition = "100";

MenuDetails.scrollbarSessionState.scrollTop = expectedScrollPosition;

_sessionState.scrollTop = expectedScrollPosition;

// Act & Assert

Assert.Equal(expectedScrollPosition, _sessionState.scrollTop);

Assert.True(MenuDetails.scrollbarSessionState.scrollTop == _sessionState.scrollTop);

Further more if you have a private method that can't easily be called in the unit test where it is declared something like: -

private async Task<int> GetScreenWidth() => await _jsRuntime.InvokeAsync<int>("getScreenWidth");

You can actually invoke the runtime of getScreenWidth by using the below code, which the bUnit test can instantiate this using the statement JSInterop.Mode = JSRuntimeMode.Loose;

See the GetScreenWidth_ShouldReturnScreenWidth() in the code sample to see how it is done.

Hope this is useful!

At some point, I will form how each of these testing methods will work with a potential demo project to show how all these work!