btesting

package
v0.12.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package btesting provides testing utilities for BubblyUI composables. It includes helpers for creating test contexts, mocking composables, and asserting cleanup behavior.

Example usage:

func TestMyComposable(t *testing.T) {
    ctx := btesting.NewTestContext()

    // Test your composable
    state := MyComposable(ctx, "initial")
    assert.Equal(t, "initial", state.Get())

    // Test cleanup
    cleanup := MyComposableWithCleanup(ctx)
    btesting.AssertComposableCleanup(t, cleanup)
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertComposableCleanup

func AssertComposableCleanup(t *testing.T, cleanup func())

AssertComposableCleanup verifies that a cleanup function executes without panicking. It's a test helper that marks itself with t.Helper() for better error reporting.

The function:

  • Handles nil cleanup gracefully (no-op)
  • Catches panics and reports them as test errors
  • Marks itself as a helper for cleaner test output

Note: This helper only verifies that cleanup doesn't panic. Verifying that cleanup actually cleans up resources is test-specific and should be done in the individual test.

Example:

cleanup := UseEventListener(ctx, "click", handler)
btesting.AssertComposableCleanup(t, cleanup)

// Then verify handler no longer executes...

func MockComposable

func MockComposable[T any](ctx *bubbly.Context, value T) composables.UseStateReturn[T]

MockComposable creates a mock composable state for testing. It returns a UseStateReturn[T] that can be used in tests without calling the real UseState implementation.

This is useful for:

  • Testing composables that depend on other composables
  • Isolating the code under test
  • Providing controlled test data

Example:

ctx := btesting.NewTestContext()
mockUser := btesting.MockComposable(ctx, User{Name: "Alice"})

// Use mockUser in tests
assert.Equal(t, "Alice", mockUser.Get().Name)
mockUser.Set(User{Name: "Bob"})
assert.Equal(t, "Bob", mockUser.Get().Name)

func NewTestContext

func NewTestContext() *bubbly.Context

NewTestContext creates a minimal Context suitable for testing composables. The returned context supports all standard Context operations including:

  • Reactive primitives (Ref, Computed, Watch)
  • Event handling (On, Emit)
  • State management (Expose, Get)
  • Lifecycle hooks (OnMounted, OnUpdated, OnUnmounted)
  • Dependency injection (Provide, Inject)

Unlike a production component, the test context:

  • Has no Bubbletea integration
  • Has no template or rendering
  • Has no parent by default (use SetParent to establish relationships)

Example:

ctx := btesting.NewTestContext()
count := ctx.Ref(0)
ctx.OnMounted(func() {
    count.Set(10)
})
btesting.TriggerMount(ctx)
// count.Get() == 10

func SetParent

func SetParent(child, parent *bubbly.Context)

SetParent establishes a parent-child relationship between two test contexts. This is needed for testing provide/inject functionality across component trees.

Example:

parent := btesting.NewTestContext()
child := btesting.NewTestContext()
btesting.SetParent(child, parent)

parent.Provide("theme", "dark")
theme := child.Inject("theme", "light")
// theme == "dark"

func TriggerMount

func TriggerMount(ctx *bubbly.Context)

TriggerMount simulates the component mount lifecycle event for testing. This executes any onMounted hooks registered on the context.

Example:

ctx := btesting.NewTestContext()
mounted := false
ctx.OnMounted(func() { mounted = true })
btesting.TriggerMount(ctx)
// mounted == true

func TriggerUnmount

func TriggerUnmount(ctx *bubbly.Context)

TriggerUnmount simulates the component unmount lifecycle event for testing. This executes any onUnmounted hooks registered on the context.

Example:

ctx := btesting.NewTestContext()
unmounted := false
ctx.OnUnmounted(func() { unmounted = true })
btesting.TriggerUnmount(ctx)
// unmounted == true

func TriggerUpdate

func TriggerUpdate(ctx *bubbly.Context)

TriggerUpdate simulates the component update lifecycle event for testing. This executes any onUpdated hooks registered on the context.

Example:

ctx := btesting.NewTestContext()
updated := false
ctx.OnUpdated(func() { updated = true })
btesting.TriggerUpdate(ctx)
// updated == true

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL