Browse Source

test(server): Add e2e tests for AdjustmentSource resolver

Michael Bromley 7 years ago
parent
commit
cf086ece89

+ 1 - 0
admin-ui/src/app/data/definitions/adjustment-source-definitions.ts

@@ -20,6 +20,7 @@ export const ADJUSTMENT_SOURCE_FRAGMENT = gql`
         updatedAt
         name
         type
+        enabled
         conditions {
             ...AdjustmentOperation
         }

+ 1 - 0
admin-ui/src/app/marketing/components/promotion-detail/promotion-detail.component.ts

@@ -116,6 +116,7 @@ export class PromotionDetailComponent extends BaseDetailComponent<AdjustmentSour
         const input: CreateAdjustmentSourceInput = {
             name: formValue.name,
             type: AdjustmentType.PROMOTION,
+            enabled: true,
             conditions: this.mapOperationsToInputs(this.conditions, formValue),
             actions: this.mapOperationsToInputs(this.actions, formValue),
         };

+ 1 - 0
admin-ui/src/app/marketing/providers/routing/promotion-resolver.ts

@@ -18,6 +18,7 @@ export class PromotionResolver extends BaseEntityResolver<AdjustmentSource.Fragm
                 updatedAt: '',
                 type: AdjustmentType.PROMOTION,
                 name: '',
+                enabled: false,
                 conditions: [],
                 actions: [],
             },

File diff suppressed because it is too large
+ 0 - 0
schema.json


+ 268 - 0
server/e2e/__snapshots__/adjustment-source.e2e-spec.ts.snap

@@ -0,0 +1,268 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`AdjustmentSource resolver adjustmentOperations, type = promotion 1`] = `
+Object {
+  "actions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "percentage",
+          "type": "percentage",
+          "value": null,
+        },
+      ],
+      "code": "promo_action",
+      "description": "description for promo_action",
+      "type": "PROMOTION",
+    },
+  ],
+  "conditions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "arg",
+          "type": "money",
+          "value": null,
+        },
+      ],
+      "code": "promo_condition",
+      "description": "description for promo_condition",
+      "type": "PROMOTION",
+    },
+    Object {
+      "args": Array [
+        Object {
+          "name": "arg",
+          "type": "money",
+          "value": null,
+        },
+      ],
+      "code": "promo_condition2",
+      "description": "description for promo_condition2",
+      "type": "PROMOTION",
+    },
+  ],
+}
+`;
+
+exports[`AdjustmentSource resolver adjustmentOperations, type = shipping 1`] = `
+Object {
+  "actions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "percentage",
+          "type": "percentage",
+          "value": null,
+        },
+      ],
+      "code": "shipping_action",
+      "description": "description for shipping_action",
+      "type": "SHIPPING",
+    },
+  ],
+  "conditions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "arg",
+          "type": "money",
+          "value": null,
+        },
+      ],
+      "code": "shipping_condition",
+      "description": "description for shipping_condition",
+      "type": "SHIPPING",
+    },
+  ],
+}
+`;
+
+exports[`AdjustmentSource resolver adjustmentOperations, type = tax 1`] = `
+Object {
+  "actions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "percentage",
+          "type": "percentage",
+          "value": null,
+        },
+      ],
+      "code": "tax_action",
+      "description": "description for tax_action",
+      "type": "TAX",
+    },
+  ],
+  "conditions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "arg",
+          "type": "money",
+          "value": null,
+        },
+      ],
+      "code": "tax_condition",
+      "description": "description for tax_condition",
+      "type": "TAX",
+    },
+  ],
+}
+`;
+
+exports[`AdjustmentSource resolver createAdjustmentSource promotion 1`] = `
+Object {
+  "actions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "percentage",
+          "type": "percentage",
+          "value": "50",
+        },
+      ],
+      "code": "promo_action",
+      "description": "description for promo_action",
+      "type": "PROMOTION",
+    },
+  ],
+  "conditions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "arg",
+          "type": "money",
+          "value": "500",
+        },
+      ],
+      "code": "promo_condition",
+      "description": "description for promo_condition",
+      "type": "PROMOTION",
+    },
+  ],
+  "enabled": true,
+  "name": "promo adjustment source",
+  "type": "PROMOTION",
+}
+`;
+
+exports[`AdjustmentSource resolver createAdjustmentSource shipping 1`] = `
+Object {
+  "actions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "percentage",
+          "type": "percentage",
+          "value": "50",
+        },
+      ],
+      "code": "shipping_action",
+      "description": "description for shipping_action",
+      "type": "SHIPPING",
+    },
+  ],
+  "conditions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "arg",
+          "type": "money",
+          "value": "500",
+        },
+      ],
+      "code": "shipping_condition",
+      "description": "description for shipping_condition",
+      "type": "SHIPPING",
+    },
+  ],
+  "enabled": true,
+  "name": "shipping adjustment source",
+  "type": "SHIPPING",
+}
+`;
+
+exports[`AdjustmentSource resolver createAdjustmentSource tax 1`] = `
+Object {
+  "actions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "percentage",
+          "type": "percentage",
+          "value": "50",
+        },
+      ],
+      "code": "tax_action",
+      "description": "description for tax_action",
+      "type": "TAX",
+    },
+  ],
+  "conditions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "arg",
+          "type": "money",
+          "value": "500",
+        },
+      ],
+      "code": "tax_condition",
+      "description": "description for tax_condition",
+      "type": "TAX",
+    },
+  ],
+  "enabled": true,
+  "name": "tax adjustment source",
+  "type": "TAX",
+}
+`;
+
+exports[`AdjustmentSource resolver updateAdjustmentSource 1`] = `
+Object {
+  "actions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "percentage",
+          "type": "percentage",
+          "value": "50",
+        },
+      ],
+      "code": "promo_action",
+      "description": "description for promo_action",
+      "type": "PROMOTION",
+    },
+  ],
+  "conditions": Array [
+    Object {
+      "args": Array [
+        Object {
+          "name": "arg",
+          "type": "money",
+          "value": "90",
+        },
+      ],
+      "code": "promo_condition",
+      "description": "description for promo_condition",
+      "type": "PROMOTION",
+    },
+    Object {
+      "args": Array [
+        Object {
+          "name": "arg",
+          "type": "money",
+          "value": "10",
+        },
+      ],
+      "code": "promo_condition2",
+      "description": "description for promo_condition2",
+      "type": "PROMOTION",
+    },
+  ],
+  "enabled": true,
+  "name": "promo adjustment source",
+  "type": "PROMOTION",
+}
+`;

+ 267 - 0
server/e2e/adjustment-source.e2e-spec.ts

@@ -0,0 +1,267 @@
+import {
+    AdjustmentSource,
+    AdjustmentType,
+    CreateAdjustmentSource,
+    GetAdjustmentOperations,
+    GetAdjustmentSource,
+    GetAdjustmentSourceList,
+    UpdateAdjustmentSource,
+} from 'shared/generated-types';
+import { pick } from 'shared/pick';
+
+import {
+    CREATE_ADJUSTMENT_SOURCE,
+    GET_ADJUSTMENT_OPERATIONS,
+    GET_ADJUSTMENT_SOURCE,
+    GET_ADJUSTMENT_SOURCE_LIST,
+    UPDATE_ADJUSTMENT_SOURCE,
+} from '../../admin-ui/src/app/data/definitions/adjustment-source-definitions';
+import {
+    AdjustmentActionDefinition,
+    AdjustmentConditionDefinition,
+} from '../src/config/adjustment/adjustment-types';
+
+import { TestClient } from './test-client';
+import { TestServer } from './test-server';
+
+// tslint:disable:no-non-null-assertion
+
+describe('AdjustmentSource resolver', () => {
+    const client = new TestClient();
+    const server = new TestServer();
+
+    const promoCondition = generateTestCondition('promo_condition', AdjustmentType.PROMOTION);
+    const promoCondition2 = generateTestCondition('promo_condition2', AdjustmentType.PROMOTION);
+    const taxCondition = generateTestCondition('tax_condition', AdjustmentType.TAX);
+    const shippingCondition = generateTestCondition('shipping_condition', AdjustmentType.SHIPPING);
+
+    const promoAction = generateTestAction('promo_action', AdjustmentType.PROMOTION);
+    const taxAction = generateTestAction('tax_action', AdjustmentType.TAX);
+    const shippingAction = generateTestAction('shipping_action', AdjustmentType.SHIPPING);
+
+    const snapshotProps = ['name', 'type', 'actions', 'conditions', 'enabled'] as Array<
+        'name' | 'type' | 'actions' | 'conditions' | 'enabled'
+    >;
+    let promoAdjustmentSource: AdjustmentSource.Fragment;
+
+    beforeAll(async () => {
+        const token = await server.init(
+            {
+                productCount: 1,
+                customerCount: 1,
+            },
+            {
+                adjustmentConditions: [promoCondition, promoCondition2, taxCondition, shippingCondition],
+                adjustmentActions: [promoAction, taxAction, shippingAction],
+            },
+        );
+        await client.init();
+    }, 60000);
+
+    afterAll(async () => {
+        await server.destroy();
+    });
+
+    it('createAdjustmentSource promotion', async () => {
+        const result = await client.query<CreateAdjustmentSource.Mutation, CreateAdjustmentSource.Variables>(
+            CREATE_ADJUSTMENT_SOURCE,
+            {
+                input: {
+                    name: 'promo adjustment source',
+                    type: AdjustmentType.PROMOTION,
+                    enabled: true,
+                    conditions: [
+                        {
+                            code: promoCondition.code,
+                            arguments: ['500'],
+                        },
+                    ],
+                    actions: [
+                        {
+                            code: promoAction.code,
+                            arguments: ['50'],
+                        },
+                    ],
+                },
+            },
+        );
+        promoAdjustmentSource = result.createAdjustmentSource;
+        expect(pick(promoAdjustmentSource, snapshotProps)).toMatchSnapshot();
+    });
+
+    it('createAdjustmentSource tax', async () => {
+        const result = await client.query<CreateAdjustmentSource.Mutation, CreateAdjustmentSource.Variables>(
+            CREATE_ADJUSTMENT_SOURCE,
+            {
+                input: {
+                    name: 'tax adjustment source',
+                    type: AdjustmentType.TAX,
+                    enabled: true,
+                    conditions: [
+                        {
+                            code: taxCondition.code,
+                            arguments: ['500'],
+                        },
+                    ],
+                    actions: [
+                        {
+                            code: taxAction.code,
+                            arguments: ['50'],
+                        },
+                    ],
+                },
+            },
+        );
+        expect(pick(result.createAdjustmentSource, snapshotProps)).toMatchSnapshot();
+    });
+
+    it('createAdjustmentSource shipping', async () => {
+        const result = await client.query<CreateAdjustmentSource.Mutation, CreateAdjustmentSource.Variables>(
+            CREATE_ADJUSTMENT_SOURCE,
+            {
+                input: {
+                    name: 'shipping adjustment source',
+                    type: AdjustmentType.SHIPPING,
+                    enabled: true,
+                    conditions: [
+                        {
+                            code: shippingCondition.code,
+                            arguments: ['500'],
+                        },
+                    ],
+                    actions: [
+                        {
+                            code: shippingAction.code,
+                            arguments: ['50'],
+                        },
+                    ],
+                },
+            },
+        );
+        expect(pick(result.createAdjustmentSource, snapshotProps)).toMatchSnapshot();
+    });
+
+    it('updateAdjustmentSource', async () => {
+        const result = await client.query<UpdateAdjustmentSource.Mutation, UpdateAdjustmentSource.Variables>(
+            UPDATE_ADJUSTMENT_SOURCE,
+            {
+                input: {
+                    id: promoAdjustmentSource.id,
+                    conditions: [
+                        {
+                            code: promoCondition.code,
+                            arguments: ['90'],
+                        },
+                        {
+                            code: promoCondition2.code,
+                            arguments: ['10'],
+                        },
+                    ],
+                },
+            },
+        );
+        expect(pick(result.updateAdjustmentSource, snapshotProps)).toMatchSnapshot();
+    });
+
+    it('adjustmentSource', async () => {
+        const result = await client.query<GetAdjustmentSource.Query, GetAdjustmentSource.Variables>(
+            GET_ADJUSTMENT_SOURCE,
+            {
+                id: promoAdjustmentSource.id,
+            },
+        );
+
+        expect(result.adjustmentSource!.name).toBe(promoAdjustmentSource.name);
+    });
+
+    it('adjustmentSources, type = promotion', async () => {
+        const result = await client.query<GetAdjustmentSourceList.Query, GetAdjustmentSourceList.Variables>(
+            GET_ADJUSTMENT_SOURCE_LIST,
+            {
+                type: AdjustmentType.PROMOTION,
+            },
+        );
+
+        expect(result.adjustmentSources.totalItems).toBe(1);
+        expect(result.adjustmentSources.items[0].name).toBe('promo adjustment source');
+    });
+
+    it('adjustmentSources, type = tax', async () => {
+        const result = await client.query<GetAdjustmentSourceList.Query, GetAdjustmentSourceList.Variables>(
+            GET_ADJUSTMENT_SOURCE_LIST,
+            {
+                type: AdjustmentType.TAX,
+            },
+        );
+
+        expect(result.adjustmentSources.totalItems).toBe(1);
+        expect(result.adjustmentSources.items[0].name).toBe('tax adjustment source');
+    });
+
+    it('adjustmentSources, type = shipping', async () => {
+        const result = await client.query<GetAdjustmentSourceList.Query, GetAdjustmentSourceList.Variables>(
+            GET_ADJUSTMENT_SOURCE_LIST,
+            {
+                type: AdjustmentType.SHIPPING,
+            },
+        );
+
+        expect(result.adjustmentSources.totalItems).toBe(1);
+        expect(result.adjustmentSources.items[0].name).toBe('shipping adjustment source');
+    });
+
+    it('adjustmentOperations, type = promotion', async () => {
+        const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
+            GET_ADJUSTMENT_OPERATIONS,
+            {
+                type: AdjustmentType.PROMOTION,
+            },
+        );
+
+        expect(result.adjustmentOperations).toMatchSnapshot();
+    });
+
+    it('adjustmentOperations, type = tax', async () => {
+        const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
+            GET_ADJUSTMENT_OPERATIONS,
+            {
+                type: AdjustmentType.TAX,
+            },
+        );
+
+        expect(result.adjustmentOperations).toMatchSnapshot();
+    });
+
+    it('adjustmentOperations, type = shipping', async () => {
+        const result = await client.query<GetAdjustmentOperations.Query, GetAdjustmentOperations.Variables>(
+            GET_ADJUSTMENT_OPERATIONS,
+            {
+                type: AdjustmentType.SHIPPING,
+            },
+        );
+
+        expect(result.adjustmentOperations).toMatchSnapshot();
+    });
+});
+
+function generateTestCondition(code: string, type: AdjustmentType): AdjustmentConditionDefinition {
+    return {
+        code,
+        description: `description for ${code}`,
+        args: [{ name: 'arg', type: 'money' }],
+        type,
+        predicate: (order, args) => true,
+    };
+}
+
+function generateTestAction(code: string, type: AdjustmentType): AdjustmentActionDefinition {
+    return {
+        code,
+        description: `description for ${code}`,
+        args: [{ name: 'percentage', type: 'percentage' }],
+        type,
+        calculate: (order, args) => {
+            return [{ amount: 42 }];
+        },
+    };
+}

+ 2 - 2
server/e2e/test-server.ts

@@ -26,9 +26,9 @@ export class TestServer {
      * The populated data is saved into an .sqlite file for each test file. On subsequent runs, this file
      * is loaded so that the populate step can be skipped, which speeds up the tests significantly.
      */
-    async init(options: PopulateOptions): Promise<void> {
+    async init(options: PopulateOptions, customConfig: Partial<VendureConfig> = {}): Promise<void> {
         setTestEnvironment();
-        const testingConfig = testConfig;
+        const testingConfig = { ...testConfig, ...customConfig };
         const dbFilePath = this.getDbFilePath();
         (testingConfig.dbConnectionOptions as Mutable<SqljsConnectionOptions>).location = dbFilePath;
         if (!fs.existsSync(dbFilePath)) {

+ 3 - 0
server/src/entity/adjustment-source/adjustment-source.graphql

@@ -4,6 +4,7 @@ type AdjustmentSource implements Node {
     updatedAt: DateTime!
     name: String!
     type: AdjustmentType!
+    enabled: Boolean!
     conditions: [AdjustmentOperation!]!
     actions: [AdjustmentOperation!]!
 }
@@ -41,6 +42,7 @@ input AdjustmentOperationInput {
 input CreateAdjustmentSourceInput {
     name: String!
     type: AdjustmentType!
+    enabled: Boolean!
     conditions: [AdjustmentOperationInput!]!
     actions: [AdjustmentOperationInput!]!
 }
@@ -48,6 +50,7 @@ input CreateAdjustmentSourceInput {
 input UpdateAdjustmentSourceInput {
     id: ID!
     name: String
+    enabled: Boolean
     conditions: [AdjustmentOperationInput!]
     actions: [AdjustmentOperationInput!]
 }

+ 1 - 0
server/src/service/providers/adjustment-source.service.ts

@@ -93,6 +93,7 @@ export class AdjustmentSourceService {
         const adjustmentSource = new AdjustmentSource({
             name: input.name,
             type: input.type,
+            enabled: input.enabled,
             conditions: input.conditions.map(c => this.parseOperationArgs('condition', c)),
             actions: input.actions.map(a => this.parseOperationArgs('action', a)),
         });

+ 14 - 26
shared/generated-types.ts

@@ -71,6 +71,7 @@ export interface AdjustmentSource extends Node {
     updatedAt: DateTime;
     name: string;
     type: AdjustmentType;
+    enabled: boolean;
     conditions: AdjustmentOperation[];
     actions: AdjustmentOperation[];
 }
@@ -316,14 +317,10 @@ export interface ProductVariantTranslation {
     name: string;
 }
 
-export interface Adjustment extends Node {
-    id: string;
-    createdAt: DateTime;
-    updatedAt: DateTime;
-    type: string;
+export interface Adjustment {
+    adjustmentSourceId: string;
+    description: string;
     amount: number;
-    target: AdjustmentTarget;
-    source: AdjustmentSource;
 }
 
 export interface OrderList extends PaginatedList {
@@ -668,6 +665,7 @@ export interface RoleFilterParameter {
 export interface CreateAdjustmentSourceInput {
     name: string;
     type: AdjustmentType;
+    enabled: boolean;
     conditions: AdjustmentOperationInput[];
     actions: AdjustmentOperationInput[];
 }
@@ -680,6 +678,7 @@ export interface AdjustmentOperationInput {
 export interface UpdateAdjustmentSourceInput {
     id: string;
     name?: string | null;
+    enabled?: boolean | null;
     conditions?: AdjustmentOperationInput[] | null;
     actions?: AdjustmentOperationInput[] | null;
 }
@@ -1312,8 +1311,6 @@ export enum LanguageCode {
     zu = 'zu',
 }
 
-export type AdjustmentTarget = Order | OrderItem;
-
 export namespace QueryResolvers {
     export interface Resolvers<Context = any> {
         adjustmentSource?: AdjustmentSourceResolver<AdjustmentSource | null, any, Context>;
@@ -1563,6 +1560,7 @@ export namespace AdjustmentSourceResolvers {
         updatedAt?: UpdatedAtResolver<DateTime, any, Context>;
         name?: NameResolver<string, any, Context>;
         type?: TypeResolver<AdjustmentType, any, Context>;
+        enabled?: EnabledResolver<boolean, any, Context>;
         conditions?: ConditionsResolver<AdjustmentOperation[], any, Context>;
         actions?: ActionsResolver<AdjustmentOperation[], any, Context>;
     }
@@ -1572,6 +1570,7 @@ export namespace AdjustmentSourceResolvers {
     export type UpdatedAtResolver<R = DateTime, Parent = any, Context = any> = Resolver<R, Parent, Context>;
     export type NameResolver<R = string, Parent = any, Context = any> = Resolver<R, Parent, Context>;
     export type TypeResolver<R = AdjustmentType, Parent = any, Context = any> = Resolver<R, Parent, Context>;
+    export type EnabledResolver<R = boolean, Parent = any, Context = any> = Resolver<R, Parent, Context>;
     export type ConditionsResolver<R = AdjustmentOperation[], Parent = any, Context = any> = Resolver<
         R,
         Parent,
@@ -2256,30 +2255,18 @@ export namespace ProductVariantTranslationResolvers {
 
 export namespace AdjustmentResolvers {
     export interface Resolvers<Context = any> {
-        id?: IdResolver<string, any, Context>;
-        createdAt?: CreatedAtResolver<DateTime, any, Context>;
-        updatedAt?: UpdatedAtResolver<DateTime, any, Context>;
-        type?: TypeResolver<string, any, Context>;
+        adjustmentSourceId?: AdjustmentSourceIdResolver<string, any, Context>;
+        description?: DescriptionResolver<string, any, Context>;
         amount?: AmountResolver<number, any, Context>;
-        target?: TargetResolver<AdjustmentTarget, any, Context>;
-        source?: SourceResolver<AdjustmentSource, any, Context>;
     }
 
-    export type IdResolver<R = string, Parent = any, Context = any> = Resolver<R, Parent, Context>;
-    export type CreatedAtResolver<R = DateTime, Parent = any, Context = any> = Resolver<R, Parent, Context>;
-    export type UpdatedAtResolver<R = DateTime, Parent = any, Context = any> = Resolver<R, Parent, Context>;
-    export type TypeResolver<R = string, Parent = any, Context = any> = Resolver<R, Parent, Context>;
-    export type AmountResolver<R = number, Parent = any, Context = any> = Resolver<R, Parent, Context>;
-    export type TargetResolver<R = AdjustmentTarget, Parent = any, Context = any> = Resolver<
-        R,
-        Parent,
-        Context
-    >;
-    export type SourceResolver<R = AdjustmentSource, Parent = any, Context = any> = Resolver<
+    export type AdjustmentSourceIdResolver<R = string, Parent = any, Context = any> = Resolver<
         R,
         Parent,
         Context
     >;
+    export type DescriptionResolver<R = string, Parent = any, Context = any> = Resolver<R, Parent, Context>;
+    export type AmountResolver<R = number, Parent = any, Context = any> = Resolver<R, Parent, Context>;
 }
 
 export namespace OrderListResolvers {
@@ -3653,6 +3640,7 @@ export namespace AdjustmentSource {
         updatedAt: DateTime;
         name: string;
         type: AdjustmentType;
+        enabled: boolean;
         conditions: Conditions[];
         actions: Actions[];
     };

Some files were not shown because too many files changed in this diff