|
1 | | -import { useResourceCardRow } from '@shell/components/Resource/Detail/Card/StateCard/composables'; |
| 1 | +import { useResourceCardRow, useResourceCardRowFromSummary, useResourceCardRowFromRelationships } from '@shell/components/Resource/Detail/Card/StateCard/composables'; |
| 2 | +import type { SummaryResult } from '@shell/components/Resource/Detail/Card/StateCard/composables'; |
2 | 3 |
|
3 | 4 | describe('useResourceCardRow', () => { |
4 | 5 | describe('with default keys', () => { |
@@ -140,3 +141,195 @@ describe('useResourceCardRow', () => { |
140 | 141 | }); |
141 | 142 | }); |
142 | 143 | }); |
| 144 | + |
| 145 | +describe('useResourceCardRowFromSummary', () => { |
| 146 | + it('should return empty props when summaryResult is null', () => { |
| 147 | + const result = useResourceCardRowFromSummary('Services', null); |
| 148 | + |
| 149 | + expect(result.label).toBe('Services'); |
| 150 | + expect(result.color).toBeUndefined(); |
| 151 | + expect(result.counts).toBeUndefined(); |
| 152 | + }); |
| 153 | + |
| 154 | + it('should return empty props when summary array is empty', () => { |
| 155 | + const result = useResourceCardRowFromSummary('Services', { count: 0, summary: [] }); |
| 156 | + |
| 157 | + expect(result.color).toBeUndefined(); |
| 158 | + expect(result.counts).toBeUndefined(); |
| 159 | + }); |
| 160 | + |
| 161 | + it('should return empty props when summary is null', () => { |
| 162 | + const result = useResourceCardRowFromSummary('Services', { count: 5, summary: null }); |
| 163 | + |
| 164 | + expect(result.color).toBeUndefined(); |
| 165 | + expect(result.counts).toBeUndefined(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('should return a plain count when no metadata.state.name entry exists', () => { |
| 169 | + const summary: SummaryResult = { |
| 170 | + count: 3, |
| 171 | + summary: [{ property: 'some.other.field', counts: { foo: 3 } }] |
| 172 | + }; |
| 173 | + |
| 174 | + const result = useResourceCardRowFromSummary('Pods', summary); |
| 175 | + |
| 176 | + expect(result.counts).toStrictEqual([{ label: '', count: 3 }]); |
| 177 | + }); |
| 178 | + |
| 179 | + it('should map state names to colors and display labels', () => { |
| 180 | + const summary: SummaryResult = { |
| 181 | + count: 5, |
| 182 | + summary: [{ property: 'metadata.state.name', counts: { running: 3, error: 2 } }] |
| 183 | + }; |
| 184 | + |
| 185 | + const result = useResourceCardRowFromSummary('Pods', summary); |
| 186 | + |
| 187 | + expect(result.counts).toHaveLength(2); |
| 188 | + expect(result.counts).toContainEqual(expect.objectContaining({ |
| 189 | + label: 'running', count: 3, color: 'success' |
| 190 | + })); |
| 191 | + expect(result.counts).toContainEqual(expect.objectContaining({ |
| 192 | + label: 'error', count: 2, color: 'error' |
| 193 | + })); |
| 194 | + }); |
| 195 | + |
| 196 | + it('should set the highest alert color as main color', () => { |
| 197 | + const summary: SummaryResult = { |
| 198 | + count: 4, |
| 199 | + summary: [{ property: 'metadata.state.name', counts: { active: 3, error: 1 } }] |
| 200 | + }; |
| 201 | + |
| 202 | + const result = useResourceCardRowFromSummary('Services', summary); |
| 203 | + |
| 204 | + expect(result.color).toBe('error'); |
| 205 | + }); |
| 206 | + |
| 207 | + it('should sort by alert level then by count', () => { |
| 208 | + const summary: SummaryResult = { |
| 209 | + count: 6, |
| 210 | + summary: [{ |
| 211 | + property: 'metadata.state.name', |
| 212 | + counts: { |
| 213 | + active: 3, error: 1, warning: 2 |
| 214 | + } |
| 215 | + }] |
| 216 | + }; |
| 217 | + |
| 218 | + const result = useResourceCardRowFromSummary('Pods', summary); |
| 219 | + |
| 220 | + expect(result.counts![0].color).toBe('error'); |
| 221 | + expect(result.counts![1].color).toBe('warning'); |
| 222 | + expect(result.counts![2].color).toBe('success'); |
| 223 | + }); |
| 224 | + |
| 225 | + it('should pass the to parameter through', () => { |
| 226 | + const to = { hash: '#pods' }; |
| 227 | + const result = useResourceCardRowFromSummary('Pods', null, to); |
| 228 | + |
| 229 | + expect(result.to).toStrictEqual(to); |
| 230 | + }); |
| 231 | + |
| 232 | + it('should handle a single state', () => { |
| 233 | + const summary: SummaryResult = { |
| 234 | + count: 2, |
| 235 | + summary: [{ property: 'metadata.state.name', counts: { active: 2 } }] |
| 236 | + }; |
| 237 | + |
| 238 | + const result = useResourceCardRowFromSummary('Services', summary); |
| 239 | + |
| 240 | + expect(result.counts).toHaveLength(1); |
| 241 | + expect(result.counts![0]).toStrictEqual(expect.objectContaining({ |
| 242 | + label: 'active', count: 2, color: 'success' |
| 243 | + })); |
| 244 | + expect(result.color).toBe('success'); |
| 245 | + }); |
| 246 | +}); |
| 247 | + |
| 248 | +describe('useResourceCardRowFromRelationships', () => { |
| 249 | + it('should return empty props for empty relationships', () => { |
| 250 | + const result = useResourceCardRowFromRelationships('Refers to', []); |
| 251 | + |
| 252 | + expect(result.label).toBe('Refers to'); |
| 253 | + expect(result.color).toBeUndefined(); |
| 254 | + expect(result.counts).toBeUndefined(); |
| 255 | + }); |
| 256 | + |
| 257 | + it('should aggregate relationship states', () => { |
| 258 | + const rels = [ |
| 259 | + { toType: 'configmap', state: 'active' }, |
| 260 | + { toType: 'secret', state: 'active' }, |
| 261 | + { toType: 'serviceaccount', state: 'error' } |
| 262 | + ]; |
| 263 | + |
| 264 | + const result = useResourceCardRowFromRelationships('Refers to', rels); |
| 265 | + |
| 266 | + expect(result.counts).toHaveLength(2); |
| 267 | + expect(result.counts).toContainEqual(expect.objectContaining({ label: 'active', count: 2 })); |
| 268 | + expect(result.counts).toContainEqual(expect.objectContaining({ label: 'error', count: 1 })); |
| 269 | + }); |
| 270 | + |
| 271 | + it('should default missing state to "missing"', () => { |
| 272 | + const rels = [ |
| 273 | + { toType: 'configmap' }, |
| 274 | + { toType: 'secret', state: 'active' } |
| 275 | + ]; |
| 276 | + |
| 277 | + const result = useResourceCardRowFromRelationships('Refers to', rels); |
| 278 | + |
| 279 | + expect(result.counts).toContainEqual(expect.objectContaining({ |
| 280 | + label: 'missing', count: 1, color: 'warning' |
| 281 | + })); |
| 282 | + expect(result.counts).toContainEqual(expect.objectContaining({ |
| 283 | + label: 'active', count: 1, color: 'success' |
| 284 | + })); |
| 285 | + }); |
| 286 | + |
| 287 | + it('should set the highest alert color as main color', () => { |
| 288 | + const rels = [ |
| 289 | + { toType: 'configmap', state: 'active' }, |
| 290 | + { toType: 'secret', state: 'error' } |
| 291 | + ]; |
| 292 | + |
| 293 | + const result = useResourceCardRowFromRelationships('Refers to', rels); |
| 294 | + |
| 295 | + expect(result.color).toBe('error'); |
| 296 | + }); |
| 297 | + |
| 298 | + it('should sort by alert level then by count', () => { |
| 299 | + const rels = [ |
| 300 | + { toType: 'a', state: 'active' }, |
| 301 | + { toType: 'b', state: 'active' }, |
| 302 | + { toType: 'c', state: 'active' }, |
| 303 | + { toType: 'd', state: 'error' }, |
| 304 | + { toType: 'e', state: 'warning' }, |
| 305 | + { toType: 'f', state: 'warning' } |
| 306 | + ]; |
| 307 | + |
| 308 | + const result = useResourceCardRowFromRelationships('Refers to', rels); |
| 309 | + |
| 310 | + expect(result.counts![0].color).toBe('error'); |
| 311 | + expect(result.counts![1].color).toBe('warning'); |
| 312 | + expect(result.counts![2].color).toBe('success'); |
| 313 | + }); |
| 314 | + |
| 315 | + it('should pass the to parameter through', () => { |
| 316 | + const to = { hash: '#related' }; |
| 317 | + const result = useResourceCardRowFromRelationships('Refers to', [], to); |
| 318 | + |
| 319 | + expect(result.to).toStrictEqual(to); |
| 320 | + }); |
| 321 | + |
| 322 | + it('should handle all relationships having no state', () => { |
| 323 | + const rels = [ |
| 324 | + { toType: 'configmap' }, |
| 325 | + { toType: 'secret' } |
| 326 | + ]; |
| 327 | + |
| 328 | + const result = useResourceCardRowFromRelationships('Refers to', rels); |
| 329 | + |
| 330 | + expect(result.counts).toHaveLength(1); |
| 331 | + expect(result.counts![0]).toStrictEqual(expect.objectContaining({ |
| 332 | + label: 'missing', count: 2, color: 'warning' |
| 333 | + })); |
| 334 | + }); |
| 335 | +}); |
0 commit comments