Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CCI] Fix type errors in Saved Object Management #3987

Closed
Closed
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Fix EUI/OUI type errors ([#3798](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3798))
- Remove unused Sass in `tile_map` plugin ([#4110](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/4110))
- [Table Visualization] Remove custom styling for text-align:center in favor of OUI utility class. ([#4164](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/4164))
- [Saved Object Management] Fix type errors ([#3987](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/3987))

### 🔩 Tests

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export async function getRelationships(
savedObjectTypes,
},
});
} catch (respError) {
} catch (respError: any) {
const respBody = get(respError, 'data', {}) as any;
Comment on lines +50 to 51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit - in general, prefer unknown to any.

const err = new Error(respBody.message || respBody.error || `${respError.status} Response`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('getQueryText', () => {
it('should know how to get the text out of the AST', () => {
const ast = {
getTermClauses: () => [{ value: 'foo' }, { value: 'bar' }],
getFieldClauses: (field) => {
getFieldClauses: (field: string) => {
if (field === 'type') {
return [{ value: 'lala' }, { value: 'lolo' }];
} else if (field === 'namespaces') {
Expand All @@ -43,7 +43,7 @@ describe('getQueryText', () => {
return [];
},
};
expect(parseQuery({ ast } as any, ['type'])).toEqual({
expect(parseQuery({ ast } as any)).toEqual({
queryText: 'foo bar',
visibleTypes: 'lala',
visibleNamespaces: 'default',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import { Query } from '@elastic/eui';
interface ParsedQuery {
queryText?: string;
visibleTypes?: string[];
visibleNamespaces?: string[];
}

export function parseQuery(query: Query): ParsedQuery {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import React from 'react';
import { shallowWithI18nProvider, mountWithIntl } from 'test_utils/enzyme_helpers';
import { OverwriteModalProps, OverwriteModal } from './overwrite_modal';
import { findTestSubject } from '@elastic/eui/lib/test';
import { findTestSubject } from 'test_utils/helpers';

describe('OverwriteModal', () => {
const obj = { type: 'foo', id: 'bar', meta: { title: 'baz' } };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,18 @@

import React from 'react';
import { shallowWithI18nProvider, mountWithI18nProvider } from 'test_utils/enzyme_helpers';
import { findTestSubject } from '@elastic/eui/lib/test';
import { findTestSubject } from 'test_utils/helpers';
import { keys } from '@elastic/eui';
import { httpServiceMock } from '../../../../../../core/public/mocks';
import { actionServiceMock } from '../../../services/action_service.mock';
import { columnServiceMock } from '../../../services/column_service.mock';
import { SavedObjectsManagementAction } from '../../..';
import { Table, TableProps } from './table';
import { namespaceServiceMock } from '../../../services/namespace_service.mock';

const defaultProps: TableProps = {
basePath: httpServiceMock.createSetupContract().basePath,
namespaceRegistry: namespaceServiceMock.createStart(),
actionRegistry: actionServiceMock.createStart(),
columnRegistry: columnServiceMock.createStart(),
selectedSavedObjects: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ import {
SavedObjectsManagementActionServiceStart,
SavedObjectsManagementAction,
SavedObjectsManagementColumnServiceStart,
SavedObjectsManagementNamespaceServiceStart,
} from '../../../services';

export interface TableProps {
Expand All @@ -77,12 +78,12 @@ export interface TableProps {
items: SavedObjectWithMetadata[];
itemId: string | (() => string);
totalItemCount: number;
onQueryChange: (query: any, filterFields: string[]) => void;
onQueryChange: (query: any, filterFields?: string[]) => void;
onTableChange: (table: any) => void;
isSearching: boolean;
onShowRelationships: (object: SavedObjectWithMetadata) => void;
canGoInApp: (obj: SavedObjectWithMetadata) => boolean;
dateFormat: string;
dateFormat?: string;
Copy link
Contributor Author

@Nicksqain Nicksqain May 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about the optional dateFormat props because it's not used in the defaultProps

 src\plugins\saved_objects_management\public\management_section\objects_table\components\table.test.tsx test 

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think making it optional is probably fine - moment will accept undefined as an input on L251. Alternatively you could just update the test defaultProps to include it, like in

}

interface TableState {
Expand Down Expand Up @@ -175,7 +176,6 @@ export class Table extends PureComponent<TableProps, TableState> {
basePath,
actionRegistry,
columnRegistry,
namespaceRegistry,
dateFormat,
} = this.props;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe('SavedObjectsTable', () => {
goInspectObject: () => {},
canGoInApp: () => true,
search,
dateFormat: 'YYYY-MM-DD',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you tell me what value the dateFormat should take? In

src\plugins\saved_objects_management\public\management_section\saved_objects_table_page.tsx 

the value is

const dateFormat = coreStart.uiSettings.get<string>('dateFormat');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. It's a little hard to tell, but to be consistent with the ui_settings test, I'd use Browser as the default test string:

const { defaults = { dateFormat: { value: 'Browser' } }, initialSettings = {} } = options;

};

findObjectsMock.mockImplementation(() => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
const exportAllOptions: ExportAllOption[] = [];
const exportAllSelectedOptions: Record<string, boolean> = {};

const filteredTypeCounts = filteredSavedObjectCounts.type || {};
const filteredTypeCounts = (filteredSavedObjectCounts.type || {}) as Record<string, number>;

Object.keys(filteredTypeCounts).forEach((id) => {
// Add this type as a bulk-export option.
Expand Down Expand Up @@ -802,7 +802,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
const selectionConfig = {
onSelectionChange: this.onSelectionChanged,
};
const typeCounts = savedObjectCounts.type || {};
const typeCounts = (savedObjectCounts.type || {}) as Record<string, number>;

const filterOptions = allowedTypes.map((type) => ({
value: type,
Expand All @@ -824,7 +824,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb

const availableNamespaces = namespaceRegistry.getAll() || [];
if (availableNamespaces.length) {
const nsCounts = savedObjectCounts.namespaces || {};
const nsCounts = (savedObjectCounts.namespaces || {}) as Record<string, number>;
const nsFilterOptions = availableNamespaces.map((ns) => {
return {
name: ns.name,
Expand Down Expand Up @@ -864,6 +864,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
basePath={http.basePath}
itemId={'id'}
actionRegistry={this.props.actionRegistry}
namespaceRegistry={this.props.namespaceRegistry}
columnRegistry={this.props.columnRegistry}
selectionConfig={selectionConfig}
selectedSavedObjects={selectedSavedObjects}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
ISavedObjectsManagementServiceRegistry,
SavedObjectsManagementActionServiceStart,
SavedObjectsManagementColumnServiceStart,
SavedObjectsManagementNamespaceServiceStart,
} from '../services';
import { SavedObjectsTable } from './objects_table';

Expand Down
3 changes: 3 additions & 0 deletions src/plugins/saved_objects_management/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@

import { actionServiceMock } from './services/action_service.mock';
import { columnServiceMock } from './services/column_service.mock';
import { namespaceServiceMock } from './services/namespace_service.mock';
import { serviceRegistryMock } from './services/service_registry.mock';
import { SavedObjectsManagementPluginSetup, SavedObjectsManagementPluginStart } from './plugin';

const createSetupContractMock = (): jest.Mocked<SavedObjectsManagementPluginSetup> => {
const mock = {
actions: actionServiceMock.createSetup(),
columns: columnServiceMock.createSetup(),
namespaces: namespaceServiceMock.createSetup(),
serviceRegistry: serviceRegistryMock.create(),
};
return mock;
Expand All @@ -46,6 +48,7 @@ const createStartContractMock = (): jest.Mocked<SavedObjectsManagementPluginStar
const mock = {
actions: actionServiceMock.createStart(),
columns: columnServiceMock.createStart(),
namespaces: namespaceServiceMock.createStart(),
};
return mock;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export interface SavedObjectsManagementNamespaceServiceStart {

export class SavedObjectsManagementNamespaceService {
private readonly namespaces = new Map<string, SavedObjectsManagementNamespace<unknown>>();
private readonly alias;
private alias: string = '';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the behavior a bit. Is there any error cases or edge case handling that needs to be changed to handle '' vs undefined as the default value?


setup(): SavedObjectsManagementNamespaceServiceSetup {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@

export { SavedObjectsManagementAction } from './action';
export { SavedObjectsManagementColumn } from './column';
export { SavedObjectsManagementNamespace } from './namespace';
export { SavedObjectsManagementRecord } from './record';
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* GitHub history for details.
*/

export interface SavedObjectsManagementNamespace {
export interface SavedObjectsManagementNamespace<T> {
id: string;
name: string;
}
3 changes: 3 additions & 0 deletions src/plugins/saved_objects_management/server/routes/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ export const registerFindRoute = (
...req.query,
fields: undefined,
searchFields: [...searchFields],
namespaces: req.query.namespaces
? ([] as string[]).concat(req.query.namespaces)
: undefined,
});

const savedObjects = await Promise.all(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ import { schema } from '@osd/config-schema';
import { IRouter, SavedObjectsFindOptions } from 'src/core/server';
import { findAll } from '../lib';

interface Counts {
type: Record<string, number>;
namespaces: Record<string, number>;
}

export const registerScrollForCountRoute = (router: IRouter) => {
router.post(
{
Expand All @@ -46,8 +51,9 @@ export const registerScrollForCountRoute = (router: IRouter) => {
},
router.handleLegacyErrors(async (context, req, res) => {
const { client } = context.core.savedObjects;
const counts = {
const counts: Counts = {
type: {},
namespaces: {},
};

const findOptions: SavedObjectsFindOptions = {
Expand Down