configurable-operation.e2e-spec.ts 5.0 KB

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