Skip to content
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { screen } from '@testing-library/react'

import { BasicField, FieldCreateDto } from 'formsg-shared/types/field'

import { render } from '~/test-utils'

import { getFieldCreationMeta } from '../../utils/fieldCreation'

import { MemoFieldDrawerContent } from './EditFieldDrawer'

vi.mock('~features/user/queries', () => ({
useUser: () => ({ user: undefined, isLoading: false }),
}))
Comment thread
LoneRifle marked this conversation as resolved.

const TODO_PLACEHOLDER_TEXT = 'TODO: Insert field options here'

const getUnsupportedFieldMessage = (fieldType: string) =>
`Unable to edit this field type (${fieldType}). Please contact support if this issue persists.`

describe('MemoFieldDrawerContent', () => {
it('shows an unsupported field error for non-MyInfo children fields', () => {
const field = getFieldCreationMeta(BasicField.Children)

render(<MemoFieldDrawerContent field={field} />)

expect(screen.getByRole('alert')).toHaveTextContent(
getUnsupportedFieldMessage(BasicField.Children),
)
expect(screen.queryByText(TODO_PLACEHOLDER_TEXT)).not.toBeInTheDocument()
})

it('shows an unsupported field error for an unregistered runtime field type', () => {
const field = {
...getFieldCreationMeta(BasicField.ShortText),
fieldType: 'unsupported_field' as BasicField,
} as FieldCreateDto

render(<MemoFieldDrawerContent field={field} />)

expect(screen.getByRole('alert')).toHaveTextContent(
getUnsupportedFieldMessage('unsupported_field'),
)
expect(screen.queryByText(TODO_PLACEHOLDER_TEXT)).not.toBeInTheDocument()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
MyInfoAttribute,
} from 'formsg-shared/types/field'

import InlineMessage from '~components/InlineMessage'

import {
BASICFIELD_TO_DRAWER_META,
MYINFO_FIELD_TO_DRAWER_META,
Expand Down Expand Up @@ -108,6 +110,21 @@ interface MemoFieldDrawerContentProps {
field: FieldCreateDto
}

interface UnsupportedFieldTypeMessageProps {
fieldType: string
}

const UnsupportedFieldTypeMessage = ({
fieldType,
}: UnsupportedFieldTypeMessageProps): JSX.Element => {
return (
<InlineMessage m="1.5rem" role="alert" variant="error">
Unable to edit this field type ({fieldType}). Please contact support if
this issue persists.
</InlineMessage>
Comment on lines +121 to +124

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Consider adding i18n here

)
}

export const MemoFieldDrawerContent = memo<MemoFieldDrawerContentProps>(
({ field, ...props }) => {
const { user } = useUser()
Expand All @@ -126,7 +143,9 @@ export const MemoFieldDrawerContent = memo<MemoFieldDrawerContentProps>(
return <EditMyInfo {...props} field={field} />
}

switch (field.fieldType) {
const fieldType = field.fieldType

switch (fieldType) {
case BasicField.Attachment:
return <EditAttachment {...props} field={field} />
case BasicField.Checkbox:
Expand Down Expand Up @@ -173,8 +192,12 @@ export const MemoFieldDrawerContent = memo<MemoFieldDrawerContentProps>(
return <EditAddress {...props} field={field} />
case BasicField.Signature:
return <EditSignature {...props} field={field} />
default:
return <div>TODO: Insert field options here</div>
case BasicField.Children:
return <UnsupportedFieldTypeMessage fieldType={fieldType} />
default: {
const _exhaustiveCheck: never = fieldType
return <UnsupportedFieldTypeMessage fieldType={_exhaustiveCheck} />
}
}
},
)