configurable-operation.e2e-spec.ts 4.8 KB

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