shipping-method.e2e-spec.ts 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* tslint:disable: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 { initialData } from '../../../e2e-common/e2e-initial-data';
  11. import { testConfig, TEST_SETUP_TIMEOUT_MS } from '../../../e2e-common/test-config';
  12. import { manualFulfillmentHandler } from '../src/config/fulfillment/manual-fulfillment-handler';
  13. import { SHIPPING_METHOD_FRAGMENT } from './graphql/fragments';
  14. import * as Codegen from './graphql/generated-e2e-admin-types';
  15. import { DeletionResult, LanguageCode } from './graphql/generated-e2e-admin-types';
  16. import {
  17. CREATE_SHIPPING_METHOD,
  18. DELETE_SHIPPING_METHOD,
  19. GET_SHIPPING_METHOD_LIST,
  20. UPDATE_SHIPPING_METHOD,
  21. } from './graphql/shared-definitions';
  22. const TEST_METADATA = {
  23. foo: 'bar',
  24. baz: [1, 2, 3],
  25. };
  26. const calculatorWithMetadata = new ShippingCalculator({
  27. code: 'calculator-with-metadata',
  28. description: [{ languageCode: LanguageCode.en, value: 'Has metadata' }],
  29. args: {},
  30. calculate: () => {
  31. return {
  32. price: 100,
  33. priceIncludesTax: true,
  34. taxRate: 0,
  35. metadata: TEST_METADATA,
  36. };
  37. },
  38. });
  39. describe('ShippingMethod resolver', () => {
  40. const { server, adminClient, shopClient } = createTestEnvironment({
  41. ...testConfig(),
  42. shippingOptions: {
  43. shippingEligibilityCheckers: [defaultShippingEligibilityChecker],
  44. shippingCalculators: [defaultShippingCalculator, calculatorWithMetadata],
  45. },
  46. });
  47. beforeAll(async () => {
  48. await server.init({
  49. initialData,
  50. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  51. customerCount: 1,
  52. });
  53. await adminClient.asSuperAdmin();
  54. }, TEST_SETUP_TIMEOUT_MS);
  55. afterAll(async () => {
  56. await server.destroy();
  57. });
  58. it('shippingEligibilityCheckers', async () => {
  59. const { shippingEligibilityCheckers } = await adminClient.query<Codegen.GetEligibilityCheckersQuery>(
  60. GET_ELIGIBILITY_CHECKERS,
  61. );
  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 } = await adminClient.query<Codegen.GetShippingMethodListQuery>(
  142. GET_SHIPPING_METHOD_LIST,
  143. );
  144. expect(shippingMethods.totalItems).toEqual(2);
  145. expect(shippingMethods.items[0].code).toBe('standard-shipping');
  146. expect(shippingMethods.items[1].code).toBe('express-shipping');
  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_3',
  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_3',
  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. });
  276. it('updateShippingMethod', async () => {
  277. const { updateShippingMethod } = await adminClient.query<
  278. Codegen.UpdateShippingMethodMutation,
  279. Codegen.UpdateShippingMethodMutationVariables
  280. >(UPDATE_SHIPPING_METHOD, {
  281. input: {
  282. id: 'T_3',
  283. translations: [{ languageCode: LanguageCode.en, name: 'changed method', description: '' }],
  284. },
  285. });
  286. expect(updateShippingMethod.name).toBe('changed method');
  287. });
  288. it('deleteShippingMethod', async () => {
  289. const listResult1 = await adminClient.query<Codegen.GetShippingMethodListQuery>(
  290. GET_SHIPPING_METHOD_LIST,
  291. );
  292. expect(listResult1.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3']);
  293. const { deleteShippingMethod } = await adminClient.query<
  294. Codegen.DeleteShippingMethodMutation,
  295. Codegen.DeleteShippingMethodMutationVariables
  296. >(DELETE_SHIPPING_METHOD, {
  297. id: 'T_3',
  298. });
  299. expect(deleteShippingMethod).toEqual({
  300. result: DeletionResult.DELETED,
  301. message: null,
  302. });
  303. const listResult2 = await adminClient.query<Codegen.GetShippingMethodListQuery>(
  304. GET_SHIPPING_METHOD_LIST,
  305. );
  306. expect(listResult2.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2']);
  307. });
  308. describe('argument ordering', () => {
  309. it('createShippingMethod corrects order of arguments', async () => {
  310. const { createShippingMethod } = await adminClient.query<
  311. Codegen.CreateShippingMethodMutation,
  312. Codegen.CreateShippingMethodMutationVariables
  313. >(CREATE_SHIPPING_METHOD, {
  314. input: {
  315. code: 'new-method',
  316. fulfillmentHandler: manualFulfillmentHandler.code,
  317. checker: {
  318. code: defaultShippingEligibilityChecker.code,
  319. arguments: [
  320. {
  321. name: 'orderMinimum',
  322. value: '0',
  323. },
  324. ],
  325. },
  326. calculator: {
  327. code: defaultShippingCalculator.code,
  328. arguments: [
  329. { name: 'rate', value: '500' },
  330. { name: 'taxRate', value: '20' },
  331. { name: 'includesTax', value: 'include' },
  332. ],
  333. },
  334. translations: [{ languageCode: LanguageCode.en, name: 'new method', description: '' }],
  335. },
  336. });
  337. expect(createShippingMethod.calculator).toEqual({
  338. code: defaultShippingCalculator.code,
  339. args: [
  340. { name: 'rate', value: '500' },
  341. { name: 'includesTax', value: 'include' },
  342. { name: 'taxRate', value: '20' },
  343. ],
  344. });
  345. });
  346. it('updateShippingMethod corrects order of arguments', async () => {
  347. const { updateShippingMethod } = await adminClient.query<
  348. Codegen.UpdateShippingMethodMutation,
  349. Codegen.UpdateShippingMethodMutationVariables
  350. >(UPDATE_SHIPPING_METHOD, {
  351. input: {
  352. id: 'T_4',
  353. translations: [],
  354. calculator: {
  355. code: defaultShippingCalculator.code,
  356. arguments: [
  357. { name: 'rate', value: '500' },
  358. { name: 'taxRate', value: '20' },
  359. { name: 'includesTax', value: 'include' },
  360. ],
  361. },
  362. },
  363. });
  364. expect(updateShippingMethod.calculator).toEqual({
  365. code: defaultShippingCalculator.code,
  366. args: [
  367. { name: 'rate', value: '500' },
  368. { name: 'includesTax', value: 'include' },
  369. { name: 'taxRate', value: '20' },
  370. ],
  371. });
  372. });
  373. it('get shippingMethod preserves correct ordering', async () => {
  374. const { shippingMethod } = await adminClient.query<
  375. Codegen.GetShippingMethodQuery,
  376. Codegen.GetShippingMethodQueryVariables
  377. >(GET_SHIPPING_METHOD, {
  378. id: 'T_4',
  379. });
  380. expect(shippingMethod?.calculator.args).toEqual([
  381. { name: 'rate', value: '500' },
  382. { name: 'includesTax', value: 'include' },
  383. { name: 'taxRate', value: '20' },
  384. ]);
  385. });
  386. it('testShippingMethod corrects order of arguments', async () => {
  387. const { testShippingMethod } = await adminClient.query<
  388. Codegen.TestShippingMethodQuery,
  389. Codegen.TestShippingMethodQueryVariables
  390. >(TEST_SHIPPING_METHOD, {
  391. input: {
  392. calculator: {
  393. code: defaultShippingCalculator.code,
  394. arguments: [
  395. { name: 'rate', value: '500' },
  396. { name: 'taxRate', value: '0' },
  397. { name: 'includesTax', value: 'include' },
  398. ],
  399. },
  400. checker: {
  401. code: defaultShippingEligibilityChecker.code,
  402. arguments: [
  403. {
  404. name: 'orderMinimum',
  405. value: '0',
  406. },
  407. ],
  408. },
  409. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  410. shippingAddress: {
  411. streetLine1: '',
  412. countryCode: 'GB',
  413. },
  414. },
  415. });
  416. expect(testShippingMethod).toEqual({
  417. eligible: true,
  418. quote: {
  419. metadata: null,
  420. price: 500,
  421. priceWithTax: 500,
  422. },
  423. });
  424. });
  425. });
  426. });
  427. const GET_SHIPPING_METHOD = gql`
  428. query GetShippingMethod($id: ID!) {
  429. shippingMethod(id: $id) {
  430. ...ShippingMethod
  431. }
  432. }
  433. ${SHIPPING_METHOD_FRAGMENT}
  434. `;
  435. const GET_ELIGIBILITY_CHECKERS = gql`
  436. query GetEligibilityCheckers {
  437. shippingEligibilityCheckers {
  438. code
  439. description
  440. args {
  441. name
  442. type
  443. description
  444. label
  445. ui
  446. }
  447. }
  448. }
  449. `;
  450. const GET_CALCULATORS = gql`
  451. query GetCalculators {
  452. shippingCalculators {
  453. code
  454. description
  455. args {
  456. name
  457. type
  458. description
  459. label
  460. ui
  461. }
  462. }
  463. }
  464. `;
  465. const TEST_SHIPPING_METHOD = gql`
  466. query TestShippingMethod($input: TestShippingMethodInput!) {
  467. testShippingMethod(input: $input) {
  468. eligible
  469. quote {
  470. price
  471. priceWithTax
  472. metadata
  473. }
  474. }
  475. }
  476. `;
  477. export const TEST_ELIGIBLE_SHIPPING_METHODS = gql`
  478. query TestEligibleMethods($input: TestEligibleShippingMethodsInput!) {
  479. testEligibleShippingMethods(input: $input) {
  480. id
  481. name
  482. description
  483. price
  484. priceWithTax
  485. metadata
  486. }
  487. }
  488. `;