shipping-method.e2e-spec.ts 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /* eslint-disable @typescript-eslint/no-non-null-assertion */
  2. import {
  3. defaultShippingCalculator,
  4. defaultShippingEligibilityChecker,
  5. ShippingCalculator,
  6. } from '@vendure/core';
  7. import { createTestEnvironment } from '@vendure/testing';
  8. import gql from 'graphql-tag';
  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 { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  13. import { manualFulfillmentHandler } from '../src/config/fulfillment/manual-fulfillment-handler';
  14. import { SHIPPING_METHOD_FRAGMENT } from './graphql/fragments';
  15. import * as Codegen from './graphql/generated-e2e-admin-types';
  16. import { DeletionResult, LanguageCode } from './graphql/generated-e2e-admin-types';
  17. import {
  18. CREATE_SHIPPING_METHOD,
  19. DELETE_SHIPPING_METHOD,
  20. GET_SHIPPING_METHOD_LIST,
  21. UPDATE_SHIPPING_METHOD,
  22. } from './graphql/shared-definitions';
  23. const TEST_METADATA = {
  24. foo: 'bar',
  25. baz: [1, 2, 3],
  26. };
  27. const calculatorWithMetadata = new ShippingCalculator({
  28. code: 'calculator-with-metadata',
  29. description: [{ languageCode: LanguageCode.en, value: 'Has metadata' }],
  30. args: {},
  31. calculate: () => {
  32. return {
  33. price: 100,
  34. priceIncludesTax: true,
  35. taxRate: 0,
  36. metadata: TEST_METADATA,
  37. };
  38. },
  39. });
  40. describe('ShippingMethod resolver', () => {
  41. const { server, adminClient, shopClient } = createTestEnvironment({
  42. ...testConfig(),
  43. shippingOptions: {
  44. shippingEligibilityCheckers: [defaultShippingEligibilityChecker],
  45. shippingCalculators: [defaultShippingCalculator, calculatorWithMetadata],
  46. },
  47. });
  48. beforeAll(async () => {
  49. await server.init({
  50. initialData,
  51. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  52. customerCount: 1,
  53. });
  54. await adminClient.asSuperAdmin();
  55. }, TEST_SETUP_TIMEOUT_MS);
  56. afterAll(async () => {
  57. await server.destroy();
  58. });
  59. it('shippingEligibilityCheckers', async () => {
  60. const { shippingEligibilityCheckers } =
  61. await adminClient.query<Codegen.GetEligibilityCheckersQuery>(GET_ELIGIBILITY_CHECKERS);
  62. expect(shippingEligibilityCheckers).toEqual([
  63. {
  64. args: [
  65. {
  66. description: 'Order is eligible only if its total is greater or equal to this value',
  67. label: 'Minimum order value',
  68. name: 'orderMinimum',
  69. type: 'int',
  70. ui: {
  71. component: 'currency-form-input',
  72. },
  73. },
  74. ],
  75. code: 'default-shipping-eligibility-checker',
  76. description: 'Default Shipping Eligibility Checker',
  77. },
  78. ]);
  79. });
  80. it('shippingCalculators', async () => {
  81. const { shippingCalculators } = await adminClient.query<Codegen.GetCalculatorsQuery>(GET_CALCULATORS);
  82. expect(shippingCalculators).toEqual([
  83. {
  84. args: [
  85. {
  86. ui: {
  87. component: 'currency-form-input',
  88. },
  89. description: null,
  90. label: 'Shipping price',
  91. name: 'rate',
  92. type: 'int',
  93. },
  94. {
  95. label: 'Price includes tax',
  96. name: 'includesTax',
  97. type: 'string',
  98. description: null,
  99. ui: {
  100. component: 'select-form-input',
  101. options: [
  102. {
  103. label: [{ languageCode: LanguageCode.en, value: 'Includes tax' }],
  104. value: 'include',
  105. },
  106. {
  107. label: [{ languageCode: LanguageCode.en, value: 'Excludes tax' }],
  108. value: 'exclude',
  109. },
  110. {
  111. label: [
  112. { languageCode: LanguageCode.en, value: 'Auto (based on Channel)' },
  113. ],
  114. value: 'auto',
  115. },
  116. ],
  117. },
  118. },
  119. {
  120. ui: {
  121. component: 'number-form-input',
  122. suffix: '%',
  123. },
  124. description: null,
  125. label: 'Tax rate',
  126. name: 'taxRate',
  127. type: 'int',
  128. },
  129. ],
  130. code: 'default-shipping-calculator',
  131. description: 'Default Flat-Rate Shipping Calculator',
  132. },
  133. {
  134. args: [],
  135. code: 'calculator-with-metadata',
  136. description: 'Has metadata',
  137. },
  138. ]);
  139. });
  140. it('shippingMethods', async () => {
  141. const { shippingMethods } =
  142. await adminClient.query<Codegen.GetShippingMethodListQuery>(GET_SHIPPING_METHOD_LIST);
  143. expect(shippingMethods.totalItems).toEqual(3);
  144. expect(shippingMethods.items[0].code).toBe('standard-shipping');
  145. expect(shippingMethods.items[1].code).toBe('express-shipping');
  146. expect(shippingMethods.items[2].code).toBe('express-shipping-taxed');
  147. });
  148. it('shippingMethod', async () => {
  149. const { shippingMethod } = await adminClient.query<
  150. Codegen.GetShippingMethodQuery,
  151. Codegen.GetShippingMethodQueryVariables
  152. >(GET_SHIPPING_METHOD, {
  153. id: 'T_1',
  154. });
  155. expect(shippingMethod!.code).toBe('standard-shipping');
  156. });
  157. it('createShippingMethod', async () => {
  158. const { createShippingMethod } = await adminClient.query<
  159. Codegen.CreateShippingMethodMutation,
  160. Codegen.CreateShippingMethodMutationVariables
  161. >(CREATE_SHIPPING_METHOD, {
  162. input: {
  163. code: 'new-method',
  164. fulfillmentHandler: manualFulfillmentHandler.code,
  165. checker: {
  166. code: defaultShippingEligibilityChecker.code,
  167. arguments: [
  168. {
  169. name: 'orderMinimum',
  170. value: '0',
  171. },
  172. ],
  173. },
  174. calculator: {
  175. code: calculatorWithMetadata.code,
  176. arguments: [],
  177. },
  178. translations: [{ languageCode: LanguageCode.en, name: 'new method', description: '' }],
  179. },
  180. });
  181. expect(createShippingMethod).toEqual({
  182. id: 'T_4',
  183. code: 'new-method',
  184. name: 'new method',
  185. description: '',
  186. calculator: {
  187. code: 'calculator-with-metadata',
  188. args: [],
  189. },
  190. checker: {
  191. code: 'default-shipping-eligibility-checker',
  192. args: [
  193. {
  194. name: 'orderMinimum',
  195. value: '0',
  196. },
  197. ],
  198. },
  199. });
  200. });
  201. it('testShippingMethod', async () => {
  202. const { testShippingMethod } = await adminClient.query<
  203. Codegen.TestShippingMethodQuery,
  204. Codegen.TestShippingMethodQueryVariables
  205. >(TEST_SHIPPING_METHOD, {
  206. input: {
  207. calculator: {
  208. code: calculatorWithMetadata.code,
  209. arguments: [],
  210. },
  211. checker: {
  212. code: defaultShippingEligibilityChecker.code,
  213. arguments: [
  214. {
  215. name: 'orderMinimum',
  216. value: '0',
  217. },
  218. ],
  219. },
  220. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  221. shippingAddress: {
  222. streetLine1: '',
  223. countryCode: 'GB',
  224. },
  225. },
  226. });
  227. expect(testShippingMethod).toEqual({
  228. eligible: true,
  229. quote: {
  230. price: 100,
  231. priceWithTax: 100,
  232. metadata: TEST_METADATA,
  233. },
  234. });
  235. });
  236. it('testEligibleShippingMethods', async () => {
  237. const { testEligibleShippingMethods } = await adminClient.query<
  238. Codegen.TestEligibleMethodsQuery,
  239. Codegen.TestEligibleMethodsQueryVariables
  240. >(TEST_ELIGIBLE_SHIPPING_METHODS, {
  241. input: {
  242. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  243. shippingAddress: {
  244. streetLine1: '',
  245. countryCode: 'GB',
  246. },
  247. },
  248. });
  249. expect(testEligibleShippingMethods).toEqual([
  250. {
  251. id: 'T_4',
  252. name: 'new method',
  253. description: '',
  254. price: 100,
  255. priceWithTax: 100,
  256. metadata: TEST_METADATA,
  257. },
  258. {
  259. id: 'T_1',
  260. name: 'Standard Shipping',
  261. description: '',
  262. price: 500,
  263. priceWithTax: 500,
  264. metadata: null,
  265. },
  266. {
  267. id: 'T_2',
  268. name: 'Express Shipping',
  269. description: '',
  270. price: 1000,
  271. priceWithTax: 1000,
  272. metadata: null,
  273. },
  274. {
  275. id: 'T_3',
  276. name: 'Express Shipping (Taxed)',
  277. description: '',
  278. price: 1000,
  279. priceWithTax: 1200,
  280. metadata: null,
  281. },
  282. ]);
  283. });
  284. it('updateShippingMethod', async () => {
  285. const { updateShippingMethod } = await adminClient.query<
  286. Codegen.UpdateShippingMethodMutation,
  287. Codegen.UpdateShippingMethodMutationVariables
  288. >(UPDATE_SHIPPING_METHOD, {
  289. input: {
  290. id: 'T_4',
  291. translations: [{ languageCode: LanguageCode.en, name: 'changed method', description: '' }],
  292. },
  293. });
  294. expect(updateShippingMethod.name).toBe('changed method');
  295. });
  296. it('deleteShippingMethod', async () => {
  297. const listResult1 =
  298. await adminClient.query<Codegen.GetShippingMethodListQuery>(GET_SHIPPING_METHOD_LIST);
  299. expect(listResult1.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3', 'T_4']);
  300. const { deleteShippingMethod } = await adminClient.query<
  301. Codegen.DeleteShippingMethodMutation,
  302. Codegen.DeleteShippingMethodMutationVariables
  303. >(DELETE_SHIPPING_METHOD, {
  304. id: 'T_4',
  305. });
  306. expect(deleteShippingMethod).toEqual({
  307. result: DeletionResult.DELETED,
  308. message: null,
  309. });
  310. const listResult2 =
  311. await adminClient.query<Codegen.GetShippingMethodListQuery>(GET_SHIPPING_METHOD_LIST);
  312. expect(listResult2.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3']);
  313. });
  314. describe('argument ordering', () => {
  315. it('createShippingMethod corrects order of arguments', async () => {
  316. const { createShippingMethod } = await adminClient.query<
  317. Codegen.CreateShippingMethodMutation,
  318. Codegen.CreateShippingMethodMutationVariables
  319. >(CREATE_SHIPPING_METHOD, {
  320. input: {
  321. code: 'new-method',
  322. fulfillmentHandler: manualFulfillmentHandler.code,
  323. checker: {
  324. code: defaultShippingEligibilityChecker.code,
  325. arguments: [
  326. {
  327. name: 'orderMinimum',
  328. value: '0',
  329. },
  330. ],
  331. },
  332. calculator: {
  333. code: defaultShippingCalculator.code,
  334. arguments: [
  335. { name: 'rate', value: '500' },
  336. { name: 'taxRate', value: '20' },
  337. { name: 'includesTax', value: 'include' },
  338. ],
  339. },
  340. translations: [{ languageCode: LanguageCode.en, name: 'new method', description: '' }],
  341. },
  342. });
  343. expect(createShippingMethod.calculator).toEqual({
  344. code: defaultShippingCalculator.code,
  345. args: [
  346. { name: 'rate', value: '500' },
  347. { name: 'includesTax', value: 'include' },
  348. { name: 'taxRate', value: '20' },
  349. ],
  350. });
  351. });
  352. it('updateShippingMethod corrects order of arguments', async () => {
  353. const { updateShippingMethod } = await adminClient.query<
  354. Codegen.UpdateShippingMethodMutation,
  355. Codegen.UpdateShippingMethodMutationVariables
  356. >(UPDATE_SHIPPING_METHOD, {
  357. input: {
  358. id: 'T_5',
  359. translations: [],
  360. calculator: {
  361. code: defaultShippingCalculator.code,
  362. arguments: [
  363. { name: 'rate', value: '500' },
  364. { name: 'taxRate', value: '20' },
  365. { name: 'includesTax', value: 'include' },
  366. ],
  367. },
  368. },
  369. });
  370. expect(updateShippingMethod.calculator).toEqual({
  371. code: defaultShippingCalculator.code,
  372. args: [
  373. { name: 'rate', value: '500' },
  374. { name: 'includesTax', value: 'include' },
  375. { name: 'taxRate', value: '20' },
  376. ],
  377. });
  378. });
  379. it('get shippingMethod preserves correct ordering', async () => {
  380. const { shippingMethod } = await adminClient.query<
  381. Codegen.GetShippingMethodQuery,
  382. Codegen.GetShippingMethodQueryVariables
  383. >(GET_SHIPPING_METHOD, {
  384. id: 'T_5',
  385. });
  386. expect(shippingMethod?.calculator.args).toEqual([
  387. { name: 'rate', value: '500' },
  388. { name: 'includesTax', value: 'include' },
  389. { name: 'taxRate', value: '20' },
  390. ]);
  391. });
  392. it('testShippingMethod corrects order of arguments', async () => {
  393. const { testShippingMethod } = await adminClient.query<
  394. Codegen.TestShippingMethodQuery,
  395. Codegen.TestShippingMethodQueryVariables
  396. >(TEST_SHIPPING_METHOD, {
  397. input: {
  398. calculator: {
  399. code: defaultShippingCalculator.code,
  400. arguments: [
  401. { name: 'rate', value: '500' },
  402. { name: 'taxRate', value: '0' },
  403. { name: 'includesTax', value: 'include' },
  404. ],
  405. },
  406. checker: {
  407. code: defaultShippingEligibilityChecker.code,
  408. arguments: [
  409. {
  410. name: 'orderMinimum',
  411. value: '0',
  412. },
  413. ],
  414. },
  415. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  416. shippingAddress: {
  417. streetLine1: '',
  418. countryCode: 'GB',
  419. },
  420. },
  421. });
  422. expect(testShippingMethod).toEqual({
  423. eligible: true,
  424. quote: {
  425. metadata: null,
  426. price: 500,
  427. priceWithTax: 500,
  428. },
  429. });
  430. });
  431. });
  432. });
  433. const GET_SHIPPING_METHOD = gql`
  434. query GetShippingMethod($id: ID!) {
  435. shippingMethod(id: $id) {
  436. ...ShippingMethod
  437. }
  438. }
  439. ${SHIPPING_METHOD_FRAGMENT}
  440. `;
  441. const GET_ELIGIBILITY_CHECKERS = gql`
  442. query GetEligibilityCheckers {
  443. shippingEligibilityCheckers {
  444. code
  445. description
  446. args {
  447. name
  448. type
  449. description
  450. label
  451. ui
  452. }
  453. }
  454. }
  455. `;
  456. const GET_CALCULATORS = gql`
  457. query GetCalculators {
  458. shippingCalculators {
  459. code
  460. description
  461. args {
  462. name
  463. type
  464. description
  465. label
  466. ui
  467. }
  468. }
  469. }
  470. `;
  471. const TEST_SHIPPING_METHOD = gql`
  472. query TestShippingMethod($input: TestShippingMethodInput!) {
  473. testShippingMethod(input: $input) {
  474. eligible
  475. quote {
  476. price
  477. priceWithTax
  478. metadata
  479. }
  480. }
  481. }
  482. `;
  483. export const TEST_ELIGIBLE_SHIPPING_METHODS = gql`
  484. query TestEligibleMethods($input: TestEligibleShippingMethodsInput!) {
  485. testEligibleShippingMethods(input: $input) {
  486. id
  487. name
  488. description
  489. price
  490. priceWithTax
  491. metadata
  492. }
  493. }
  494. `;