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

[plugin-klogs] feat: adds button to remove column from Documents table #608

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
112 changes: 84 additions & 28 deletions app/packages/klogs/src/components/Logs.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,92 @@ import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { vi } from 'vitest';

import { LogsDownload } from './Logs';

describe('LogsDownload', () => {
const documents = [
{
log: '{"namespace": "foo"}',
namespace: 'foo',
timestamp: new Date(2000, 2, 2).toISOString(),
},
];

it('can download logs', async () => {
const download = vi.fn();
render(<LogsDownload documents={documents} download={download} fields={[]} />);

const openMenu = screen.getByLabelText('open menu');
await userEvent.click(openMenu);
const downloadJSON = screen.getByLabelText('Download Logs');
await userEvent.click(downloadJSON);
expect(download).toHaveBeenCalledWith('{"namespace": "foo"}', 'kobs-export-logs.log');
import { Documents, LogsDownload } from './Logs';

describe('Logs', () => {
describe('LogsDownload', () => {
const documents = [
{
log: '{"namespace": "foo"}',
namespace: 'foo',
timestamp: new Date(2000, 2, 2).toISOString(),
},
];

it('can download logs', async () => {
const download = vi.fn();
render(<LogsDownload documents={documents} download={download} fields={[]} />);

const openMenu = screen.getByLabelText('open menu');
await userEvent.click(openMenu);
const downloadJSON = screen.getByLabelText('Download Logs');
await userEvent.click(downloadJSON);
expect(download).toHaveBeenCalledWith('{"namespace": "foo"}', 'kobs-export-logs.log');
});

it('can download csv', async () => {
const download = vi.fn();
render(<LogsDownload documents={documents} download={download} fields={['namespace']} />);

const openMenu = screen.getByLabelText('open menu');
await userEvent.click(openMenu);
const downloadCSV = screen.getByLabelText('Download CSV');
await userEvent.click(downloadCSV);
expect(download).toHaveBeenCalledWith('2000-03-02 00:00:00;foo\r\n', 'kobs-export-logs.csv');
});
});

it('can download csv', async () => {
const download = vi.fn();
render(<LogsDownload documents={documents} download={download} fields={['namespace']} />);
describe('Documents', () => {
const documents = [
{
log: '{"namespace": "foo"}',
namespace: 'foo',
timestamp: new Date(2000, 2, 2).toISOString(),
},
];

const iconName = 'TableChartIcon';

const setup = (
documents: Record<string, string>[],
selectedFields: string[],
selectField?: (field: string) => void,
) => {
return (
<Documents
documents={documents}
fields={[
{ name: 'log', type: 'string' },
{ name: 'namespace', type: 'string' },
{ name: 'timestamp', type: 'string' },
]}
order={'descending'}
orderBy={'timestamp'}
selectField={selectField}
selectedFields={selectedFields}
/>
);
};

it('show documents with column remove button', async () => {
const removeFieldMock = vi.fn();
const selectedFields = ['log', 'namespace', 'timestamp'];
render(setup(documents, selectedFields, removeFieldMock));

const removeColumnIcons = screen.getAllByTestId(iconName);
expect(removeColumnIcons.length).toBe(selectedFields.length);
expect(screen.getByText('Time')).toBeInTheDocument();
expect(screen.getByText('log')).toBeInTheDocument();
expect(screen.getByText('namespace')).toBeInTheDocument();
expect(screen.getByText('timestamp')).toBeInTheDocument();

const element = document.querySelector(`button > svg[data-testid="${iconName}"]`);
expect(element).not.toBeNull();
/* eslint-disable-next-line @typescript-eslint/no-non-null-assertion */
await userEvent.click(element!);

const openMenu = screen.getByLabelText('open menu');
await userEvent.click(openMenu);
const downloadCSV = screen.getByLabelText('Download CSV');
await userEvent.click(downloadCSV);
expect(download).toHaveBeenCalledWith('2000-03-02 00:00:00;foo\r\n', 'kobs-export-logs.csv');
// check to be called -> first button -> first column, which is 'log'
expect(removeFieldMock).toBeCalledWith('log');
});
});
});
14 changes: 12 additions & 2 deletions app/packages/klogs/src/components/Logs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -480,9 +480,9 @@ const Document: FunctionComponent<{
/**
* The `Documents` component is used to render the documents in a table. The table will show the timestamp of each log
* line and a preview of the most important fields. If the user select a list of fields these fields will be shown
* insteand of the preview.
* instead of the preview.
*/
const Documents: FunctionComponent<{
export const Documents: FunctionComponent<{
addFilter?: (filter: string) => void;
changeOrder?: (orderBy: string) => void;
documents: Record<string, string>[];
Expand Down Expand Up @@ -524,6 +524,16 @@ const Documents: FunctionComponent<{
>
{field}
</TableSortLabel>
<IconButton
edge="end"
color="inherit"
size={'small'}
sx={{ m: 0 }}
aria-label="toggle field column"
onClick={() => selectField?.(field)}
>
<TableChart sx={{ fontSize: 16 }} />
</IconButton>
Comment on lines +527 to +536
Copy link
Contributor Author

Choose a reason for hiding this comment

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

core change.

</TableCell>
))}
</>
Expand Down
Loading