inbox_test.go

 1package tui
 2
 3import (
 4	"testing"
 5	"time"
 6
 7	"github.com/andrinoff/email-cli/fetcher"
 8	tea "github.com/charmbracelet/bubbletea"
 9)
10
11// TestInboxUpdate verifies the state transitions in the inbox view.
12func TestInboxUpdate(t *testing.T) {
13	// Create a sample list of emails.
14	sampleEmails := []fetcher.Email{
15		{From: "a@example.com", Subject: "Email 1", Date: time.Now()},
16		{From: "b@example.com", Subject: "Email 2", Date: time.Now().Add(-time.Hour)},
17		{From: "c@example.com", Subject: "Email 3", Date: time.Now().Add(-2 * time.Hour)},
18	}
19
20	inbox := NewInbox(sampleEmails)
21
22	t.Run("Select email to view", func(t *testing.T) {
23		// By default, the first item is selected (index 0).
24		// Move down to the second item (index 1).
25		inbox.list, _ = inbox.list.Update(tea.KeyMsg{Type: tea.KeyDown})
26
27		// Simulate pressing Enter to view the selected email.
28		_, cmd := inbox.Update(tea.KeyMsg{Type: tea.KeyEnter})
29		if cmd == nil {
30			t.Fatal("Expected a command, but got nil.")
31		}
32
33		// Check the resulting message.
34		msg := cmd()
35		viewMsg, ok := msg.(ViewEmailMsg)
36		if !ok {
37			t.Fatalf("Expected a ViewEmailMsg, but got %T", msg)
38		}
39
40		// The index should match the selected item in the list.
41		expectedIndex := 1
42		if viewMsg.Index != expectedIndex {
43			t.Errorf("Expected index %d, but got %d", expectedIndex, viewMsg.Index)
44		}
45	})
46}