Miko.Testing 0.1.0

dotnet add package Miko.Testing --version 0.1.0
                    
NuGet\Install-Package Miko.Testing -Version 0.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Miko.Testing" Version="0.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Miko.Testing" Version="0.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Miko.Testing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Miko.Testing --version 0.1.0
                    
#r "nuget: Miko.Testing, 0.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Miko.Testing@0.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Miko.Testing&version=0.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Miko.Testing&version=0.1.0
                    
Install as a Cake Tool

Miko Testing

Testing framework for Miko - includes component testing, layout testing, and more utilities.

Overview

This testing framework provides utilities for unit testing Miko applications. Currently focused on Razor component testing (inspired by bUnit), with plans to expand into additional testing utilities for layout, styling, and other Miko features.

Current Features

  • Component testing - Unit test Razor components with full lifecycle support
  • DOM structure assertions - Verify the component's rendered HTML structure
  • Computed style assertions - Check applied styles and computed CSS properties
  • Layout assertions - Validate box model, dimensions, and positioning
  • Parameter assertions - Test component behavior with different parameters
  • Interaction testing - Simulate user interactions and state changes

Installation

Add the package to your test project (it is a preview; the API may change):

dotnet add package Miko.Testing --prerelease

Quick Start

Basic Test Setup

using Miko.Testing;
using Shouldly;
using Xunit;

public class MyComponentTests : IDisposable
{
    protected TestContext Context { get; }

    public MyComponentTests()
    {
        Context = new TestContext();
    }

    public void Dispose()
    {
        Context.Dispose();
    }
}

Rendering a Component

[Fact]
public void MyComponent_RendersCorrectly()
{
    // Act
    var cut = Context.Render<MyComponent>();

    // Assert
    cut.Root.TagName.ShouldBe("div");
    cut.Root.Class.ShouldBe("my-component");
}

Testing with Parameters

[Fact]
public void MyComponent_AppliesParameterCorrectly()
{
    // Act
    var cut = Context.Render<MyComponent>(parameters =>
        parameters.Add(nameof(MyComponent.Title), "Hello World"));

    // Assert
    cut.GetTextContent().ShouldContain("Hello World");
}

Testing Standards

The framework supports the standard component testing dimensions:

1. DOM Structure Testing

The DOM structure is part of the component contract. Test that the component renders the expected elements:

[Fact]
public void Component_HasCorrectDOMStructure()
{
    var cut = Context.Render<MyComponent>();

    cut.Root.TagName.ShouldBe("div");
    cut.Root.Children.Count.ShouldBe(2);
    cut.Root.Children[0].TagName.ShouldBe("header");
    cut.Root.Children[1].TagName.ShouldBe("section");
}

2. Parameter and State Testing

Test how components respond to parameters and state changes:

[Fact]
public void Component_UpdatesOnParameterChange()
{
    var cut1 = Context.Render<MyComponent>(p => p.Add("Active", false));
    cut1.Root.Class.ShouldBe("component");

    var cut2 = Context.Render<MyComponent>(p => p.Add("Active", true));
    cut2.Root.Class.ShouldBe("component active");
}

3. Key Style Testing

Test critical styles that affect component appearance:

[Fact]
public void Component_AppliesKeyStyles()
{
    var cut = Context.Render<MyComponent>();

    cut.Root.Style.Display.ShouldBe(Display.Flex);
    cut.Root.Style.FlexDirection.ShouldBe(FlexDirection.Column);
}

4. Box Model Testing (Layout Components)

For layout-critical components, test box model properties:

[Fact]
public void LayoutComponent_HasCorrectBoxModel()
{
    var cut = Context.Render<LayoutComponent>();

    var boxModel = cut.GetBoxModel(cut.Root);
    boxModel.ShouldNotBeNull();
    boxModel!.Content.Width.ShouldBeGreaterThan(0);
}

API Reference

TestContext

Main entry point for rendering components.

var context = new TestContext();

// Set viewport dimensions
context.ViewportWidth = 1024f;
context.ViewportHeight = 768f;

// Add stylesheets
context.AddStyleSheet(myStyleSheet);

// Render a component
var cut = context.Render<MyComponent>();

ComponentUnderTest

Result of rendering a component, provides access to DOM, styles, and layout.

// Access root element
Element root = cut.Root;

// Find elements
Element? element = cut.FindById("my-id");
List<Element> elements = cut.FindByClass("my-class");
List<Element> divs = cut.FindByTagName("div");

// Get computed styles
ComputedStyle? style = cut.GetComputedStyle(element);

// Get layout information
LayoutBox? layoutBox = cut.FindLayoutBox(element);
BoxModel? boxModel = cut.GetBoxModel(element);

// Get text content
string text = cut.GetTextContent();

// Get all elements
List<Element> allElements = cut.GetAllElements();

ComponentParameterBuilder

Builder for setting component parameters.

Context.Render<MyComponent>(parameters =>
{
    parameters.Add("Title", "My Title");
    parameters.Add("IsActive", true);
    parameters.AddChildContent(builder =>
    {
        builder.AddContent(0, "Child content");
    });
});

Extension Methods

// Fluent assertion extensions
cut.ShouldContainId("my-id");
cut.ShouldContainClass("my-class");
cut.ShouldContainTag("div");
cut.ShouldContainText("expected text");

Example: Ionic Component Tests

See tests/Miko.Ionic.Tests/ for complete examples of component tests.

[Fact]
public void IonButton_RendersWithCorrectSlot()
{
    var cut = Context.Render<IonButtons>(parameters =>
        parameters.Add(nameof(IonButtons.Slot), "end"));

    cut.Root.Class.ShouldBe("ion-buttons buttons-end");
}

Best Practices

  1. Test the contract, not the implementation: Focus on the component's public API (parameters, rendered output) rather than internal implementation details.

  2. Keep tests focused: Each test should verify one aspect of the component's behavior.

  3. Use meaningful test names: Test names should describe what is being tested and the expected outcome.

  4. Test edge cases: Include tests for boundary conditions, null values, and error cases.

  5. Don't over-test styles: Only test styles that are critical to the component's function. Don't test every CSS property.

  6. Use computed styles for assertions: When checking styles, use GetComputedStyle() to get the final resolved values.

Comparison with bUnit

This framework is inspired by bUnit but adapted for Miko's architecture:

Feature bUnit Miko.Testing
Component rendering ✓ ✓
Parameter passing ✓ ✓
DOM assertions ✓ ✓
Style assertions Limited ✓ Full access
Layout assertions ✗ ✓ Box model access
Event simulation ✓ Planned
JSInterop mocking ✓ N/A

Contributing

When adding new assertion helpers or testing utilities, follow these guidelines:

  1. Keep the API simple and fluent
  2. Provide clear error messages when assertions fail
  3. Add XML documentation for all public APIs
  4. Include examples in tests

License

MIT License - see LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net10.0

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.1.0 104 10/26/2026