|
| 1 | +package dialog |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "zfs-file-history/internal/ui/table" |
| 6 | + |
| 7 | + "github.com/rivo/tview" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func TestComputeAvailableColumns(t *testing.T) { |
| 12 | + all := []*table.Column{ |
| 13 | + {Id: table.ColumnId(1), Title: "Col 1"}, |
| 14 | + {Id: table.ColumnId(2), Title: "Col 2"}, |
| 15 | + {Id: table.ColumnId(3), Title: "Col 3"}, |
| 16 | + } |
| 17 | + active := []*table.Column{ |
| 18 | + {Id: table.ColumnId(2), Title: "Col 2"}, |
| 19 | + } |
| 20 | + |
| 21 | + available := computeAvailableColumns(all, active) |
| 22 | + assert.Len(t, available, 2) |
| 23 | + assert.Equal(t, table.ColumnId(1), available[0].Id) |
| 24 | + assert.Equal(t, table.ColumnId(3), available[1].Id) |
| 25 | +} |
| 26 | + |
| 27 | +func TestColumnSelectionDialog(t *testing.T) { |
| 28 | + app := tview.NewApplication() |
| 29 | + all := []*table.Column{ |
| 30 | + {Id: table.ColumnId(1), Title: "Col 1"}, |
| 31 | + {Id: table.ColumnId(2), Title: "Col 2"}, |
| 32 | + } |
| 33 | + active := []*table.Column{ |
| 34 | + {Id: table.ColumnId(1), Title: "Col 1"}, |
| 35 | + } |
| 36 | + |
| 37 | + d := NewColumnSelectionDialog(app, "Column Selection", all, active, func(activeColumns []*table.Column) {}) |
| 38 | + assert.Equal(t, "ColumnSelectionDialog", d.GetName()) |
| 39 | + assert.NotNil(t, d.GetLayout()) |
| 40 | + assert.NotNil(t, d.GetActionChannel()) |
| 41 | +} |
| 42 | + |
| 43 | +func TestColumnSelectionDialog_Actions(t *testing.T) { |
| 44 | + app := tview.NewApplication() |
| 45 | + all := []*table.Column{ |
| 46 | + {Id: table.ColumnId(1), Title: "Col 1"}, |
| 47 | + {Id: table.ColumnId(2), Title: "Col 2"}, |
| 48 | + {Id: table.ColumnId(3), Title: "Col 3"}, |
| 49 | + } |
| 50 | + active := []*table.Column{ |
| 51 | + {Id: table.ColumnId(1), Title: "Col 1"}, |
| 52 | + {Id: table.ColumnId(2), Title: "Col 2"}, |
| 53 | + } |
| 54 | + |
| 55 | + changeCalled := false |
| 56 | + d := NewColumnSelectionDialog(app, "Column Selection", all, active, func(activeColumns []*table.Column) { |
| 57 | + changeCalled = true |
| 58 | + }) |
| 59 | + |
| 60 | + // Test Close |
| 61 | + go func() { |
| 62 | + d.Close() |
| 63 | + }() |
| 64 | + action := <-d.GetActionChannel() |
| 65 | + assert.Equal(t, DialogCloseActionId, action) |
| 66 | + |
| 67 | + // Test add selected available column |
| 68 | + d.addSelectedAvailableColumn() |
| 69 | + assert.True(t, changeCalled) |
| 70 | + assert.Len(t, d.activeColumns, 3) |
| 71 | + |
| 72 | + // Reset changeCalled |
| 73 | + changeCalled = false |
| 74 | + |
| 75 | + // Test remove selected active column |
| 76 | + d.activeTable.Select(1, 0) |
| 77 | + d.removeSelectedActiveColumn() |
| 78 | + assert.True(t, changeCalled) |
| 79 | + assert.Len(t, d.activeColumns, 2) |
| 80 | + |
| 81 | + // Reset changeCalled |
| 82 | + changeCalled = false |
| 83 | + |
| 84 | + // Test move active column up/down |
| 85 | + d.activeTable.Select(1, 0) |
| 86 | + d.moveActiveColumnUp() |
| 87 | + assert.True(t, changeCalled) |
| 88 | + |
| 89 | + changeCalled = false |
| 90 | + d.moveActiveColumnDown() |
| 91 | + assert.True(t, changeCalled) |
| 92 | +} |
0 commit comments