Skip to content

Commit

Permalink
refactor: review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Moreau committed Oct 15, 2024
1 parent 498353c commit 61ce3a1
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/services/smart-action-form-layout-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ class SmartActionFormLayoutService {

if (!formElements) return { fields: [], layout: [] };

const isFirstElementPage = formElements[0].component === 'Page';

formElements.forEach((element) => {
if (element.type === 'Layout') {
hasLayout = true;
}

if (isFirstElementPage && element.component !== 'Page') {
throw new Error('You cannot use pages and other elements at the same level');
}
layout.push(SmartActionFormLayoutService.parseLayout(element, fields));
});

Expand Down
126 changes: 126 additions & 0 deletions test/services/smart-action-form-layout-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,132 @@ describe('services > smart-action-form-layout', () => {
});

describe('extractFieldsAndLayout', () => {
it('should throw an an error when mixing pages and other types at the same level', async () => {
const { smartActionFormLayoutService } = setup();

const form = [
{
type: 'Layout',
component: 'Page',
elements: [
{
type: 'String',
field: 'Credit card plan',
id: 'Plan',
},
{
type: 'Layout',
component: 'HtmlBlock',
content: `
<p>You will be asked to provide them in the next pages</p>`,
}],
},
{
type: 'String',
field: 'Credit card plan',
isRequired: true,
},

{
type: 'String',
field: 'Credit card number',
isRequired: true,
},
];

await expect(() => smartActionFormLayoutService.extractFieldsAndLayout(form)).toThrow('You cannot use pages and other elements at the same level');
});

it('should return fields only if no layout is specified', async () => {
const { smartActionFormLayoutService } = setup();

const form = [

{
type: 'String',
field: 'Credit card plan',
id: 'Plan',
widget: 'Dropdown',
options: [
'Base',
'Gold',
'Black',
],
isRequired: true,
},
{
type: 'Number',
field: 'price',
defaultValue: 40,
},
{
type: 'Number',
field: 'Max withdraw',
},
{
type: 'Number',
field: 'Max payment',
},
{
type: 'Boolean',
field: 'Systematic check',
},
{
type: 'Number',
field: 'Discount',
},
{
type: 'Number',
widget: 'NumberInput',
field: 'Discount duration',
},

];

const { fields, layout } = smartActionFormLayoutService.extractFieldsAndLayout(form);

expect(fields).toStrictEqual([
{
type: 'String',
field: 'Credit card plan',
id: 'Plan',
widget: 'Dropdown',
options: [
'Base',
'Gold',
'Black',
],
isRequired: true,
},
{
type: 'Number',
field: 'price',
defaultValue: 40,
},
{
type: 'Number',
field: 'Max withdraw',
},
{
type: 'Number',
field: 'Max payment',
},
{
type: 'Boolean',
field: 'Systematic check',
},
{
type: 'Number',
field: 'Discount',
},
{
type: 'Number',
widget: 'NumberInput',
field: 'Discount duration',
},
]);
expect(layout).toStrictEqual([]);
});
it('should extract out the layout and fields from the hook result', async () => {
const { smartActionFormLayoutService } = setup();

Expand Down

0 comments on commit 61ce3a1

Please sign in to comment.