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 ¶
- func AssertComposableCleanup(t *testing.T, cleanup func())
- func MockComposable[T any](ctx *bubbly.Context, value T) composables.UseStateReturn[T]
- func NewTestContext() *bubbly.Context
- func SetParent(child, parent *bubbly.Context)
- func TriggerMount(ctx *bubbly.Context)
- func TriggerUnmount(ctx *bubbly.Context)
- func TriggerUpdate(ctx *bubbly.Context)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AssertComposableCleanup ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.