Skip to content

Commit

Permalink
fix(core): Fix custom MoneyStrategy handling from plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Nov 21, 2023
1 parent 9b6dce6 commit a09c2b2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 12 deletions.
69 changes: 58 additions & 11 deletions packages/core/e2e/money-strategy.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { DefaultMoneyStrategy, Logger, mergeConfig, MoneyStrategy } from '@vendure/core';
import { DefaultMoneyStrategy, Logger, mergeConfig, MoneyStrategy, VendurePlugin } from '@vendure/core';
import { createErrorResultGuard, createTestEnvironment, ErrorResultGuard } from '@vendure/testing';
import path from 'path';
import { ColumnOptions } from 'typeorm';
import { afterAll, beforeAll, describe, expect, it } from 'vitest';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';

import { initialData } from '../../../e2e-common/e2e-initial-data';
import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
Expand All @@ -21,13 +21,15 @@ const orderGuard: ErrorResultGuard<CodegenShop.UpdatedOrderFragment> = createErr
);

class CustomMoneyStrategy implements MoneyStrategy {
static transformerFromSpy = vi.fn();
readonly moneyColumnOptions: ColumnOptions = {
type: 'bigint',
transformer: {
to: (entityValue: number) => {
return entityValue;
},
from: (databaseValue: string): number => {
CustomMoneyStrategy.transformerFromSpy(databaseValue);
if (databaseValue == null) {
return databaseValue;
}
Expand All @@ -48,12 +50,18 @@ class CustomMoneyStrategy implements MoneyStrategy {
}
}

@VendurePlugin({
configuration: config => {
config.entityOptions.moneyStrategy = new CustomMoneyStrategy();
return config;
},
})
class MyPlugin {}

describe('Custom MoneyStrategy', () => {
const { server, adminClient, shopClient } = createTestEnvironment(
mergeConfig(testConfig(), {
entityOptions: {
moneyStrategy: new CustomMoneyStrategy(),
},
plugins: [MyPlugin],
}),
);

Expand All @@ -74,6 +82,8 @@ describe('Custom MoneyStrategy', () => {
});

it('check initial prices', async () => {
expect(CustomMoneyStrategy.transformerFromSpy).toHaveBeenCalledTimes(0);

const { productVariants } = await adminClient.query<
Codegen.GetProductVariantListQuery,
Codegen.GetProductVariantListQueryVariables
Expand All @@ -91,6 +101,8 @@ describe('Custom MoneyStrategy', () => {

cheapVariantId = productVariants.items[0].id;
expensiveVariantId = productVariants.items[1].id;

expect(CustomMoneyStrategy.transformerFromSpy).toHaveBeenCalledTimes(6);
});

// https://github.com/vendure-ecommerce/vendure/issues/838
Expand Down Expand Up @@ -127,9 +139,44 @@ describe('Custom MoneyStrategy', () => {
expect(addItemToOrder.lines[0].linePriceWithTax).toBe(372);
});
});

class CustomRoundingStrategy extends DefaultMoneyStrategy {
round(value: number): number {
return value;
}
}
//
//
// describe('custom MoneyStrategy as part of a plugin', () => {
// const testConfigValue = testConfig();
// delete testConfigValue.entityOptions.moneyStrategy;
// const { server, adminClient, shopClient } = createTestEnvironment(
// mergeConfig(testConfigValue, {
// plugins: [MyPlugin],
// }),
// );
//
// beforeAll(async () => {
// await server.init({
// initialData,
// productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-money-handling.csv'),
// customerCount: 1,
// });
// await adminClient.asSuperAdmin();
// }, TEST_SETUP_TIMEOUT_MS);
//
// afterAll(async () => {
// await server.destroy();
// });
//
// it('invokes the transformer', async () => {
// CustomMoneyStrategy.transformerFromSpy.mockReset();
//
// expect(CustomMoneyStrategy.transformerFromSpy).toHaveBeenCalledTimes(0);
//
// await shopClient.asAnonymousUser();
// const { addItemToOrder } = await shopClient.query<
// AddItemToOrderMutation,
// AddItemToOrderMutationVariables
// >(ADD_ITEM_TO_ORDER, {
// productVariantId: 'T_1',
// quantity: 2,
// });
//
// expect(CustomMoneyStrategy.transformerFromSpy).toHaveBeenCalledTimes(2);
// });
// });
2 changes: 1 addition & 1 deletion packages/core/src/bootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export async function preBootstrapConfig(
});

let config = getConfig();
config = await runPluginConfigurations(config);
const entityIdStrategy = config.entityOptions.entityIdStrategy ?? config.entityIdStrategy;
setEntityIdStrategy(entityIdStrategy, entities);
const moneyStrategy = config.entityOptions.moneyStrategy;
Expand All @@ -156,7 +157,6 @@ export async function preBootstrapConfig(
process.exitCode = 1;
throw new Error('CustomFields config error:\n- ' + customFieldValidationResult.errors.join('\n- '));
}
config = await runPluginConfigurations(config);
registerCustomEntityFields(config);
await runEntityMetadataModifiers(config);
setExposedHeaders(config);
Expand Down

0 comments on commit a09c2b2

Please sign in to comment.