| 1 |
package spinner |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"io" |
| 7 |
"reflect" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
"time" |
| 11 |
|
| 12 |
"github.com/charmbracelet/bubbles/spinner" |
| 13 |
tea "github.com/charmbracelet/bubbletea" |
| 14 |
"github.com/charmbracelet/lipgloss" |
| 15 |
) |
| 16 |
|
| 17 |
func TestNewSpinner(t *testing.T) { |
| 18 |
s := New() |
| 19 |
if s.title != "Loading..." { |
| 20 |
t.Errorf("Expected default title 'Loading...', got '%s'", s.title) |
| 21 |
} |
| 22 |
if !reflect.DeepEqual(s.spinner.Spinner, spinner.Dot) { |
| 23 |
t.Errorf("Expected default spinner type to be Dot, got %v", s.spinner.Spinner) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func TestSpinnerType(t *testing.T) { |
| 28 |
s := New().Type(Dots) |
| 29 |
if !reflect.DeepEqual(s.spinner.Spinner, spinner.Dot) { |
| 30 |
t.Errorf("Expected spinner type to be Dot, got %v", s.spinner.Spinner) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func TestSpinnerDifferentTypes(t *testing.T) { |
| 35 |
s := New().Type(Line) |
| 36 |
if !reflect.DeepEqual(s.spinner.Spinner, spinner.Line) { |
| 37 |
t.Errorf("Expected spinner type to be Line, got %v", s.spinner.Spinner) |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
func TestSpinnerView(t *testing.T) { |
| 42 |
s := New().Title("Test") |
| 43 |
view := s.View() |
| 44 |
|
| 45 |
if !strings.Contains(view, "Test") { |
| 46 |
t.Errorf("Expected view to contain title 'Test', got '%s'", view) |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
func TestSpinnerContextCancellation(t *testing.T) { |
| 51 |
exercise(t, func() *Spinner { |
| 52 |
ctx, cancel := context.WithCancel(context.Background()) |
| 53 |
s := New().Context(ctx) |
| 54 |
cancel() |
| 55 |
return s |
| 56 |
}, requireContextCanceled) |
| 57 |
} |
| 58 |
|
| 59 |
func TestSpinnerContextCancellationWhileRunning(t *testing.T) { |
| 60 |
exercise(t, func() *Spinner { |
| 61 |
ctx, cancel := context.WithCancel(context.Background()) |
| 62 |
go func() { |
| 63 |
time.Sleep(250 * time.Millisecond) |
| 64 |
cancel() |
| 65 |
}() |
| 66 |
return New().Context(ctx) |
| 67 |
}, requireContextCanceled) |
| 68 |
} |
| 69 |
|
| 70 |
func TestSpinnerStyleMethods(t *testing.T) { |
| 71 |
s := New() |
| 72 |
style := lipgloss.NewStyle().Foreground(lipgloss.Color("red")) |
| 73 |
titleStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("blue")) |
| 74 |
|
| 75 |
s.Style(style) |
| 76 |
s.TitleStyle(titleStyle) |
| 77 |
|
| 78 |
if !reflect.DeepEqual(s.spinner.Style, style) { |
| 79 |
t.Errorf("Style was not set correctly") |
| 80 |
} |
| 81 |
|
| 82 |
if !reflect.DeepEqual(s.titleStyle, titleStyle) { |
| 83 |
t.Errorf("TitleStyle was not set correctly") |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
func TestSpinnerInit(t *testing.T) { |
| 88 |
s := New() |
| 89 |
cmd := s.Init() |
| 90 |
|
| 91 |
if cmd == nil { |
| 92 |
t.Errorf("Init did not return a valid command") |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
func TestSpinnerUpdate(t *testing.T) { |
| 97 |
s := New() |
| 98 |
cmd := s.Init() |
| 99 |
if cmd == nil { |
| 100 |
t.Errorf("Init did not return a valid command") |
| 101 |
} |
| 102 |
|
| 103 |
model, cmd := s.Update(spinner.TickMsg{}) |
| 104 |
if reflect.TypeOf(model) != reflect.TypeOf(&Spinner{}) { |
| 105 |
t.Errorf("Update did not return correct model type") |
| 106 |
} |
| 107 |
|
| 108 |
if cmd == nil { |
| 109 |
t.Errorf("Update should return a non-nil command in this scenario") |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
_, cmd = s.Update(tea.KeyMsg{Type: tea.KeyCtrlC}) |
| 114 |
if cmd == nil { |
| 115 |
t.Errorf("Update did not handle key press correctly") |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
func TestSpinnerSimple(t *testing.T) { |
| 120 |
exercise(t, func() *Spinner { |
| 121 |
return New().Action(func() {}) |
| 122 |
}, requireNoError) |
| 123 |
} |
| 124 |
|
| 125 |
func TestSpinnerWithContextAndAction(t *testing.T) { |
| 126 |
exercise(t, func() *Spinner { |
| 127 |
ctx := context.Background() |
| 128 |
return New().Context(ctx).Action(func() {}) |
| 129 |
}, requireNoError) |
| 130 |
} |
| 131 |
|
| 132 |
func TestSpinnerWithActionError(t *testing.T) { |
| 133 |
fake := errors.New("fake") |
| 134 |
exercise(t, func() *Spinner { |
| 135 |
return New().ActionWithErr(func(context.Context) error { return fake }) |
| 136 |
}, requireErrorIs(fake)) |
| 137 |
} |
| 138 |
|
| 139 |
func exercise(t *testing.T, factory func() *Spinner, checker func(tb testing.TB, err error)) { |
| 140 |
t.Helper() |
| 141 |
t.Run("accessible", func(t *testing.T) { |
| 142 |
err := factory(). |
| 143 |
Accessible(true). |
| 144 |
Output(io.Discard). |
| 145 |
Run() |
| 146 |
checker(t, err) |
| 147 |
}) |
| 148 |
t.Run("regular", func(t *testing.T) { |
| 149 |
err := factory(). |
| 150 |
Accessible(false). |
| 151 |
Output(io.Discard). |
| 152 |
Run() |
| 153 |
checker(t, err) |
| 154 |
}) |
| 155 |
} |
| 156 |
|
| 157 |
func requireNoError(tb testing.TB, err error) { |
| 158 |
tb.Helper() |
| 159 |
if err != nil { |
| 160 |
tb.Errorf("expected no error, got %v", err) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
func requireErrorIs(target error) func(tb testing.TB, err error) { |
| 165 |
return func(tb testing.TB, err error) { |
| 166 |
tb.Helper() |
| 167 |
if !errors.Is(err, target) { |
| 168 |
tb.Errorf("expected error to be %v, got %v", target, err) |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
func requireContextCanceled(tb testing.TB, err error) { |
| 174 |
tb.Helper() |
| 175 |
switch { |
| 176 |
case errors.Is(err, context.Canceled): |
| 177 |
case errors.Is(err, tea.ErrProgramKilled): |
| 178 |
default: |
| 179 |
tb.Errorf("expected to get a context canceled error, got %v", err) |
| 180 |
} |
| 181 |
} |