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

[Papercut][Dashboard][Data Discovery] Add description to saved object finder table if applicable #198816

Merged
merged 21 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/plugins/saved_objects_finder/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ export type SavedObjectCommon<T extends FinderAttributes = FinderAttributes> = S
export interface FinderAttributes {
title?: string;
name?: string;
description?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ describe('SavedObjectsFinder', () => {
const doc = {
id: '1',
type: 'search',
attributes: { title: 'Example title' },
attributes: { title: 'Example title', description: 'example description' },
};

const doc2 = {
id: '2',
type: 'search',
attributes: { title: 'Another title' },
attributes: { title: 'Another title', description: 'another description' },
};

const doc3 = { type: 'vis', id: '3', attributes: { title: 'Vis' } };

const doc4 = { type: 'search', id: '4', attributes: { title: 'Search' } };

const searchMetaData = [
{
type: 'search',
Expand Down Expand Up @@ -217,6 +219,29 @@ describe('SavedObjectsFinder', () => {
</React.Fragment>
`);
});

it('render description if provided', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

In this test you are testing implementation details which is not ideal. Changing the implementation (underlying component, props to those component) will break this test.

Concretely you assume that the EuiInMemoryTable is present and that it takes items prop.

Now I know that you basically copied what other tests are doing 😊 but let's take this opportunity to improve the test.

In https://github.com/elastic/kibana/blob/main/packages/kbn-test-jest-helpers/src/testbed/testbed.ts I wrote a helper function to parse an EUI table and read its content for testing purpose. Let's copy it over in this file.

So on L37 (below the imports), add the helper func

...
import { coreMock } from '@kbn/core/public/mocks';

// Copied from packages/kbn-test-jest-helpers/src/testbed/testbed.ts
const getTableMetadata = (subject: string, reactWrapper: ReactWrapper) => {
  const table = findTestSubject(reactWrapper, subject);

  if (!table.length) {
    throw new Error(`Eui Table "${subject}" not found.`);
  }

  const rows = table
    .find('tr')
    .slice(1) // we remove the first row as it is the table header
    .map((row) => ({
      reactWrapper: row,
      columns: row.find('div.euiTableCellContent').map((col) => ({
        reactWrapper: col,
        // We can't access the td value with col.text() because
        // eui adds an extra div in td on mobile => (.euiTableRowCell__mobileHeader)
        value: col.find('div.euiTableCellContent').text(),
      })),
    }));

  // Also output the raw cell values, in the following format: [[td0, td1, td2], [td0, td1, td2]]
  const tableCellsValues = rows.map(({ columns }) => columns.map((col) => col.value));
  return { rows, tableCellsValues };
};

Now this test can become

it('render description if provided', async () => {
  (contentClient.mSearch as any as jest.SpyInstance).mockImplementation(() =>
    Promise.resolve({ hits: [doc, doc2, doc4] })
  );

  const wrapper = mount(
    <SavedObjectFinder
      services={{ uiSettings, contentClient, savedObjectsTagging }}
      savedObjectMetaData={searchMetaData}
    />
  );

  await nextTick();
  wrapper.update();

  const table = getTableMetadata('savedObjectsFinderTable', wrapper);
  expect(table.tableCellsValues).toEqual([
    ['Another titleanother description', 'tag-2'],
    ['Example titleexample description', 'tag-1'],
    ['Search', 'tag-4'],
  ]);
});

See how we don't test the underlying implementation. We test that the table renders the correct text.

(contentClient.mSearch as any as jest.SpyInstance).mockImplementation(() =>
Promise.resolve({ hits: [doc, doc2, doc4] })
);

const wrapper = shallow(
<SavedObjectFinder
services={{ uiSettings, contentClient, savedObjectsTagging }}
jughosta marked this conversation as resolved.
Show resolved Hide resolved
savedObjectMetaData={searchMetaData}
/>
);

wrapper.instance().componentDidMount!();
await nextTick();
expect(
wrapper
.find(EuiInMemoryTable)
.prop('items')
.filter((item: any) => item.attributes.description)
.map((item: any) => item.attributes.description)
).toEqual([doc.attributes.description, doc2.attributes.description]);
});
});

describe('sorting', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export class SavedObjectFinderUi extends React.Component<
const savedObjects = response.hits
.map((savedObject) => {
const {
attributes: { name, title },
attributes: { name, title, description },
} = savedObject;
const titleToUse = typeof title === 'string' ? title : '';
const nameToUse = name ? name : titleToUse;
Expand All @@ -150,6 +150,7 @@ export class SavedObjectFinderUi extends React.Component<
title: titleToUse,
name: nameToUse,
simple: savedObject,
description,
};
})
.filter((savedObject) => {
Expand Down Expand Up @@ -307,13 +308,23 @@ export class SavedObjectFinderUi extends React.Component<
);

const tooltipText = this.props.getTooltipText?.(item);

const description = !!item.simple.attributes.description && (
<EuiText size="xs" color="subdued">
{item.simple.attributes.description}
</EuiText>
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the changes!

I noticed some UI issues when testing:

  • descriptions are being partially covered with tags
  • and long description are going offscreen.

Would be great to address them.

Screenshot 2024-11-07 at 21 11 55

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Awesome good catch 6b6fe38 for changes

return tooltipText ? (
<EuiToolTip position="left" content={tooltipText}>
{link}
</EuiToolTip>
<EuiFlexItem grow={false}>
<EuiToolTip position="left" content={tooltipText}>
{link}
</EuiToolTip>
{description}
</EuiFlexItem>
) : (
link
<EuiFlexItem grow={false}>
{link}
{description}
</EuiFlexItem>
);
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,22 +82,25 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
it('allows to manually type tag filter query', async () => {
await PageObjects.discover.openLoadSavedSearchPanel();
await testSubjects.setValue('savedObjectFinderSearchInput', 'tag:(tag-1)');
await expectSavedSearches('A Saved Search');
await expectSavedSearches('A Saved Search\nA Saved Search Description');
});

it('allows to filter by selecting a tag in the filter menu', async () => {
await PageObjects.discover.openLoadSavedSearchPanel();
await selectFilterTags('tag-2');
await expectSavedSearches('A Saved Search', 'A Different Saved Search');
await expectSavedSearches(
'A Saved Search\nA Saved Search Description',
'A Different Saved Search\nA Different Saved Search Description'
);
});

it('allows to filter by multiple tags', async () => {
await PageObjects.discover.openLoadSavedSearchPanel();
await selectFilterTags('tag-2', 'tag-3');
await expectSavedSearches(
'A Saved Search',
'A Different Saved Search',
'A Third Saved Search'
'A Different Saved Search\nA Different Saved Search Description',
'A Saved Search\nA Saved Search Description',
'A Third Saved Search\nAn Untagged Saved Search Description'
);
});
});
Expand All @@ -116,7 +119,11 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
});
await PageObjects.discover.openLoadSavedSearchPanel();
await selectFilterTags('tag-1', 'tag-2');
await expectSavedSearches('A Saved Search', 'A Different Saved Search', 'My New Search');
await expectSavedSearches(
'A Different Saved Search\nA Different Saved Search Description',
'A Saved Search\nA Saved Search Description',
'My New Search'
);
});

it('allows to create a tag from the tag selector', async () => {
Expand Down Expand Up @@ -172,9 +179,9 @@ export default function ({ getPageObjects, getService }: FtrProviderContext) {
await PageObjects.discover.openLoadSavedSearchPanel();
await selectFilterTags('tag-3');
await expectSavedSearches(
'A Different Saved Search',
'A Third Saved Search',
'A Saved Search'
'A Different Saved Search\nA Different Saved Search Description',
'A Saved Search\nA Saved Search Description',
'A Third Saved Search\nAn Untagged Saved Search Description'
);
});
});
Expand Down