shipping-method.e2e-spec.ts 17 KB

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