Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: tea.Batch returns nil if all cmds are nil #217

Merged
merged 1 commit into from Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions commands_test.go
Expand Up @@ -80,3 +80,28 @@ func TestSequentially(t *testing.T) {
})
}
}

func TestBatch(t *testing.T) {
t.Run("nil cmd", func(t *testing.T) {
if b := Batch(nil); b != nil {
t.Fatalf("expected nil, got %+v", b)
}
})
t.Run("empty cmd", func(t *testing.T) {
if b := Batch(); b != nil {
t.Fatalf("expected nil, got %+v", b)
}
})
t.Run("single cmd", func(t *testing.T) {
b := Batch(Quit)()
if l := len(b.(batchMsg)); l != 1 {
t.Fatalf("expected a []Cmd with len 1, got %d", l)
}
})
t.Run("mixed nil cmds", func(t *testing.T) {
b := Batch(nil, Quit, nil, Quit, nil, nil)()
if l := len(b.(batchMsg)); l != 2 {
t.Fatalf("expected a []Cmd with len 2, got %d", l)
}
})
}
11 changes: 9 additions & 2 deletions tea.go
Expand Up @@ -119,11 +119,18 @@ type Program struct {
// }
//
func Batch(cmds ...Cmd) Cmd {
if len(cmds) == 0 {
var validCmds []Cmd
for _, c := range cmds {
if c == nil {
continue
}
validCmds = append(validCmds, c)
}
if len(validCmds) == 0 {
return nil
}
return func() Msg {
return batchMsg(cmds)
return batchMsg(validCmds)
}
}

Expand Down