configurable-operation.e2e-spec.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { pick } from '@vendure/common/lib/pick';
  2. import {
  3. defaultShippingEligibilityChecker,
  4. LanguageCode,
  5. mergeConfig,
  6. ShippingEligibilityChecker,
  7. } from '@vendure/core';
  8. import { createTestEnvironment } from '@vendure/testing';
  9. import path from 'path';
  10. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  11. import { initialData } from '../../../e2e-common/e2e-initial-data';
  12. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  13. import { getCheckersDocument, updateShippingMethodDocument } from './graphql/shared-definitions';
  14. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  15. const testShippingEligibilityChecker = new ShippingEligibilityChecker({
  16. code: 'test-checker',
  17. description: [{ languageCode: LanguageCode.en, value: 'test checker' }],
  18. args: {
  19. optional: {
  20. label: [
  21. { languageCode: LanguageCode.en, value: 'Optional argument' },
  22. { languageCode: LanguageCode.de, value: 'Optional eingabe' },
  23. ],
  24. description: [
  25. { languageCode: LanguageCode.en, value: 'This is an optional argument' },
  26. { languageCode: LanguageCode.de, value: 'Das ist eine optionale eingabe' },
  27. ],
  28. required: false,
  29. type: 'string',
  30. },
  31. required: {
  32. required: true,
  33. type: 'string',
  34. defaultValue: 'hello',
  35. },
  36. },
  37. check: ctx => true,
  38. });
  39. describe('Configurable operations', () => {
  40. const { server, adminClient, shopClient } = createTestEnvironment(
  41. mergeConfig(testConfig(), {
  42. shippingOptions: {
  43. shippingEligibilityCheckers: [
  44. defaultShippingEligibilityChecker,
  45. testShippingEligibilityChecker,
  46. ],
  47. },
  48. }),
  49. );
  50. beforeAll(async () => {
  51. await server.init({
  52. initialData,
  53. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  54. customerCount: 1,
  55. });
  56. await adminClient.asSuperAdmin();
  57. }, TEST_SETUP_TIMEOUT_MS);
  58. afterAll(async () => {
  59. await server.destroy();
  60. });
  61. describe('required args', () => {
  62. it('allows empty optional arg', async () => {
  63. const { updateShippingMethod } = await adminClient.query(updateShippingMethodDocument, {
  64. input: {
  65. id: 'T_1',
  66. checker: {
  67. code: testShippingEligibilityChecker.code,
  68. arguments: [
  69. { name: 'optional', value: '' },
  70. { name: 'required', value: 'foo' },
  71. ],
  72. },
  73. translations: [],
  74. },
  75. });
  76. expect(updateShippingMethod.checker.args).toEqual([
  77. {
  78. name: 'optional',
  79. value: '',
  80. },
  81. {
  82. name: 'required',
  83. value: 'foo',
  84. },
  85. ]);
  86. });
  87. it(
  88. 'throws if a required arg is null',
  89. assertThrowsWithMessage(async () => {
  90. await adminClient.query(updateShippingMethodDocument, {
  91. input: {
  92. id: 'T_1',
  93. checker: {
  94. code: testShippingEligibilityChecker.code,
  95. arguments: [
  96. { name: 'optional', value: 'null' },
  97. { name: 'required', value: '' },
  98. ],
  99. },
  100. translations: [],
  101. },
  102. });
  103. }, 'The argument "required" is required'),
  104. );
  105. });
  106. it('defaultValue', async () => {
  107. const { shippingEligibilityCheckers } = await adminClient.query(getCheckersDocument);
  108. expect(shippingEligibilityCheckers[1].args.map(pick(['name', 'defaultValue']))).toEqual([
  109. { name: 'optional', defaultValue: null },
  110. { name: 'required', defaultValue: 'hello' },
  111. ]);
  112. });
  113. });