|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import React from 'react'; |
| 7 | +import { render, screen, fireEvent, waitFor } from '@testing-library/react'; |
| 8 | +import '@testing-library/jest-dom/extend-expect'; |
| 9 | +import { MemoryRouter } from 'react-router-dom'; |
| 10 | +import Configuration from './Configuration'; |
| 11 | + |
| 12 | +const mockConfigInfo = jest.fn(); |
| 13 | +const mockCoreStart = { |
| 14 | + chrome: { |
| 15 | + setBreadcrumbs: jest.fn(), |
| 16 | + }, |
| 17 | +}; |
| 18 | + |
| 19 | +const defaultLatencySettings = { |
| 20 | + isEnabled: true, |
| 21 | + currTopN: '5', |
| 22 | + currWindowSize: '10', |
| 23 | + currTimeUnit: 'MINUTES', |
| 24 | +}; |
| 25 | +const defaultCpuSettings = { |
| 26 | + isEnabled: false, |
| 27 | + currTopN: '10', |
| 28 | + currWindowSize: '1', |
| 29 | + currTimeUnit: 'HOURS', |
| 30 | +}; |
| 31 | +const defaultMemorySettings = { |
| 32 | + isEnabled: false, |
| 33 | + currTopN: '15', |
| 34 | + currWindowSize: '2', |
| 35 | + currTimeUnit: 'HOURS', |
| 36 | +}; |
| 37 | + |
| 38 | +const renderConfiguration = (overrides = {}) => |
| 39 | + render( |
| 40 | + <MemoryRouter> |
| 41 | + <Configuration |
| 42 | + latencySettings={{ ...defaultLatencySettings, ...overrides }} |
| 43 | + cpuSettings={defaultCpuSettings} |
| 44 | + memorySettings={defaultMemorySettings} |
| 45 | + configInfo={mockConfigInfo} |
| 46 | + core={mockCoreStart} |
| 47 | + /> |
| 48 | + </MemoryRouter> |
| 49 | + ); |
| 50 | + |
| 51 | +const getWindowSizeConfigurations = () => screen.getAllByRole('combobox'); |
| 52 | +const getTopNSizeConfiguration = () => screen.getByRole('spinbutton'); |
| 53 | +const getEnableToggle = () => screen.getByRole('switch'); |
| 54 | + |
| 55 | +describe('Configuration Component', () => { |
| 56 | + beforeEach(() => { |
| 57 | + jest.clearAllMocks(); |
| 58 | + }); |
| 59 | + |
| 60 | + it('renders with default settings', () => { |
| 61 | + const { container } = renderConfiguration(); |
| 62 | + expect(container).toMatchSnapshot('should match default settings snapshot'); |
| 63 | + }); |
| 64 | + |
| 65 | + // The following tests test the interactions on the frontend with Mocks. |
| 66 | + it('updates state when toggling metrics and enables Save button when changes are made', () => { |
| 67 | + const { container } = renderConfiguration(); |
| 68 | + // before toggling the metric |
| 69 | + expect(getWindowSizeConfigurations()[0]).toHaveValue('latency'); |
| 70 | + expect(getEnableToggle()).toBeChecked(); |
| 71 | + // toggle the metric |
| 72 | + fireEvent.change(getWindowSizeConfigurations()[0], { target: { value: 'cpu' } }); |
| 73 | + // after toggling the metric |
| 74 | + expect(getWindowSizeConfigurations()[0]).toHaveValue('cpu'); |
| 75 | + // the enabled box should be disabled by default based on our configuration |
| 76 | + const cpuEnableBox = getEnableToggle(); |
| 77 | + expect(cpuEnableBox).toBeInTheDocument(); |
| 78 | + expect(cpuEnableBox).not.toBeChecked(); |
| 79 | + |
| 80 | + fireEvent.click(getEnableToggle()); |
| 81 | + expect(getEnableToggle()).toBeChecked(); |
| 82 | + expect(screen.getByText('Save')).toBeEnabled(); |
| 83 | + expect(container).toMatchSnapshot('should match settings snapshot after toggling'); |
| 84 | + }); |
| 85 | + |
| 86 | + it('validates topNSize and windowSize inputs and disables Save button for invalid input', () => { |
| 87 | + renderConfiguration(); |
| 88 | + fireEvent.change(getTopNSizeConfiguration(), { target: { value: '101' } }); |
| 89 | + expect(screen.queryByText('Save')).not.toBeInTheDocument(); |
| 90 | + fireEvent.change(getWindowSizeConfigurations()[1], { target: { value: '999' } }); |
| 91 | + expect(screen.queryByText('Save')).not.toBeInTheDocument(); |
| 92 | + }); |
| 93 | + |
| 94 | + it('calls configInfo and navigates on Save button click', async () => { |
| 95 | + renderConfiguration(); |
| 96 | + fireEvent.change(getTopNSizeConfiguration(), { target: { value: '7' } }); |
| 97 | + fireEvent.click(screen.getByText('Save')); |
| 98 | + await waitFor(() => { |
| 99 | + expect(mockConfigInfo).toHaveBeenCalledWith(false, true, 'latency', '7', '10', 'MINUTES'); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + it('resets state on Cancel button click', async () => { |
| 104 | + renderConfiguration(); |
| 105 | + fireEvent.change(getTopNSizeConfiguration(), { target: { value: '7' } }); |
| 106 | + fireEvent.click(screen.getByText('Cancel')); |
| 107 | + expect(getTopNSizeConfiguration()).toHaveValue(5); // Resets to initial value |
| 108 | + }); |
| 109 | +}); |
0 commit comments