github.com/erda-project/erda-infra@v1.0.9/providers/component-protocol/protocol/render_test.go (about) 1 // Copyright (c) 2021 Terminus, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package protocol 16 17 import ( 18 "reflect" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "gopkg.in/yaml.v3" 23 24 "github.com/erda-project/erda-infra/providers/component-protocol/cptype" 25 ) 26 27 func Test_getCompNameAndInstanceName(t *testing.T) { 28 type args struct { 29 name string 30 } 31 tests := []struct { 32 name string 33 args args 34 wantCompName string 35 wantInstanceName string 36 }{ 37 { 38 name: "with @", 39 args: args{name: "mt_block_detail_item@mt_case_num_total"}, 40 wantCompName: "mt_block_detail_item", 41 wantInstanceName: "mt_case_num_total", 42 }, 43 { 44 name: "without @", 45 args: args{name: "mt_case_num_total"}, 46 wantCompName: "mt_case_num_total", 47 wantInstanceName: "mt_case_num_total", 48 }, 49 { 50 name: "with @@", 51 args: args{name: "a@@b"}, 52 wantCompName: "a", 53 wantInstanceName: "@b", 54 }, 55 } 56 for _, tt := range tests { 57 t.Run(tt.name, func(t *testing.T) { 58 gotCompName, gotInstanceName := getCompNameAndInstanceName(tt.args.name) 59 if gotCompName != tt.wantCompName { 60 t.Errorf("getCompNameAndInstanceName() gotCompName = %v, want %v", gotCompName, tt.wantCompName) 61 } 62 if gotInstanceName != tt.wantInstanceName { 63 t.Errorf("getCompNameAndInstanceName() gotInstanceName = %v, want %v", gotInstanceName, tt.wantInstanceName) 64 } 65 }) 66 } 67 } 68 69 func Test_calculateDefaultRenderOrderByHierarchy(t *testing.T) { 70 py := ` 71 hierarchy: 72 root: page 73 structure: 74 page: 75 - filter 76 - overview_group 77 - blocks 78 - mt_plan_chart_group 79 - leftHead 80 - executeHistory 81 overview_group: 82 - quality_chart 83 - blocks 84 blocks: 85 - mt_block 86 - at_block 87 mt_block: 88 - mt_block_header 89 - mt_block_detail 90 mt_block_header: 91 right: mt_block_header_filter 92 left: mt_block_header_title 93 mt_plan_chart_group: 94 children: 95 - mt_plan_chart2 96 - mt_plan_chart 97 extraContent: mt_plan_chart_filter 98 leftHead: 99 right: 100 - leftHeadAddSceneSet 101 - moreOperation 102 left: leftHeadTitle 103 tabBarExtraContent: 104 - tabSceneSetExecuteButton 105 executeHistory: 106 children: executeHistoryButton 107 content: executeHistoryPop 108 ` 109 var p cptype.ComponentProtocol 110 assert.NoError(t, yaml.Unmarshal([]byte(py), &p)) 111 orders, err := calculateDefaultRenderOrderByHierarchy(&p) 112 assert.NoError(t, err) 113 expected := []string{"page", "filter", "overview_group", "quality_chart", "blocks", "mt_block", 114 "mt_block_header", "mt_block_header_title", "mt_block_header_filter", 115 "mt_block_detail", "at_block", 116 "mt_plan_chart_group", "mt_plan_chart_filter", "mt_plan_chart2", "mt_plan_chart", 117 "leftHead", "leftHeadTitle", "leftHeadAddSceneSet", "moreOperation", "tabSceneSetExecuteButton", 118 "executeHistory", "executeHistoryPop", "executeHistoryButton", 119 } 120 assert.Equal(t, expected, orders) 121 } 122 123 func Test_slot_calculateDefaultRenderOrderByHierarchy(t *testing.T) { 124 py := ` 125 hierarchy: 126 root: myPage 127 structure: 128 myPage: 129 - pageHeader 130 - tabsTable 131 pageHeader: 132 slot: slotFilter 133 left: pipelineTabs 134 right: addPipelineBtn 135 tabsTable: 136 slot: filterContainer 137 table: pipelineTable 138 filterContainer: 139 left: customFilter 140 ` 141 var p cptype.ComponentProtocol 142 assert.NoError(t, yaml.Unmarshal([]byte(py), &p)) 143 orders, err := calculateDefaultRenderOrderByHierarchy(&p) 144 assert.NoError(t, err) 145 expected := []string{"myPage", 146 "pageHeader", "slotFilter", "pipelineTabs", "addPipelineBtn", 147 "tabsTable", 148 "filterContainer", "customFilter", 149 "pipelineTable", 150 } 151 assert.Equal(t, expected, orders) 152 } 153 154 func Test_recursiveWalkCompOrder(t *testing.T) { 155 // recursive walk from root 156 var result []string 157 allCompSubMap := make(map[string][]string) 158 allCompSubMap["page"] = []string{"title", "overview", "filter"} 159 allCompSubMap["overview"] = []string{"quality_chart", "blocks"} 160 err := recursiveWalkCompOrder("page", &result, allCompSubMap) 161 assert.NoError(t, err) 162 expected := []string{"page", "title", "overview", "quality_chart", "blocks", "filter"} 163 assert.Equal(t, expected, result) 164 } 165 166 func Test_getDefaultHierarchyRenderOrderFromCompExclude(t *testing.T) { 167 type args struct { 168 fullOrders []string 169 startFromCompExclude string 170 } 171 tests := []struct { 172 name string 173 args args 174 want []string 175 }{ 176 { 177 name: "not found, return nil", 178 args: args{ 179 fullOrders: []string{"root", "page", "overview", "split"}, 180 startFromCompExclude: "not", 181 }, 182 want: []string{}, 183 }, 184 { 185 name: "found in center", 186 args: args{ 187 fullOrders: []string{"root", "page", "overview", "split"}, 188 startFromCompExclude: "page", 189 }, 190 want: []string{"overview", "split"}, 191 }, 192 { 193 name: "found in the beginning", 194 args: args{ 195 fullOrders: []string{"root", "page", "overview", "split"}, 196 startFromCompExclude: "root", 197 }, 198 want: []string{"page", "overview", "split"}, 199 }, 200 { 201 name: "found in the last", 202 args: args{ 203 fullOrders: []string{"root", "page", "overview", "split"}, 204 startFromCompExclude: "split", 205 }, 206 want: []string{}, 207 }, 208 } 209 for _, tt := range tests { 210 t.Run(tt.name, func(t *testing.T) { 211 if got := getDefaultHierarchyRenderOrderFromCompExclude(tt.args.fullOrders, tt.args.startFromCompExclude); !reflect.DeepEqual(got, tt.want) { 212 t.Errorf("getDefaultHierarchyRenderOrderFromCompExclude() = %v, want %v", got, tt.want) 213 } 214 }) 215 } 216 }