shipping-method.e2e-spec.ts 18 KB

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