configurable-operation.e2e-spec.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 gql from 'graphql-tag';
  10. import path from 'path';
  11. import { initialData } from '../../../e2e-common/e2e-initial-data';
  12. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  13. import { UpdateShippingMethodMutationVariables } from './graphql/generated-e2e-admin-types';
  14. import { GetCheckersQuery, UpdateShippingMethodMutation } from './graphql/generated-e2e-admin-types';
  15. import { UPDATE_SHIPPING_METHOD } from './graphql/shared-definitions';
  16. import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
  17. const testShippingEligibilityChecker = new ShippingEligibilityChecker({
  18. code: 'test-checker',
  19. description: [{ languageCode: LanguageCode.en, value: 'test checker' }],
  20. args: {
  21. optional: {
  22. label: [
  23. { languageCode: LanguageCode.en, value: 'Optional argument' },
  24. { languageCode: LanguageCode.de, value: 'Optional eingabe' },
  25. ],
  26. description: [
  27. { languageCode: LanguageCode.en, value: 'This is an optional argument' },
  28. { languageCode: LanguageCode.de, value: 'Das ist eine optionale eingabe' },
  29. ],
  30. required: false,
  31. type: 'string',
  32. },
  33. required: {
  34. required: true,
  35. type: 'string',
  36. defaultValue: 'hello',
  37. },
  38. },
  39. check: ctx => true,
  40. });
  41. describe('Configurable operations', () => {
  42. const { server, adminClient, shopClient } = createTestEnvironment(
  43. mergeConfig(testConfig(), {
  44. shippingOptions: {
  45. shippingEligibilityCheckers: [
  46. defaultShippingEligibilityChecker,
  47. testShippingEligibilityChecker,
  48. ],
  49. },
  50. }),
  51. );
  52. beforeAll(async () => {
  53. await server.init({
  54. initialData,
  55. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  56. customerCount: 1,
  57. });
  58. await adminClient.asSuperAdmin();
  59. }, TEST_SETUP_TIMEOUT_MS);
  60. afterAll(async () => {
  61. await server.destroy();
  62. });
  63. describe('required args', () => {
  64. it('allows empty optional arg', async () => {
  65. const { updateShippingMethod } = await adminClient.query<
  66. UpdateShippingMethodMutation,
  67. UpdateShippingMethodMutationVariables
  68. >(UPDATE_SHIPPING_METHOD, {
  69. input: {
  70. id: 'T_1',
  71. checker: {
  72. code: testShippingEligibilityChecker.code,
  73. arguments: [
  74. { name: 'optional', value: '' },
  75. { name: 'required', value: 'foo' },
  76. ],
  77. },
  78. translations: [],
  79. },
  80. });
  81. expect(updateShippingMethod.checker.args).toEqual([
  82. {
  83. name: 'optional',
  84. value: '',
  85. },
  86. {
  87. name: 'required',
  88. value: 'foo',
  89. },
  90. ]);
  91. });
  92. it(
  93. 'throws if a required arg is null',
  94. assertThrowsWithMessage(async () => {
  95. await adminClient.query<UpdateShippingMethodMutation, UpdateShippingMethodMutationVariables>(
  96. UPDATE_SHIPPING_METHOD,
  97. {
  98. input: {
  99. id: 'T_1',
  100. checker: {
  101. code: testShippingEligibilityChecker.code,
  102. arguments: [
  103. { name: 'optional', value: 'null' },
  104. { name: 'required', value: '' },
  105. ],
  106. },
  107. translations: [],
  108. },
  109. },
  110. );
  111. }, "The argument 'required' is required"),
  112. );
  113. });
  114. it('defaultValue', async () => {
  115. const { shippingEligibilityCheckers } = await adminClient.query<GetCheckersQuery>(GET_CHECKERS);
  116. expect(shippingEligibilityCheckers[1].args.map(pick(['name', 'defaultValue']))).toEqual([
  117. { name: 'optional', defaultValue: null },
  118. { name: 'required', defaultValue: 'hello' },
  119. ]);
  120. });
  121. });
  122. export const GET_CHECKERS = gql`
  123. query GetCheckers {
  124. shippingEligibilityCheckers {
  125. code
  126. args {
  127. defaultValue
  128. description
  129. label
  130. list
  131. name
  132. required
  133. type
  134. }
  135. }
  136. }
  137. `;