-
Notifications
You must be signed in to change notification settings - Fork 20
Fixes #39231 - Update webhooks table to use TableIndexPage #99
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0512189
Fixes #39232 - Update webhooks table to use TableIndexPage
pondrejk 02ed70d
tests
pondrejk b3c8fb8
removing test mocks
pondrejk a9656f7
simplify formatters
pondrejk 2019a1c
review feedback
pondrejk 10ae5a7
removed the remaining index
pondrejk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
109 changes: 109 additions & 0 deletions
109
...hooks/Components/WebhookTable/Components/Formatters/__tests__/actionCellFormatter.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import '@testing-library/jest-dom'; | ||
|
|
||
| import actionCellFormatter from '../actionCellFormatter'; | ||
|
|
||
| jest.mock( | ||
| 'foremanReact/components/common/table', | ||
| () => ({ | ||
| cellFormatter: content => ( | ||
| <div data-testid="cell-formatter-mock"> | ||
| {content === false ? null : content} | ||
| </div> | ||
| ), | ||
| }), | ||
| { virtual: true } | ||
| ); | ||
|
|
||
| jest.mock('../../ActionButtons/ActionButton', () => { | ||
| // eslint-disable-next-line global-require | ||
| const PropTypes = require('prop-types'); | ||
|
|
||
| const ActionButton = ({ id, name, canDelete }) => ( | ||
| <span | ||
| data-testid="action-button" | ||
| data-id={id} | ||
| data-name={name} | ||
| data-can-delete={String(canDelete)} | ||
| /> | ||
| ); | ||
| ActionButton.propTypes = { | ||
| id: PropTypes.oneOfType([PropTypes.string, PropTypes.number]).isRequired, | ||
| name: PropTypes.string.isRequired, | ||
| canDelete: PropTypes.bool, | ||
| }; | ||
| ActionButton.defaultProps = { | ||
| canDelete: false, | ||
| }; | ||
|
|
||
| return { ActionButton }; | ||
| }); | ||
|
|
||
| describe('actionCellFormatter', () => { | ||
| const webhookActions = { | ||
| deleteWebhook: jest.fn(), | ||
| testWebhook: jest.fn(), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| it('wraps the action control in cellFormatter when rowData is present and the row is editable', () => { | ||
| const formatter = actionCellFormatter(webhookActions); | ||
| const row = { id: 10, name: 'Wh', can_edit: true, can_delete: true }; | ||
|
|
||
| render(formatter(null, { rowData: row })); | ||
|
|
||
| expect(screen.getByTestId('cell-formatter-mock')).toBeInTheDocument(); | ||
| const action = screen.getByTestId('action-button'); | ||
| expect(action).toHaveAttribute('data-id', '10'); | ||
| expect(action).toHaveAttribute('data-name', 'Wh'); | ||
| expect(action).toHaveAttribute('data-can-delete', 'true'); | ||
| }); | ||
|
|
||
| it('passes canDelete from can_delete when rowData is present', () => { | ||
| const formatter = actionCellFormatter(webhookActions); | ||
| const row = { id: 2, name: 'A', can_edit: true, can_delete: false }; | ||
|
|
||
| render(formatter(null, { rowData: row })); | ||
|
|
||
| expect(screen.getByTestId('action-button')).toHaveAttribute( | ||
| 'data-can-delete', | ||
| 'false' | ||
| ); | ||
| }); | ||
|
|
||
| it('renders falsy content through cellFormatter when rowData is present but the row is not editable', () => { | ||
| const formatter = actionCellFormatter(webhookActions); | ||
| const row = { id: 5, name: 'B', can_edit: false }; | ||
|
|
||
| render(formatter(null, { rowData: row })); | ||
|
|
||
| expect(screen.getByTestId('cell-formatter-mock')).toBeInTheDocument(); | ||
| expect(screen.queryByTestId('action-button')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('returns the action control without cellFormatter when rowData is omitted', () => { | ||
| const formatter = actionCellFormatter(webhookActions); | ||
| const row = { id: 20, name: 'Direct', can_edit: true, canDelete: true }; | ||
|
|
||
| render(formatter(row)); | ||
|
|
||
| expect(screen.queryByTestId('cell-formatter-mock')).not.toBeInTheDocument(); | ||
| expect(screen.getByTestId('action-button')).toHaveAttribute( | ||
| 'data-id', | ||
| '20' | ||
| ); | ||
| }); | ||
|
|
||
| it('returns nothing when rowData is omitted and the row is not editable', () => { | ||
| const formatter = actionCellFormatter(webhookActions); | ||
| const row = { id: 1, name: 'X', can_edit: false }; | ||
|
|
||
| const { container } = render(formatter(row)); | ||
|
|
||
| expect(container).toBeEmptyDOMElement(); | ||
| }); | ||
| }); |
38 changes: 38 additions & 0 deletions
38
...ooks/Components/WebhookTable/Components/Formatters/__tests__/enabledCellFormatter.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import React from 'react'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import '@testing-library/jest-dom'; | ||
|
|
||
| import enabledCellFormatter from '../enabledCellFormatter'; | ||
|
|
||
| jest.mock('@patternfly/react-icons', () => ({ | ||
| CheckIcon: () => <span data-testid="check-icon" />, | ||
| BanIcon: () => <span data-testid="ban-icon" />, | ||
| })); | ||
|
|
||
| const renderCell = (value, extra) => { | ||
| const formatter = enabledCellFormatter(); | ||
| const element = formatter(value, extra); | ||
| return render(element); | ||
| }; | ||
|
|
||
| describe('enabledCellFormatter', () => { | ||
| it('uses the cell value when row context includes rowData and value is true', () => { | ||
| renderCell(true, { rowData: { id: 1, enabled: false } }); | ||
| expect(screen.getByTestId('check-icon')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('uses the cell value when row context includes rowData and value is false', () => { | ||
| renderCell(false, { rowData: { id: 1, enabled: true } }); | ||
| expect(screen.getByTestId('ban-icon')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('uses value.enabled when rowData is not present and enabled is true', () => { | ||
| renderCell({ enabled: true }); | ||
| expect(screen.getByTestId('check-icon')).toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('uses value.enabled when rowData is not present and enabled is false', () => { | ||
| renderCell({ enabled: false }); | ||
| expect(screen.getByTestId('ban-icon')).toBeInTheDocument(); | ||
| }); | ||
| }); |
82 changes: 82 additions & 0 deletions
82
...hooks/Components/WebhookTable/Components/Formatters/__tests__/nameToEditFormatter.test.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import React from 'react'; | ||
| import { render, screen, fireEvent } from '@testing-library/react'; | ||
| import '@testing-library/jest-dom'; | ||
|
|
||
| import nameToEditFormatter from '../nameToEditFormatter'; | ||
|
|
||
| jest.mock('@patternfly/react-core', () => { | ||
| // eslint-disable-next-line global-require | ||
| const PropTypes = require('prop-types'); | ||
|
|
||
| const Button = ({ children, onClick, isDisabled }) => ( | ||
| <button type="button" onClick={onClick} disabled={isDisabled}> | ||
| {children} | ||
| </button> | ||
| ); | ||
| Button.propTypes = { | ||
| children: PropTypes.node, | ||
| onClick: PropTypes.func, | ||
| isDisabled: PropTypes.bool, | ||
| }; | ||
| Button.defaultProps = { | ||
| children: null, | ||
| onClick: () => {}, | ||
| isDisabled: false, | ||
| }; | ||
|
|
||
| return { Button }; | ||
| }); | ||
|
|
||
| describe('nameToEditFormatter', () => { | ||
| it('renders the cell value as the label and calls the handler with the row id when can_edit is true', () => { | ||
| const onEdit = jest.fn(); | ||
| const formatter = nameToEditFormatter(onEdit); | ||
| const row = { id: 42, name: 'Other', can_edit: true }; | ||
|
|
||
| render(formatter('Shown name', { rowData: row })); | ||
|
|
||
| expect(screen.getByText('Shown name')).toBeInTheDocument(); | ||
| fireEvent.click(screen.getByText('Shown name')); | ||
| expect(onEdit).toHaveBeenCalledTimes(1); | ||
| expect(onEdit).toHaveBeenCalledWith(42); | ||
| }); | ||
|
|
||
| it('prefers canEdit over can_edit when both are set', () => { | ||
| const onEdit = jest.fn(); | ||
| const formatter = nameToEditFormatter(onEdit); | ||
|
|
||
| render( | ||
| formatter('Label', { | ||
| rowData: { id: 1, name: 'N', canEdit: true, can_edit: false }, | ||
| }) | ||
| ); | ||
|
|
||
| fireEvent.click(screen.getByText('Label')); | ||
| expect(onEdit).toHaveBeenCalledWith(1); | ||
| }); | ||
|
|
||
| it('does not call the handler when the row cannot be edited', () => { | ||
| const onEdit = jest.fn(); | ||
| const formatter = nameToEditFormatter(onEdit); | ||
|
|
||
| render( | ||
| formatter('Read only', { | ||
| rowData: { id: 7, name: 'X', can_edit: false }, | ||
| }) | ||
| ); | ||
|
|
||
| const control = screen.getByText('Read only'); | ||
| expect(control.closest('button')).toBeDisabled(); | ||
| fireEvent.click(control); | ||
| expect(onEdit).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('uses row.name as the label when rowData is not passed and value is the row object', () => { | ||
| const formatter = nameToEditFormatter(jest.fn()); | ||
| const row = { id: 3, name: 'From row', can_edit: false }; | ||
|
|
||
| render(formatter(row)); | ||
|
|
||
| expect(screen.getByText('From row')).toBeInTheDocument(); | ||
| }); | ||
| }); |
23 changes: 23 additions & 0 deletions
23
...ooks/Routes/Webhooks/Components/WebhookTable/Components/Formatters/actionCellFormatter.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import React from 'react'; | ||
| import { cellFormatter } from 'foremanReact/components/common/table'; | ||
| import { ActionButton } from '../ActionButtons/ActionButton'; | ||
|
|
||
| const actionCellFormatter = webhookActions => (value, extra) => { | ||
| const row = extra?.rowData ?? value; | ||
| const canEdit = row.canEdit ?? row.can_edit; | ||
| const canDelete = row.canDelete ?? row.can_delete; | ||
| const { id, name } = row; | ||
|
|
||
| const content = canEdit && ( | ||
| <ActionButton | ||
| canDelete={canDelete} | ||
| id={id} | ||
| name={name} | ||
| webhookActions={webhookActions} | ||
| /> | ||
| ); | ||
|
|
||
| return extra?.rowData != null ? cellFormatter(content) : content; | ||
| }; | ||
|
|
||
| export default actionCellFormatter; |
|
Lukshio marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...ooks/Routes/Webhooks/Components/WebhookTable/Components/Formatters/nameToEditFormatter.js
|
Lukshio marked this conversation as resolved.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import React from 'react'; | ||
| import { Button } from '@patternfly/react-core'; | ||
|
|
||
| const nameToEditFormatter = onClick => (value, extra) => { | ||
| const row = extra?.rowData ?? value; | ||
| const canEdit = row.canEdit ?? row.can_edit; | ||
| const { id } = row; | ||
| const label = extra?.rowData != null ? value : row.name; | ||
|
|
||
| return canEdit ? ( | ||
| <Button | ||
| ouiaId="name-edit-active-button" | ||
| variant="link" | ||
| isInline | ||
| component="span" | ||
| onClick={() => onClick(id)} | ||
| > | ||
| {label} | ||
| </Button> | ||
| ) : ( | ||
| <Button | ||
| ouiaId="name-edit-disabled-button" | ||
| variant="link" | ||
| isInline | ||
| isDisabled | ||
| component="span" | ||
| > | ||
| {label} | ||
| </Button> | ||
| ); | ||
| }; | ||
|
|
||
| export default nameToEditFormatter; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
...ebhooks/WebhooksIndexPage/Components/WebhooksTable/Components/EmptyWebhooksTable/index.js
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...ooksTable/Components/Formatters/__tests__/__snapshots__/enabledCellFormatter.test.js.snap
This file was deleted.
Oops, something went wrong.
7 changes: 0 additions & 7 deletions
7
...age/Components/WebhooksTable/Components/Formatters/__tests__/enabledCellFormatter.test.js
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.