Skip to content

Commit

Permalink
fix(console): reset compdef when editing (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
meganwolf0 authored Oct 3, 2024
1 parent 02101a5 commit 4e25f01
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
11 changes: 7 additions & 4 deletions src/internal/testhelpers/testhelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ func CreateTempFile(t *testing.T, ext string) *os.File {
}

// RunTestModelView runs a test model view with a given model and messages, impelements a retry loop if final model is nil
func RunTestModelView(t *testing.T, m tea.Model, msgs []tea.Msg, timeout time.Duration, maxRetries, height, width int) error {
func RunTestModelView(t *testing.T, m tea.Model, reset func() tea.Model, msgs []tea.Msg, timeout time.Duration, maxRetries, height, width int) error {

testModelView := func(t *testing.T) (bool, error) {
testModelView := func(t *testing.T, try int) (bool, error) {
tm := teatest.NewTestModel(t, m, teatest.WithInitialTermSize(width, height))

for _, msg := range msgs {
tm.Send(msg)
time.Sleep(time.Millisecond * 50)
time.Sleep(time.Millisecond * time.Duration(50*try))
}

if err := tm.Quit(); err != nil {
Expand All @@ -63,8 +63,11 @@ func RunTestModelView(t *testing.T, m tea.Model, msgs []tea.Msg, timeout time.Du
}

for i := 0; i < maxRetries; i++ {
retry, err := testModelView(t)
retry, err := testModelView(t, i+1)
if retry {
if reset != nil {
m = reset()
}
continue
}
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestAssessmentResultsBasicView(t *testing.T) {

msgs := []tea.Msg{}

err := testhelpers.RunTestModelView(t, model, msgs, timeout, maxRetries, height, width)
err := testhelpers.RunTestModelView(t, model, nil, msgs, timeout, maxRetries, height, width)
if err != nil {
t.Fatal(err)
}
Expand Down
15 changes: 11 additions & 4 deletions src/internal/tui/component/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestComponentDefinitionBasicView(t *testing.T) {

msgs := []tea.Msg{}

err := testhelpers.RunTestModelView(t, model, msgs, timeout, maxRetries, height, width)
err := testhelpers.RunTestModelView(t, model, nil, msgs, timeout, maxRetries, height, width)
if err != nil {
t.Fatal(err)
}
Expand All @@ -61,7 +61,7 @@ func TestComponentDefinitionComponentSwitch(t *testing.T) {
tea.KeyMsg{Type: tea.KeyEnter}, // Open control
}

err := testhelpers.RunTestModelView(t, model, msgs, timeout, maxRetries, height, width)
err := testhelpers.RunTestModelView(t, model, nil, msgs, timeout, maxRetries, height, width)
if err != nil {
t.Fatal(err)
}
Expand All @@ -81,7 +81,7 @@ func TestComponentControlSelect(t *testing.T) {
tea.KeyMsg{Type: tea.KeyEnter}, // Open control
}

err := testhelpers.RunTestModelView(t, model, msgs, timeout, maxRetries, height, width)
err := testhelpers.RunTestModelView(t, model, nil, msgs, timeout, maxRetries, height, width)
if err != nil {
t.Fatal(err)
}
Expand All @@ -105,7 +105,14 @@ func TestEditViewComponentDefinitionModel(t *testing.T) {
tea.KeyMsg{Type: tea.KeyEnter}, // Confirm edit
}

err := testhelpers.RunTestModelView(t, model, msgs, timeout, maxRetries, height, width)
reset := func() tea.Model {
resetOscalModel := testhelpers.OscalFromPath(t, validCompDefValidations)
resetModel := component.NewComponentDefinitionModel(resetOscalModel.ComponentDefinition)
resetModel.Open(height, width)
return resetModel
}

err := testhelpers.RunTestModelView(t, model, reset, msgs, timeout, maxRetries, height, width)
if err != nil {
t.Fatal(err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/internal/tui/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestNewOSCALModel(t *testing.T) {

msgs := []tea.Msg{}

err := testhelpers.RunTestModelView(t, model, msgs, timeout, maxRetries, height, width)
err := testhelpers.RunTestModelView(t, model, nil, msgs, timeout, maxRetries, height, width)
if err != nil {
t.Fatal(err)
}
Expand Down

0 comments on commit 4e25f01

Please sign in to comment.