Browse Source

fix(core): Fix failing tests

jacobfrantz1 3 years ago
parent
commit
a87dd3c6aa

+ 6 - 2
packages/core/e2e/shop-order.e2e-spec.ts

@@ -564,7 +564,9 @@ describe('Shop orders', () => {
                 expect(addItemToOrder!.lines[1].quantity).toBe(1);
 
                 const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
-                expect(activeOrder.lines[1].customFields.lineImages).toEqual([{ id: 'T_1' }, { id: 'T_2' }]);
+                expect(activeOrder.lines[1].customFields.lineImages.length).toBe(2);
+                expect(activeOrder.lines[1].customFields.lineImages).toContainEqual({ id: 'T_1' });
+                expect(activeOrder.lines[1].customFields.lineImages).toContainEqual({ id: 'T_2' });
             });
 
             it('addItemToOrder with equal list relation customField adds to quantity', async () => {
@@ -585,7 +587,9 @@ describe('Shop orders', () => {
 
                 const { activeOrder } = await shopClient.query(GET_ORDER_WITH_ORDER_LINE_CUSTOM_FIELDS);
 
-                expect(activeOrder.lines[1].customFields.lineImages).toEqual([{ id: 'T_1' }, { id: 'T_2' }]);
+                expect(activeOrder.lines[1].customFields.lineImages.length).toBe(2);
+                expect(activeOrder.lines[1].customFields.lineImages).toContainEqual({ id: 'T_1' });
+                expect(activeOrder.lines[1].customFields.lineImages).toContainEqual({ id: 'T_2' });
             });
 
             it('addItemToOrder with different list relation customField adds new line', async () => {

+ 7 - 1
packages/core/src/service/helpers/order-modifier/order-modifier.ts

@@ -587,7 +587,13 @@ export class OrderModifier {
             // or equal to the defaultValue
             for (const def of customFieldDefs) {
                 const key = def.name;
-                const existingValue = existingCustomFields?.[key];
+                // This ternary is there because with the MySQL driver, boolean customFields with a default
+                // of `false` were being rep-resented as `0`, thus causing the equality check to fail.
+                // So if it's a boolean, we'll explicitly coerce the value to a boolean.
+                const existingValue =
+                    def.type === 'boolean' && typeof existingCustomFields?.[key] === 'number'
+                        ? !!existingCustomFields?.[key]
+                        : existingCustomFields?.[key];
                 if (existingValue != null && (!def.list || existingValue?.length !== 0)) {
                     if (def.defaultValue != null) {
                         if (existingValue !== def.defaultValue) {