Skip to content

Commit

Permalink
feat(core): Added example implementation for vendure-ecommerce#303
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyratox committed Jun 9, 2020
1 parent a6de120 commit 75c80b9
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 26 deletions.
40 changes: 28 additions & 12 deletions packages/core/e2e/lifecycle.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,58 @@ import {
AutoIncrementIdStrategy,
defaultShippingEligibilityChecker,
Injector,
Order,
ProductService,
ShippingEligibilityChecker,
} from '@vendure/core';
import { createTestEnvironment } from '@vendure/testing';
import path from 'path';
import { Connection } from 'typeorm';

import { initialData } from '../../../e2e-common/e2e-initial-data';
import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';

const strategyInitSpy = jest.fn();
const strategyDestroySpy = jest.fn();
const codInitSpy = jest.fn();
const codCheckSpy = jest.fn();
const codDestroySpy = jest.fn();

class TestIdStrategy extends AutoIncrementIdStrategy {
async init(injector: Injector) {
const productService = injector.get(ProductService);
const connection = injector.getConnection();
await new Promise(resolve => setTimeout(resolve, 100));
await new Promise((resolve) => setTimeout(resolve, 100));
strategyInitSpy(productService.constructor.name, connection.name);
}

async destroy() {
await new Promise(resolve => setTimeout(resolve, 100));
await new Promise((resolve) => setTimeout(resolve, 100));
strategyDestroySpy();
}
}

const testShippingEligChecker = new ShippingEligibilityChecker({
class ConnectedShippingEligibilityChecker extends ShippingEligibilityChecker {
connection: Connection;
productService: ProductService;
}

const testShippingEligChecker = new ConnectedShippingEligibilityChecker({
code: 'test',
args: {},
description: [],
init: async injector => {
const productService = injector.get(ProductService);
const connection = injector.getConnection();
await new Promise(resolve => setTimeout(resolve, 100));
codInitSpy(productService.constructor.name, connection.name);
async init(this: ConnectedShippingEligibilityChecker, injector) {
this.productService = injector.get(ProductService);
this.connection = injector.getConnection();
await new Promise((resolve) => setTimeout(resolve, 100));
codInitSpy(this.productService.constructor.name, this.connection.name);
},
destroy: async () => {
await new Promise(resolve => setTimeout(resolve, 100));
async destroy() {
await new Promise((resolve) => setTimeout(resolve, 100));
codDestroySpy();
},
check: order => {
check(this: ConnectedShippingEligibilityChecker, order) {
codCheckSpy(this.productService.constructor.name, this.connection.name);
return true;
},
});
Expand Down Expand Up @@ -83,6 +92,7 @@ describe('lifecycle hooks for configurable objects', () => {
describe('configurable operation', () => {
beforeAll(async () => {
await server.bootstrap();
testShippingEligChecker.check(new Order({}), []);
});

it('runs init with Injector', () => {
Expand All @@ -91,6 +101,12 @@ describe('lifecycle hooks for configurable objects', () => {
expect(codInitSpy.mock.calls[0][1]).toBe('default');
});

it('runs check with injected values on this', () => {
expect(codCheckSpy).toHaveBeenCalled();
expect(codCheckSpy.mock.calls[0][0]).toEqual('ProductService');
expect(codCheckSpy.mock.calls[0][1]).toBe('default');
});

it('runs destroy', async () => {
await server.destroy();
expect(codDestroySpy).toHaveBeenCalled();
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/common/configurable-operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,12 @@ export class ConfigurableOperationDef<T extends ConfigArgs<ConfigArgType>> {

async init(injector: Injector) {
if (typeof this.options.init === 'function') {
await this.options.init(injector);
await this.options.init.call(this, injector);
}
}
async destroy() {
if (typeof this.options.destroy === 'function') {
await this.options.destroy();
await this.options.destroy.call(this);
}
}
}
Expand Down Expand Up @@ -221,9 +221,9 @@ function localizeConfig(
}

function localizeString(stringArray: LocalizedStringArray, languageCode: LanguageCode): string {
let match = stringArray.find(x => x.languageCode === languageCode);
let match = stringArray.find((x) => x.languageCode === languageCode);
if (!match) {
match = stringArray.find(x => x.languageCode === DEFAULT_LANGUAGE_CODE);
match = stringArray.find((x) => x.languageCode === DEFAULT_LANGUAGE_CODE);
}
if (!match) {
match = stringArray[0];
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/config/collection/collection-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export interface CollectionFilterConfig<T extends CollectionFilterArgs>
apply: ApplyCollectionFilterFn<T>;
}

/* tslint:disable:max-line-length */
/**
* @description
* A CollectionFilter defines a rule which can be used to associate ProductVariants with a Collection.
Expand All @@ -35,12 +36,13 @@ export interface CollectionFilterConfig<T extends CollectionFilterArgs>
*
* @docsCategory configuration
*/
/* tslint:enable:max-line-length*/
export class CollectionFilter<T extends CollectionFilterArgs = {}> extends ConfigurableOperationDef<T> {
private readonly applyFn: ApplyCollectionFilterFn<T>;

constructor(config: CollectionFilterConfig<T>) {
super(config);
this.applyFn = config.apply;
this.applyFn = config.apply.bind(this);
}

apply(qb: SelectQueryBuilder<ProductVariant>, args: ConfigArg[]): SelectQueryBuilder<ProductVariant> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ export class PaymentMethodHandler<

constructor(config: PaymentMethodConfigOptions<T>) {
super(config);
this.createPaymentFn = config.createPayment;
this.settlePaymentFn = config.settlePayment;
this.settlePaymentFn = config.settlePayment;
this.createRefundFn = config.createRefund;
this.onTransitionStartFn = config.onStateTransitionStart;
this.createPaymentFn = config.createPayment.bind(this);
this.settlePaymentFn = config.settlePayment.bind(this);
this.settlePaymentFn = config.settlePayment.bind(this);
this.createRefundFn = config.createRefund && config.createRefund.bind(this);
this.onTransitionStartFn = config.onStateTransitionStart && config.onStateTransitionStart.bind(this);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config/promotion/promotion-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export class PromotionItemAction<T extends PromotionActionArgs = {}> extends Pro
private readonly executeFn: ExecutePromotionItemActionFn<T>;
constructor(config: PromotionItemActionConfig<T>) {
super(config);
this.executeFn = config.execute;
this.executeFn = config.execute.bind(this);
}

/** @internal */
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/config/promotion/promotion-condition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export class PromotionCondition<T extends PromotionConditionArgs = {}> extends C

constructor(config: PromotionConditionConfig<T>) {
super(config);
this.checkFn = config.check;
this.checkFn = config.check.bind(this);
this.priorityValue = config.priorityValue || 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class ShippingCalculator<T extends ShippingCalculatorArgs = {}> extends C

constructor(config: ShippingCalculatorConfig<T>) {
super(config);
this.calculateFn = config.calculate;
this.calculateFn = config.calculate.bind(this);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class ShippingEligibilityChecker<

constructor(config: ShippingEligibilityCheckerConfig<T>) {
super(config);
this.checkFn = config.check;
this.checkFn = config.check.bind(this);
}

/**
Expand Down

0 comments on commit 75c80b9

Please sign in to comment.