github.com/oam-dev/kubevela@v1.9.11/references/cli/top/component/app_test.go (about)

     1  /*
     2  Copyright 2022 The KubeVela Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  	http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package component
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/gdamore/tcell/v2"
    23  	"github.com/stretchr/testify/assert"
    24  
    25  	"github.com/oam-dev/kubevela/references/cli/top/config"
    26  	"github.com/oam-dev/kubevela/references/cli/top/model"
    27  )
    28  
    29  var themeConfig = config.ThemeConfig{
    30  	Info: struct {
    31  		Title config.Color `yaml:"title"`
    32  		Text  config.Color `yaml:"text"`
    33  	}{
    34  		Title: "royalblue",
    35  		Text:  "lightgray",
    36  	},
    37  	Menu: struct {
    38  		Description config.Color `yaml:"description"`
    39  		Key         config.Color `yaml:"key"`
    40  	}{
    41  		Description: "gray",
    42  		Key:         "royalblue",
    43  	},
    44  	Logo: struct {
    45  		Text config.Color `yaml:"text"`
    46  	}{
    47  		Text: "royalblue",
    48  	},
    49  	Crumbs: struct {
    50  		Foreground config.Color `yaml:"foreground"`
    51  		Background config.Color `yaml:"background"`
    52  	}{
    53  		Foreground: "white",
    54  		Background: "royalblue",
    55  	},
    56  	Border: struct {
    57  		App   config.Color `yaml:"app"`
    58  		Table config.Color `yaml:"table"`
    59  	}{
    60  		App:   "black",
    61  		Table: "lightgray",
    62  	},
    63  	Table: struct {
    64  		Title    config.Color `yaml:"title"`
    65  		Header   config.Color `yaml:"header"`
    66  		Body     config.Color `yaml:"body"`
    67  		CursorBg config.Color `yaml:"cursorbg"`
    68  		CursorFg config.Color `yaml:"cursorfg"`
    69  	}{
    70  		Title:    "royalblue",
    71  		Header:   "white",
    72  		Body:     "blue",
    73  		CursorBg: "blue",
    74  		CursorFg: "black",
    75  	},
    76  	Status: struct {
    77  		Starting  config.Color `yaml:"starting"`
    78  		Healthy   config.Color `yaml:"healthy"`
    79  		UnHealthy config.Color `yaml:"unhealthy"`
    80  		Waiting   config.Color `yaml:"waiting"`
    81  		Succeeded config.Color `yaml:"succeeded"`
    82  		Failed    config.Color `yaml:"failed"`
    83  		Unknown   config.Color `yaml:"unknown"`
    84  	}{
    85  		Starting:  "blue",
    86  		Healthy:   "green",
    87  		UnHealthy: "red",
    88  		Waiting:   "yellow",
    89  		Succeeded: "orange",
    90  		Failed:    "purple",
    91  		Unknown:   "gray",
    92  	},
    93  	Yaml: struct {
    94  		Key   config.Color `yaml:"key"`
    95  		Colon config.Color `yaml:"colon"`
    96  		Value config.Color `yaml:"value"`
    97  	}{
    98  		Key:   "#d33582",
    99  		Colon: "lightgray",
   100  		Value: "#839495",
   101  	},
   102  	Topology: struct {
   103  		Line      config.Color `yaml:"line"`
   104  		App       config.Color `yaml:"app"`
   105  		Workflow  config.Color `yaml:"workflow"`
   106  		Component config.Color `yaml:"component"`
   107  		Policy    config.Color `yaml:"policy"`
   108  		Trait     config.Color `yaml:"trait"`
   109  		Kind      config.Color `yaml:"kind"`
   110  	}{
   111  		Line:      "cadetblue",
   112  		App:       "red",
   113  		Workflow:  "orange",
   114  		Component: "green",
   115  		Policy:    "yellow",
   116  		Trait:     "lightseagreen",
   117  		Kind:      "orange",
   118  	},
   119  }
   120  
   121  func TestApp(t *testing.T) {
   122  	app := NewApp(&themeConfig)
   123  	assert.Equal(t, len(app.actions), 0)
   124  	assert.Equal(t, len(app.Components()), 4)
   125  	t.Run("app init", func(t *testing.T) {
   126  		app.Init()
   127  		assert.NotEmpty(t, app.GetFocus())
   128  	})
   129  	t.Run("add action", func(t *testing.T) {
   130  		app.AddAction(model.KeyActions{
   131  			tcell.KeyEnter: model.KeyAction{
   132  				Description: "",
   133  				Action:      nil,
   134  				Visible:     false,
   135  				Shared:      false,
   136  			},
   137  		})
   138  		assert.Equal(t, len(app.actions), 1)
   139  	})
   140  	t.Run("delete action", func(t *testing.T) {
   141  		app.DelAction([]tcell.Key{tcell.KeyEnter})
   142  		assert.Equal(t, len(app.actions), 0)
   143  	})
   144  	t.Run("menu", func(t *testing.T) {
   145  		assert.NotEmpty(t, app.Menu())
   146  	})
   147  	t.Run("crumbs", func(t *testing.T) {
   148  		assert.NotEmpty(t, app.Crumbs())
   149  	})
   150  	t.Run("logo", func(t *testing.T) {
   151  		assert.NotEmpty(t, app.Logo())
   152  	})
   153  	t.Run("info board", func(t *testing.T) {
   154  		assert.NotEmpty(t, app.InfoBoard())
   155  	})
   156  }