فهرست منبع

fix(admin-ui): Correctly convert out-of-order config args

Relates to #1682
Michael Bromley 3 سال پیش
والد
کامیت
f1f7e719fc

+ 58 - 1
packages/admin-ui/src/lib/core/src/common/utilities/configurable-operation-utils.spec.ts

@@ -1,4 +1,8 @@
-import { getDefaultConfigArgValue } from '@vendure/admin-ui/core';
+import {
+    ConfigurableOperation,
+    getDefaultConfigArgValue,
+    toConfigurableOperationInput,
+} from '@vendure/admin-ui/core';
 
 describe('getDefaultConfigArgValue()', () => {
     it('returns a default string value', () => {
@@ -87,3 +91,56 @@ describe('getDefaultConfigArgValue()', () => {
         expect(value).toBe(false);
     });
 });
+
+describe('toConfigurableOperationInput()', () => {
+    const configOp: ConfigurableOperation = {
+        code: 'test',
+        args: [
+            {
+                name: 'option1',
+                value: 'value1',
+            },
+            {
+                name: 'option2',
+                value: 'value2',
+            },
+        ],
+    };
+    it('returns correct structure', () => {
+        const value = toConfigurableOperationInput(configOp, {
+            args: { option1: 'value1-2', option2: 'value2-2' },
+        });
+        expect(value).toEqual({
+            code: 'test',
+            arguments: [
+                {
+                    name: 'option1',
+                    value: 'value1-2',
+                },
+                {
+                    name: 'option2',
+                    value: 'value2-2',
+                },
+            ],
+        });
+    });
+
+    it('works with out-of-order args', () => {
+        const value = toConfigurableOperationInput(configOp, {
+            args: { option2: 'value2-2', option1: 'value1-2' },
+        });
+        expect(value).toEqual({
+            code: 'test',
+            arguments: [
+                {
+                    name: 'option1',
+                    value: 'value1-2',
+                },
+                {
+                    name: 'option2',
+                    value: 'value2-2',
+                },
+            ],
+        });
+    });
+});

+ 11 - 8
packages/admin-ui/src/lib/core/src/common/utilities/configurable-operation-utils.ts

@@ -56,7 +56,7 @@ export function configurableDefinitionToInstance(
  * ```
  * {
  *     code: 'my-operation',
- *     args: [
+ *     arguments: [
  *         { name: 'someProperty', value: 'foo' }
  *     ]
  * }
@@ -64,16 +64,19 @@ export function configurableDefinitionToInstance(
  */
 export function toConfigurableOperationInput(
     operation: ConfigurableOperation,
-    formValueOperations: any,
+    formValueOperations: { args: Record<string, any> },
 ): ConfigurableOperationInput {
     return {
         code: operation.code,
-        arguments: Object.values<any>(formValueOperations.args || {}).map((value, j) => ({
-            name: operation.args[j].name,
-            value: value?.hasOwnProperty('value')
-                ? encodeConfigArgValue((value as any).value)
-                : encodeConfigArgValue(value),
-        })),
+        arguments: operation.args.map(({ name, value }, j) => {
+            const formValue = formValueOperations.args[name];
+            return {
+                name,
+                value: formValue?.hasOwnProperty('value')
+                    ? encodeConfigArgValue((formValue as any).value)
+                    : encodeConfigArgValue(formValue),
+            };
+        }),
     };
 }