src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/cli/modes/stub_test.go (about)

     1  package modes
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"src.elv.sh/pkg/cli"
     8  	. "src.elv.sh/pkg/cli/clitest"
     9  	"src.elv.sh/pkg/cli/term"
    10  	"src.elv.sh/pkg/cli/tk"
    11  )
    12  
    13  func TestStub_Rendering(t *testing.T) {
    14  	f := Setup()
    15  	defer f.Stop()
    16  
    17  	startStub(f.App, StubSpec{Name: " STUB "})
    18  	f.TestTTY(t,
    19  		"", term.DotHere, "\n",
    20  		" STUB ", Styles,
    21  		"******",
    22  	)
    23  }
    24  
    25  func TestStub_Handling(t *testing.T) {
    26  	f := Setup()
    27  	defer f.Stop()
    28  
    29  	bindingCalled := make(chan bool)
    30  	startStub(f.App, StubSpec{
    31  		Bindings: tk.MapBindings{
    32  			term.K('a'): func(tk.Widget) { bindingCalled <- true }},
    33  	})
    34  
    35  	f.TTY.Inject(term.K('a'))
    36  	select {
    37  	case <-bindingCalled:
    38  		// OK
    39  	case <-time.After(time.Second):
    40  		t.Errorf("Handler not called after 1s")
    41  	}
    42  }
    43  
    44  func startStub(app cli.App, spec StubSpec) {
    45  	w := NewStub(spec)
    46  	app.PushAddon(w)
    47  	app.Redraw()
    48  }