-
Notifications
You must be signed in to change notification settings - Fork 13k
chore: ABAC add/edit room attributes form #37244
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
13 commits
Select commit
Hold shift + click to select a range
a7c4d90
chore: ABAC add/edit room attributes form
MartinSchoeler 07deda5
tests: replace random uuids from RHF array fields
MartinSchoeler 7197941
review: :tasso:
MartinSchoeler 7fe95ee
review: don't use id
MartinSchoeler 579a048
review: storybook formatting
MartinSchoeler 84819cd
snapshot
MartinSchoeler e3c7bb2
snap
MartinSchoeler 5ad306b
review: make tests more objective
MartinSchoeler 130efa1
Merge branch 'develop' into feat/abac-room-att-form
kodiakhq[bot] f8d48c2
Merge branch 'develop' into feat/abac-room-att-form
MartinSchoeler 6f583cd
Merge branch 'develop' into feat/abac-room-att-form
MartinSchoeler 9751fb8
Merge branch 'develop' into feat/abac-room-att-form
MartinSchoeler 737d351
Merge branch 'develop' into feat/abac-room-att-form
MartinSchoeler 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
259 changes: 259 additions & 0 deletions
259
apps/meteor/client/views/admin/ABAC/AdminABACRoomAttributesForm.spec.tsx
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,259 @@ | ||
| /* eslint-disable @typescript-eslint/naming-convention */ | ||
| import { mockAppRoot } from '@rocket.chat/mock-providers'; | ||
| import { composeStories } from '@storybook/react'; | ||
| import { render, screen, waitFor } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
| import { axe } from 'jest-axe'; | ||
| import type { ReactNode } from 'react'; | ||
| import { FormProvider, useForm } from 'react-hook-form'; | ||
|
|
||
| import AdminABACRoomAttributesForm, { type AdminABACRoomAttributesFormFormData } from './AdminABACRoomAttributesForm'; | ||
| import * as stories from './AdminABACRoomAttributesForm.stories'; | ||
|
|
||
| const testCases = Object.values(composeStories(stories)).map((Story) => [Story.storyName || 'Story', Story]); | ||
|
|
||
| const appRoot = mockAppRoot() | ||
| .withTranslations('en', 'core', { | ||
| Name: 'Name', | ||
| Values: 'Values', | ||
| Add_Value: 'Add Value', | ||
| Cancel: 'Cancel', | ||
| Save: 'Save', | ||
| Required_field: '{{field}} is required', | ||
| }) | ||
| .build(); | ||
|
|
||
| const FormProviderWrapper = ({ | ||
| children, | ||
| defaultValues, | ||
| }: { | ||
| children: ReactNode; | ||
| defaultValues?: Partial<AdminABACRoomAttributesFormFormData>; | ||
| }) => { | ||
| const methods = useForm<AdminABACRoomAttributesFormFormData>({ | ||
| defaultValues: { | ||
| name: '', | ||
| attributeValues: [{ value: '' }], | ||
| lockedAttributes: [], | ||
| ...defaultValues, | ||
| }, | ||
| mode: 'onChange', | ||
| }); | ||
|
|
||
| return <FormProvider {...methods}>{children}</FormProvider>; | ||
| }; | ||
|
|
||
| describe('AdminABACRoomAttributesForm', () => { | ||
| const defaultProps = { | ||
| onSave: jest.fn(), | ||
| onCancel: jest.fn(), | ||
| description: 'Create an attribute that can later be assigned to rooms.', | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| test.each(testCases)(`renders %s without crashing`, async (_storyname, Story) => { | ||
| const { baseElement } = render(<Story />, { wrapper: appRoot }); | ||
|
|
||
| expect(baseElement).toMatchSnapshot(); | ||
| }); | ||
|
|
||
| test.each(testCases)('%s should have no a11y violations', async (_storyname, Story) => { | ||
| const { container } = render(<Story />, { wrapper: appRoot }); | ||
|
|
||
| const results = await axe(container); | ||
| expect(results).toHaveNoViolations(); | ||
| }); | ||
|
|
||
| it('should show validation errors for required fields', async () => { | ||
| render( | ||
| <FormProviderWrapper> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| const saveButton = screen.getByRole('button', { name: 'Save' }); | ||
| await userEvent.click(saveButton); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText('Name is required')).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should show validation error for empty attribute values', async () => { | ||
| render( | ||
| <FormProviderWrapper> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| const nameInput = screen.getByLabelText('Name*'); | ||
| await userEvent.type(nameInput, 'Test Attribute'); | ||
|
|
||
| const saveButton = screen.getByRole('button', { name: 'Save' }); | ||
| await userEvent.click(saveButton); | ||
|
|
||
| await waitFor(() => { | ||
| expect(screen.getByText('Values is required')).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should add new attribute value when Add Value button is clicked', async () => { | ||
| const defaultValues = { | ||
| name: 'Test Attribute', | ||
| attributeValues: [{ value: 'Value 1' }], | ||
| }; | ||
|
|
||
| render( | ||
| <FormProviderWrapper defaultValues={defaultValues}> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| const addButton = screen.getByRole('button', { name: 'Add Value' }); | ||
| await userEvent.click(addButton); | ||
|
|
||
| const valueInputs = screen.getAllByLabelText('Values*'); | ||
| expect(valueInputs).toHaveLength(2); | ||
| }); | ||
|
|
||
| it('should remove attribute value when trash button is clicked', async () => { | ||
| const defaultValues = { | ||
| name: 'Test Attribute', | ||
| attributeValues: [{ value: 'Value 1' }, { value: 'Value 2' }], | ||
| }; | ||
|
|
||
| render( | ||
| <FormProviderWrapper defaultValues={defaultValues}> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| const trashButtons = screen.getAllByRole('button', { name: 'Remove' }); | ||
| expect(screen.getByDisplayValue('Value 1')).toBeInTheDocument(); | ||
| expect(screen.getByDisplayValue('Value 2')).toBeInTheDocument(); | ||
|
|
||
| await userEvent.click(trashButtons[0]); | ||
|
|
||
| expect(screen.getByDisplayValue('Value 1')).toBeInTheDocument(); | ||
| expect(screen.queryByDisplayValue('Value 2')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should remove locked attribute when trash button is clicked', async () => { | ||
| const defaultValues = { | ||
| name: 'Room Type', | ||
| lockedAttributes: [{ value: 'Locked Value 1' }, { value: 'Locked Value 2' }], | ||
| }; | ||
|
|
||
| render( | ||
| <FormProviderWrapper defaultValues={defaultValues}> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| const trashButtons = screen.queryAllByRole('button', { name: 'Remove' }); | ||
|
|
||
| expect(screen.getByDisplayValue('Locked Value 1')).toBeInTheDocument(); | ||
| expect(screen.getByDisplayValue('Locked Value 2')).toBeInTheDocument(); | ||
|
|
||
| await userEvent.click(trashButtons[0]); | ||
|
|
||
| expect(screen.getByDisplayValue('Locked Value 1')).toBeInTheDocument(); | ||
| expect(screen.queryByDisplayValue('Locked Value 2')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('should disable Add Value button when there are empty values', async () => { | ||
| render( | ||
| <FormProviderWrapper> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| const addButton = screen.getByRole('button', { name: 'Add Value' }); | ||
| expect(addButton).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('should enable Add Value button when all values are filled', async () => { | ||
| const defaultValues = { | ||
| name: 'Test Attribute', | ||
| attributeValues: [{ value: 'Value 1' }], | ||
| }; | ||
|
|
||
| render( | ||
| <FormProviderWrapper defaultValues={defaultValues}> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| const addButton = screen.getByRole('button', { name: 'Add Value' }); | ||
| expect(addButton).not.toBeDisabled(); | ||
| }); | ||
|
|
||
| it('should call onSave with correct data when form is submitted', async () => { | ||
| const defaultValues = { | ||
| name: 'Test Attribute', | ||
| attributeValues: [{ value: 'Value 1' }, { value: 'Value 2' }], | ||
| }; | ||
|
|
||
| render( | ||
| <FormProviderWrapper defaultValues={defaultValues}> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| const trashButtons = screen.queryAllByRole('button', { name: 'Remove' }); | ||
| await userEvent.click(trashButtons[0]); | ||
|
|
||
| const saveButton = screen.getByRole('button', { name: 'Save' }); | ||
| await userEvent.click(saveButton); | ||
|
|
||
| await waitFor(() => { | ||
| expect(defaultProps.onSave).toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| it('should call onCancel when Cancel button is clicked', async () => { | ||
| render( | ||
| <FormProviderWrapper> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| const cancelButton = screen.getByRole('button', { name: 'Cancel' }); | ||
| await userEvent.click(cancelButton); | ||
|
|
||
| expect(defaultProps.onCancel).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should handle mixed locked and regular attributes correctly', async () => { | ||
| const defaultValues = { | ||
| name: 'Room Type', | ||
| lockedAttributes: [{ value: 'Locked Value' }], | ||
| attributeValues: [{ value: 'Regular Value' }], | ||
| }; | ||
|
|
||
| render( | ||
| <FormProviderWrapper defaultValues={defaultValues}> | ||
| <AdminABACRoomAttributesForm {...defaultProps} /> | ||
| </FormProviderWrapper>, | ||
| { wrapper: appRoot }, | ||
| ); | ||
|
|
||
| expect(screen.getByDisplayValue('Locked Value')).toBeDisabled(); | ||
| expect(screen.getByDisplayValue('Regular Value')).not.toBeDisabled(); | ||
|
|
||
| const trashButtons = screen.getAllByRole('button', { name: 'Remove' }); | ||
| expect(trashButtons).toHaveLength(1); | ||
| }); | ||
| }); | ||
68 changes: 68 additions & 0 deletions
68
apps/meteor/client/views/admin/ABAC/AdminABACRoomAttributesForm.stories.tsx
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,68 @@ | ||
| import { Contextualbar } from '@rocket.chat/fuselage'; | ||
| import { mockAppRoot } from '@rocket.chat/mock-providers'; | ||
| import type { Meta, StoryFn } from '@storybook/react'; | ||
| import { FormProvider, useForm } from 'react-hook-form'; | ||
|
|
||
| import AdminABACRoomAttributesForm, { type AdminABACRoomAttributesFormFormData } from './AdminABACRoomAttributesForm'; | ||
|
|
||
| export default { | ||
| component: AdminABACRoomAttributesForm, | ||
| parameters: { | ||
| layout: 'padded', | ||
| }, | ||
| args: { | ||
| description: 'Create an attribute that can later be assigned to rooms.', | ||
| }, | ||
| decorators: [mockAppRoot().buildStoryDecorator()], | ||
| } satisfies Meta<typeof AdminABACRoomAttributesForm>; | ||
|
|
||
| const Template: StoryFn<typeof AdminABACRoomAttributesForm> = (args) => <AdminABACRoomAttributesForm {...args} />; | ||
|
|
||
| export const NewAttribute = Template.bind({}); | ||
|
|
||
| NewAttribute.decorators = [ | ||
| (fn) => { | ||
| const methods = useForm<AdminABACRoomAttributesFormFormData>({ | ||
| defaultValues: { | ||
| name: '', | ||
| attributeValues: [{ value: '' }], | ||
| lockedAttributes: [], | ||
| }, | ||
| mode: 'onChange', | ||
| }); | ||
|
|
||
| return ( | ||
| <FormProvider {...methods}> | ||
| <Contextualbar width='400px' p={16}> | ||
| {fn()} | ||
| </Contextualbar> | ||
| </FormProvider> | ||
| ); | ||
| }, | ||
| ]; | ||
|
|
||
| export const WithLockedAttributes = Template.bind({}); | ||
|
|
||
| WithLockedAttributes.args = { | ||
| description: 'Attribute values cannot be edited, but can be added or deleted.', | ||
| }; | ||
|
|
||
| WithLockedAttributes.decorators = [ | ||
| (fn) => { | ||
| const methods = useForm<AdminABACRoomAttributesFormFormData>({ | ||
| defaultValues: { | ||
| name: 'Room Type', | ||
| lockedAttributes: [{ value: 'Locked Value 1' }, { value: 'Locked Value 2' }, { value: 'Locked Value 3' }], | ||
| }, | ||
| mode: 'onChange', | ||
| }); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return ( | ||
| <FormProvider {...methods}> | ||
| <Contextualbar width='400px' p={16}> | ||
| {fn()} | ||
| </Contextualbar> | ||
| </FormProvider> | ||
| ); | ||
| }, | ||
| ]; | ||
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.