|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +import React from 'react'; |
| 9 | +import { render, screen } from '@testing-library/react'; |
| 10 | +import { Map } from './map'; |
| 11 | +import type { SnapshotNode } from '../../../../../../common/http_api/snapshot_api'; |
| 12 | +import type { InfraWaffleMapOptions } from '../../../../../common/inventory/types'; |
| 13 | +import type { InventoryItemType } from '@kbn/metrics-data-access-plugin/common'; |
| 14 | +import { InfraFormatterType } from '@kbn/observability-plugin/common/custom_threshold_rule/types'; |
| 15 | + |
| 16 | +// Mock GroupOfNodes to simplify rendering |
| 17 | +jest.mock('./group_of_nodes', () => ({ |
| 18 | + GroupOfNodes: ({ group }: any) => { |
| 19 | + return ( |
| 20 | + <div data-test-subj="groupOfNodes"> |
| 21 | + {group.nodes.map((node: any) => ( |
| 22 | + <div key={node.id} data-test-subj="nodeContainer"> |
| 23 | + <span data-test-subj="nodeName">{node.name}</span> |
| 24 | + <span data-test-subj="nodeValue">{node.metrics?.[0]?.value || 0}</span> |
| 25 | + </div> |
| 26 | + ))} |
| 27 | + </div> |
| 28 | + ); |
| 29 | + }, |
| 30 | +})); |
| 31 | + |
| 32 | +const createMockNode = (name: string, value: number): SnapshotNode => ({ |
| 33 | + name, |
| 34 | + path: [{ value: name, label: name }], |
| 35 | + metrics: [ |
| 36 | + { |
| 37 | + name: 'cpu', |
| 38 | + value, |
| 39 | + avg: value, |
| 40 | + max: value, |
| 41 | + }, |
| 42 | + ], |
| 43 | +}); |
| 44 | + |
| 45 | +const hostNodes: SnapshotNode[] = [ |
| 46 | + createMockNode('host-1', 0.5), |
| 47 | + createMockNode('host-2', 0.7), |
| 48 | + createMockNode('host-3', 0.9), |
| 49 | + createMockNode('host-4', 0.3), |
| 50 | + createMockNode('host-5', 0.1), |
| 51 | +]; |
| 52 | + |
| 53 | +const defaultOptions: InfraWaffleMapOptions = { |
| 54 | + formatter: InfraFormatterType.percent, |
| 55 | + formatTemplate: '{{value}}', |
| 56 | + sort: { by: 'name', direction: 'asc' }, |
| 57 | + metric: { type: 'cpu' }, |
| 58 | + groupBy: [], |
| 59 | + legend: { |
| 60 | + type: 'gradient', |
| 61 | + rules: [], |
| 62 | + }, |
| 63 | +}; |
| 64 | + |
| 65 | +const defaultProps = { |
| 66 | + nodes: hostNodes, |
| 67 | + nodeType: 'host' as InventoryItemType, |
| 68 | + options: defaultOptions, |
| 69 | + formatter: (value: string | number) => `${value}`, |
| 70 | + currentTime: Date.now(), |
| 71 | + onFilter: jest.fn(), |
| 72 | + bounds: { min: 0, max: 1, legend: { min: 0, max: 1 } }, |
| 73 | + bottomMargin: 0, |
| 74 | + staticHeight: false, |
| 75 | + detailsItemId: null, |
| 76 | +}; |
| 77 | + |
| 78 | +describe('Map sorting', () => { |
| 79 | + beforeEach(() => { |
| 80 | + jest.clearAllMocks(); |
| 81 | + }); |
| 82 | + |
| 83 | + const getNodeNames = () => { |
| 84 | + const nodeContainers = screen.getAllByTestId('nodeContainer'); |
| 85 | + |
| 86 | + return nodeContainers.map((container) => { |
| 87 | + const nodeName = container.querySelector('[data-test-subj="nodeName"]'); |
| 88 | + return nodeName?.textContent || ''; |
| 89 | + }); |
| 90 | + }; |
| 91 | + |
| 92 | + const getNodeValues = () => { |
| 93 | + const nodeContainers = screen.getAllByTestId('nodeContainer'); |
| 94 | + |
| 95 | + return nodeContainers.map((container) => { |
| 96 | + const nodeValue = container.querySelector('[data-test-subj="nodeValue"]'); |
| 97 | + const valueText = nodeValue?.textContent || ''; |
| 98 | + return parseFloat(valueText); |
| 99 | + }); |
| 100 | + }; |
| 101 | + |
| 102 | + it('renders nodes in correct order when sorted by value descending', () => { |
| 103 | + const options: InfraWaffleMapOptions = { |
| 104 | + ...defaultOptions, |
| 105 | + sort: { by: 'value', direction: 'desc' }, |
| 106 | + }; |
| 107 | + |
| 108 | + render(<Map {...defaultProps} options={options} />); |
| 109 | + |
| 110 | + const nodeNames = getNodeNames(); |
| 111 | + const nodeValues = getNodeValues(); |
| 112 | + |
| 113 | + expect(nodeNames).toEqual(['host-3', 'host-2', 'host-1', 'host-4', 'host-5']); |
| 114 | + expect(nodeValues).toEqual([0.9, 0.7, 0.5, 0.3, 0.1]); |
| 115 | + }); |
| 116 | + |
| 117 | + it('renders nodes in correct order when sorted by value ascending', () => { |
| 118 | + const options: InfraWaffleMapOptions = { |
| 119 | + ...defaultOptions, |
| 120 | + sort: { by: 'value', direction: 'asc' }, |
| 121 | + }; |
| 122 | + |
| 123 | + render(<Map {...defaultProps} options={options} />); |
| 124 | + |
| 125 | + const nodeNames = getNodeNames(); |
| 126 | + const nodeValues = getNodeValues(); |
| 127 | + |
| 128 | + expect(nodeNames).toEqual(['host-5', 'host-4', 'host-1', 'host-2', 'host-3']); |
| 129 | + expect(nodeValues).toEqual([0.1, 0.3, 0.5, 0.7, 0.9]); |
| 130 | + }); |
| 131 | + |
| 132 | + it('updates node order when sort option changes from desc to asc', () => { |
| 133 | + const { rerender } = render(<Map {...defaultProps} />); |
| 134 | + |
| 135 | + let nodeNames = getNodeNames(); |
| 136 | + let nodeValues = getNodeValues(); |
| 137 | + |
| 138 | + expect(nodeNames).toEqual(['host-1', 'host-2', 'host-3', 'host-4', 'host-5']); |
| 139 | + expect(nodeValues).toEqual([0.5, 0.7, 0.9, 0.3, 0.1]); |
| 140 | + |
| 141 | + const descOptions: InfraWaffleMapOptions = { |
| 142 | + ...defaultOptions, |
| 143 | + sort: { by: 'value', direction: 'desc' }, |
| 144 | + }; |
| 145 | + rerender(<Map {...defaultProps} options={descOptions} />); |
| 146 | + |
| 147 | + nodeNames = getNodeNames(); |
| 148 | + nodeValues = getNodeValues(); |
| 149 | + |
| 150 | + expect(nodeNames).toEqual(['host-3', 'host-2', 'host-1', 'host-4', 'host-5']); |
| 151 | + expect(nodeValues).toEqual([0.9, 0.7, 0.5, 0.3, 0.1]); |
| 152 | + |
| 153 | + const ascOptions: InfraWaffleMapOptions = { |
| 154 | + ...defaultOptions, |
| 155 | + sort: { by: 'value', direction: 'asc' }, |
| 156 | + }; |
| 157 | + rerender(<Map {...defaultProps} options={ascOptions} />); |
| 158 | + |
| 159 | + nodeNames = getNodeNames(); |
| 160 | + nodeValues = getNodeValues(); |
| 161 | + |
| 162 | + expect(nodeNames).toEqual(['host-5', 'host-4', 'host-1', 'host-2', 'host-3']); |
| 163 | + expect(nodeValues).toEqual([0.1, 0.3, 0.5, 0.7, 0.9]); |
| 164 | + }); |
| 165 | +}); |
0 commit comments