Skip to content

Commit

Permalink
Fix table strings and add unit tests (#25) (#37)
Browse files Browse the repository at this point in the history
* Fix table strings and add unit tests



* update snapshots



* fix date string



---------


(cherry picked from commit c2519b8)

Signed-off-by: Chenyang Ji <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent dd80887 commit 1632706
Show file tree
Hide file tree
Showing 17 changed files with 4,362 additions and 84 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/cypress-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ jobs:

- name: Sleep until OSD server starts - non-windows
if: ${{ matrix.os != 'windows-latest' }}
run: sleep 450
run: sleep 500
shell: bash

- name: Install Cypress
Expand Down Expand Up @@ -134,7 +134,7 @@ jobs:
working-directory: OpenSearch-Dashboards/plugins/query-insights-dashboards
command: yarn run cypress run
wait-on: 'http://localhost:5601'
wait-on-timeout: 300
wait-on-timeout: 600
browser: chrome
env:
CYPRESS_CACHE_FOLDER: ${{ matrix.cypress_cache_folder }}
Expand Down
6 changes: 3 additions & 3 deletions common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ export const LATENCY = 'Latency';
export const CPU_TIME = 'CPU Time';
export const MEMORY_USAGE = 'Memory Usage';
export const INDICES = 'Indices';
export const SEARCH_TYPE = 'Search type';
export const NODE_ID = 'Coordinator node ID';
export const TOTAL_SHARDS = 'Total shards';
export const SEARCH_TYPE = 'Search Type';
export const NODE_ID = 'Coordinator Node ID';
export const TOTAL_SHARDS = 'Total Shards';
12 changes: 6 additions & 6 deletions cypress/e2e/2_query_details.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ describe('Top Queries Details Page', () => {
'CPU Time',
'Memory Usage',
'Indices',
'Search type',
'Coordinator node ID',
'Total shards',
'Search Type',
'Coordinator Node ID',
'Total Shards',
];
fieldLabels.forEach((label) => {
cy.get('.euiPanel').contains('h4', label).should('be.visible');
Expand Down Expand Up @@ -96,19 +96,19 @@ describe('Top Queries Details Page', () => {
// Validate Indices
cy.contains('h4', 'Indices').parent().next().invoke('text').should('not.be.empty');
// Validate Search Type
cy.contains('h4', 'Search type')
cy.contains('h4', 'Search Type')
.parent()
.next()
.invoke('text')
.should('equal', 'query then fetch');
// Validate Coordinator Node ID
cy.contains('h4', 'Coordinator node ID')
cy.contains('h4', 'Coordinator Node ID')
.parent()
.next()
.invoke('text')
.should('not.be.empty');
// Validate Total Shards
cy.contains('h4', 'Total shards')
cy.contains('h4', 'Total Shards')
.parent()
.next()
.invoke('text')
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@elastic/elastic-eslint-config-kibana": "link:../../packages/opensearch-eslint-config-opensearch-dashboards",
"@elastic/eslint-import-resolver-kibana": "link:../../packages/osd-eslint-import-resolver-opensearch-dashboards",
"@testing-library/dom": "^8.11.3",
"@testing-library/jest-dom": "^5.16.2",
"@testing-library/user-event": "^14.4.3",
"@types/react-dom": "^16.9.8",
"@types/object-hash": "^3.0.0",
Expand Down
24 changes: 12 additions & 12 deletions public/components/__snapshots__/app.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ exports[`<QueryInsightsDashboardsApp /> spec renders the component 1`] = `
>
<span
class="euiFilterButton__textShift"
data-text="Search type"
title="Search type"
data-text="Search Type"
title="Search Type"
>
Search type
Search Type
</span>
</span>
</span>
Expand Down Expand Up @@ -215,10 +215,10 @@ exports[`<QueryInsightsDashboardsApp /> spec renders the component 1`] = `
>
<span
class="euiFilterButton__textShift"
data-text="Coordinator node ID"
title="Coordinator node ID"
data-text="Coordinator Node ID"
title="Coordinator Node ID"
>
Coordinator node ID
Coordinator Node ID
</span>
</span>
</span>
Expand Down Expand Up @@ -566,9 +566,9 @@ exports[`<QueryInsightsDashboardsApp /> spec renders the component 1`] = `
>
<span
class="euiTableCellContent__text"
title="Search type"
title="Search Type"
>
Search type
Search Type
</span>
</span>
</button>
Expand All @@ -591,9 +591,9 @@ exports[`<QueryInsightsDashboardsApp /> spec renders the component 1`] = `
>
<span
class="euiTableCellContent__text"
title="Coordinator node ID"
title="Coordinator Node ID"
>
Coordinator node ID
Coordinator Node ID
</span>
</span>
</button>
Expand All @@ -616,9 +616,9 @@ exports[`<QueryInsightsDashboardsApp /> spec renders the component 1`] = `
>
<span
class="euiTableCellContent__text"
title="Total shards"
title="Total Shards"
>
Total shards
Total Shards
</span>
</span>
</button>
Expand Down
109 changes: 109 additions & 0 deletions public/pages/Configuration/Configuration.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import { MemoryRouter } from 'react-router-dom';
import Configuration from './Configuration';

const mockConfigInfo = jest.fn();
const mockCoreStart = {
chrome: {
setBreadcrumbs: jest.fn(),
},
};

const defaultLatencySettings = {
isEnabled: true,
currTopN: '5',
currWindowSize: '10',
currTimeUnit: 'MINUTES',
};
const defaultCpuSettings = {
isEnabled: false,
currTopN: '10',
currWindowSize: '1',
currTimeUnit: 'HOURS',
};
const defaultMemorySettings = {
isEnabled: false,
currTopN: '15',
currWindowSize: '2',
currTimeUnit: 'HOURS',
};

const renderConfiguration = (overrides = {}) =>
render(
<MemoryRouter>
<Configuration
latencySettings={{ ...defaultLatencySettings, ...overrides }}
cpuSettings={defaultCpuSettings}
memorySettings={defaultMemorySettings}
configInfo={mockConfigInfo}
core={mockCoreStart}
/>
</MemoryRouter>
);

const getWindowSizeConfigurations = () => screen.getAllByRole('combobox');
const getTopNSizeConfiguration = () => screen.getByRole('spinbutton');
const getEnableToggle = () => screen.getByRole('switch');

describe('Configuration Component', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('renders with default settings', () => {
const { container } = renderConfiguration();
expect(container).toMatchSnapshot('should match default settings snapshot');
});

// The following tests test the interactions on the frontend with Mocks.
it('updates state when toggling metrics and enables Save button when changes are made', () => {
const { container } = renderConfiguration();
// before toggling the metric
expect(getWindowSizeConfigurations()[0]).toHaveValue('latency');
expect(getEnableToggle()).toBeChecked();
// toggle the metric
fireEvent.change(getWindowSizeConfigurations()[0], { target: { value: 'cpu' } });
// after toggling the metric
expect(getWindowSizeConfigurations()[0]).toHaveValue('cpu');
// the enabled box should be disabled by default based on our configuration
const cpuEnableBox = getEnableToggle();
expect(cpuEnableBox).toBeInTheDocument();
expect(cpuEnableBox).not.toBeChecked();

fireEvent.click(getEnableToggle());
expect(getEnableToggle()).toBeChecked();
expect(screen.getByText('Save')).toBeEnabled();
expect(container).toMatchSnapshot('should match settings snapshot after toggling');
});

it('validates topNSize and windowSize inputs and disables Save button for invalid input', () => {
renderConfiguration();
fireEvent.change(getTopNSizeConfiguration(), { target: { value: '101' } });
expect(screen.queryByText('Save')).not.toBeInTheDocument();
fireEvent.change(getWindowSizeConfigurations()[1], { target: { value: '999' } });
expect(screen.queryByText('Save')).not.toBeInTheDocument();
});

it('calls configInfo and navigates on Save button click', async () => {
renderConfiguration();
fireEvent.change(getTopNSizeConfiguration(), { target: { value: '7' } });
fireEvent.click(screen.getByText('Save'));
await waitFor(() => {
expect(mockConfigInfo).toHaveBeenCalledWith(false, true, 'latency', '7', '10', 'MINUTES');
});
});

it('resets state on Cancel button click', async () => {
renderConfiguration();
fireEvent.change(getTopNSizeConfiguration(), { target: { value: '7' } });
fireEvent.click(screen.getByText('Cancel'));
expect(getTopNSizeConfiguration()).toHaveValue(5); // Resets to initial value
});
});
Loading

0 comments on commit 1632706

Please sign in to comment.