|
| 1 | +// Copyright The Perses Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +import { render, screen, fireEvent } from '@testing-library/react'; |
| 15 | +import { LogsTableColumnsEditor } from './LogsTableColumnsEditor'; |
| 16 | +import { LogsTableOptions, LogsColumnDefinition } from './model'; |
| 17 | + |
| 18 | +const createProps = (columns?: LogsColumnDefinition[]): { value: LogsTableOptions; onChange: jest.Mock } => { |
| 19 | + const value: LogsTableOptions = { |
| 20 | + allowWrap: true, |
| 21 | + enableDetails: true, |
| 22 | + columns, |
| 23 | + }; |
| 24 | + const onChange = jest.fn(); |
| 25 | + return { value, onChange }; |
| 26 | +}; |
| 27 | + |
| 28 | +describe('LogsTableColumnsEditor', () => { |
| 29 | + beforeEach(() => { |
| 30 | + jest.clearAllMocks(); |
| 31 | + }); |
| 32 | + |
| 33 | + it('should render the description text about default columns', () => { |
| 34 | + const props = createProps(); |
| 35 | + render(<LogsTableColumnsEditor {...props} />); |
| 36 | + expect(screen.getByText(/Timestamp and Log line are shown by default/i)).toBeInTheDocument(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should render columns when provided', () => { |
| 40 | + const props = createProps([{ name: 'service', header: 'Service' }, { name: 'level' }]); |
| 41 | + render(<LogsTableColumnsEditor {...props} />); |
| 42 | + expect(screen.getByText('Service')).toBeInTheDocument(); |
| 43 | + expect(screen.getByText('level')).toBeInTheDocument(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should pre-populate with timestamp and line columns on first add', () => { |
| 47 | + const props = createProps(undefined); |
| 48 | + render(<LogsTableColumnsEditor {...props} />); |
| 49 | + fireEvent.click(screen.getByRole('button', { name: /add column/i })); |
| 50 | + expect(props.onChange).toHaveBeenCalledTimes(1); |
| 51 | + const newValue = props.onChange.mock.calls[0][0]; |
| 52 | + expect(newValue.columns).toHaveLength(2); |
| 53 | + expect(newValue.columns[0]).toEqual({ |
| 54 | + name: 'timestamp', |
| 55 | + header: 'Timestamp', |
| 56 | + sortMode: 'timestamp', |
| 57 | + sort: 'desc', |
| 58 | + }); |
| 59 | + expect(newValue.columns[1]).toEqual({ name: 'line', header: 'Log line', allowWrap: true, enableSorting: false }); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should call onChange with new column appended when add is clicked', () => { |
| 63 | + const props = createProps([{ name: 'existing' }]); |
| 64 | + render(<LogsTableColumnsEditor {...props} />); |
| 65 | + fireEvent.click(screen.getByRole('button', { name: /add column/i })); |
| 66 | + expect(props.onChange).toHaveBeenCalledTimes(1); |
| 67 | + const newValue = props.onChange.mock.calls[0][0]; |
| 68 | + expect(newValue.columns).toHaveLength(2); |
| 69 | + expect(newValue.columns[1]).toEqual({ name: '' }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should call onChange with column removed when delete is clicked', () => { |
| 73 | + const props = createProps([{ name: 'first' }, { name: 'second' }]); |
| 74 | + render(<LogsTableColumnsEditor {...props} />); |
| 75 | + const deleteButtons = screen.getAllByLabelText('Delete column'); |
| 76 | + fireEvent.click(deleteButtons[0]!); |
| 77 | + expect(props.onChange).toHaveBeenCalledTimes(1); |
| 78 | + const newValue = props.onChange.mock.calls[0][0]; |
| 79 | + expect(newValue.columns).toHaveLength(1); |
| 80 | + expect(newValue.columns[0].name).toBe('second'); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should call onChange with columns reordered when move up is clicked', () => { |
| 84 | + const props = createProps([{ name: 'first' }, { name: 'second' }]); |
| 85 | + render(<LogsTableColumnsEditor {...props} />); |
| 86 | + const moveUpButtons = screen.getAllByLabelText('Move column up'); |
| 87 | + fireEvent.click(moveUpButtons[1]!); // move second column up |
| 88 | + expect(props.onChange).toHaveBeenCalledTimes(1); |
| 89 | + const newValue = props.onChange.mock.calls[0][0]; |
| 90 | + expect(newValue.columns[0].name).toBe('second'); |
| 91 | + expect(newValue.columns[1].name).toBe('first'); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should call onChange with columns reordered when move down is clicked', () => { |
| 95 | + const props = createProps([{ name: 'first' }, { name: 'second' }]); |
| 96 | + render(<LogsTableColumnsEditor {...props} />); |
| 97 | + const moveDownButtons = screen.getAllByLabelText('Move column down'); |
| 98 | + fireEvent.click(moveDownButtons[0]!); // move first column down |
| 99 | + expect(props.onChange).toHaveBeenCalledTimes(1); |
| 100 | + const newValue = props.onChange.mock.calls[0][0]; |
| 101 | + expect(newValue.columns[0].name).toBe('second'); |
| 102 | + expect(newValue.columns[1].name).toBe('first'); |
| 103 | + }); |
| 104 | + |
| 105 | + it('should render wrap content checkbox for each column', () => { |
| 106 | + const props = createProps([{ name: 'col1' }, { name: 'col2' }]); |
| 107 | + render(<LogsTableColumnsEditor {...props} />); |
| 108 | + const wrapCheckboxes = screen.getAllByLabelText('Wrap content'); |
| 109 | + expect(wrapCheckboxes).toHaveLength(2); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should have wrap content unchecked by default', () => { |
| 113 | + const props = createProps([{ name: 'col1' }]); |
| 114 | + render(<LogsTableColumnsEditor {...props} />); |
| 115 | + const wrapCheckbox = screen.getByLabelText('Wrap content'); |
| 116 | + expect(wrapCheckbox).not.toBeChecked(); |
| 117 | + }); |
| 118 | + |
| 119 | + it('should have wrap content checked when allowWrap is true', () => { |
| 120 | + const props = createProps([{ name: 'col1', allowWrap: true }]); |
| 121 | + render(<LogsTableColumnsEditor {...props} />); |
| 122 | + const wrapCheckbox = screen.getByLabelText('Wrap content'); |
| 123 | + expect(wrapCheckbox).toBeChecked(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('should render column name field with helper text', () => { |
| 127 | + const props = createProps([{ name: 'service' }]); |
| 128 | + render(<LogsTableColumnsEditor {...props} />); |
| 129 | + expect(screen.getByText(/Use 'timestamp', 'line', or a label key/)).toBeInTheDocument(); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should display "New column" as display name for empty column', () => { |
| 133 | + const props = createProps([{ name: '' }]); |
| 134 | + render(<LogsTableColumnsEditor {...props} />); |
| 135 | + expect(screen.getByText('New column')).toBeInTheDocument(); |
| 136 | + }); |
| 137 | + |
| 138 | + it('should render with empty columns array when columns is undefined', () => { |
| 139 | + const props = createProps(undefined); |
| 140 | + render(<LogsTableColumnsEditor {...props} />); |
| 141 | + expect(screen.getByRole('button', { name: /add column/i })).toBeInTheDocument(); |
| 142 | + expect(screen.queryByLabelText('Enable sorting')).not.toBeInTheDocument(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should render sort mode options including Alphabetical, Numeric, and Timestamp', () => { |
| 146 | + const props = createProps([{ name: 'col1' }]); |
| 147 | + render(<LogsTableColumnsEditor {...props} />); |
| 148 | + // The sort mode select should be present |
| 149 | + expect(screen.getByLabelText('Sort mode')).toBeInTheDocument(); |
| 150 | + }); |
| 151 | +}); |
0 commit comments