Skip to content

Commit

Permalink
test: fixed tests and lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Quentin Le Caignec committed Aug 27, 2024
1 parent c314a74 commit ac486c8
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Alien, Leaf, TreasureChest } from '@phosphor-icons/react';

export const blockOptionsMock = [
{ id: 'default', label: 'Default', leftSection: <Alien /> },
{ id: 'other', label: 'Other', leftSection: <Leaf /> },
{ id: 'stuff', label: 'Stuff', leftSection: <TreasureChest /> },
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { ReactElement } from 'react';

import { renderWithProviders } from '@smile/haring-react-shared/test-utils';
import { FormProvider, useForm } from 'react-hook-form';

import { FormDynamicZone } from './FormDynamicZone';

describe('FormDynamicZone', () => {
it('matches snapshot', () => {
function TestWithProvider(): ReactElement {
const methods = useForm();
return (
<FormProvider {...methods}>
<FormDynamicZone dynamicZoneName="dynamicZoneTest" />
</FormProvider>
);
}

const { container } = renderWithProviders(<TestWithProvider />);
expect(container).toMatchSnapshot();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { Flex } from '@mantine/core';
import { DynamicZone } from '@smile/haring-react';
import { useFieldArray, useFormContext, useWatch } from 'react-hook-form';

import { blockOptionsMock } from './FormDynamicZone.mock';

interface IFormBlock extends IBaseBlock {
fieldName: string;
value?: string;
Expand Down Expand Up @@ -68,7 +70,7 @@ export function FormDynamicZone(props: IFormDynamicZoneProps): ReactElement {
<form onSubmit={handleSubmit(onSubmit)}>
form container, {dynamicZoneName}: {JSON.stringify(watched)}
<DynamicZone<IFormBlock>
blockOptions={['default']}
blockOptions={blockOptionsMock}
blocks={fields as IFormBlock[]}
onAppendBlock={onAppend}
onRenderBlockContent={newBlock}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const DynamicZone: IStory = {
args: {
blockOptions: dynamicZoneButtons,
blocks: dynamicZoneBlocks,
buttonsGroupLabel: 'Ajouter un block',
buttonsText: 'Ajouter un block',
internalBlockCardProps: {
headerCardSectionProps: {
bg: 'cadetblue',
Expand Down
45 changes: 23 additions & 22 deletions packages/haring-react/src/Form/DynamicZone/DynamicZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
ContainerProps,
GroupProps,
StackProps,
TextProps,
} from '@mantine/core';
import type { ReactElement } from 'react';

Expand All @@ -19,8 +20,9 @@ export interface IDynamicZoneProps<Block extends IBaseBlock> {
blocks: Block[];
blocksStackProps?: StackProps;
bottomContainerProps?: ContainerProps;
buttonsGroupLabel?: string;
buttonsGroupProps?: GroupProps;
buttonsText?: string;
buttonsTextProps?: TextProps;
internalBlockCardProps?: IDynamicZoneBlockInternalComponentProps;
onAppendBlock: (blockType: string) => void;
onRenderBlockContent: (block: Block, index: number) => ReactElement;
Expand All @@ -37,8 +39,9 @@ export function DynamicZone<Block extends IBaseBlock>(
blocks,
blocksStackProps,
bottomContainerProps,
buttonsGroupLabel,
buttonsGroupProps,
buttonsText,
buttonsTextProps,
internalBlockCardProps,
onAppendBlock,
onRenderBlockContent,
Expand Down Expand Up @@ -76,26 +79,24 @@ export function DynamicZone<Block extends IBaseBlock>(
p="sm"
{...bottomContainerProps}
>
<Container fluid p={0}>
<Text className={classes.buttonsLabel} fw="bold">
{buttonsGroupLabel}
</Text>
<Group {...buttonsGroupProps}>
{blockOptions.map((button) => (
<Button
radius="md"
size="md"
type="button"
variant="default"
{...button}
key={`button-${button.id}`}
onClick={() => onAddBlock(button.id)}
>
{button.label}
</Button>
))}
</Group>
</Container>
<Text className={classes.buttonsLabel} fw="bold" {...buttonsTextProps}>
{buttonsText}
</Text>
<Group {...buttonsGroupProps}>
{blockOptions.map((button) => (
<Button
radius="md"
size="md"
type="button"
variant="default"
{...button}
key={`button-${button.id}`}
onClick={() => onAddBlock(button.id)}
>
{button.label}
</Button>
))}
</Group>
</Container>
</Container>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ exports[`FoldableColumnLayout matches snapshot 1`] = `
aria-hidden="true"
class="m_9307d992 mantine-Switch-track"
data-label-position="right"
data-without-labels="true"
>
<span
class="m_93039a1d mantine-Switch-thumb"
Expand Down

0 comments on commit ac486c8

Please sign in to comment.