shipping-method.e2e-spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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 { CREATE_SHIPPING_METHOD, UPDATE_SHIPPING_METHOD } from './graphql/shared-definitions';
  28. const TEST_METADATA = {
  29. foo: 'bar',
  30. baz: [1, 2, 3],
  31. };
  32. const calculatorWithMetadata = new ShippingCalculator({
  33. code: 'calculator-with-metadata',
  34. description: [{ languageCode: LanguageCode.en, value: 'Has metadata' }],
  35. args: {},
  36. calculate: () => {
  37. return {
  38. price: 100,
  39. priceIncludesTax: true,
  40. taxRate: 0,
  41. metadata: TEST_METADATA,
  42. };
  43. },
  44. });
  45. describe('ShippingMethod resolver', () => {
  46. const { server, adminClient, shopClient } = createTestEnvironment({
  47. ...testConfig,
  48. shippingOptions: {
  49. shippingEligibilityCheckers: [defaultShippingEligibilityChecker],
  50. shippingCalculators: [defaultShippingCalculator, calculatorWithMetadata],
  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. it('shippingEligibilityCheckers', async () => {
  65. const { shippingEligibilityCheckers } = await adminClient.query<GetEligibilityCheckers.Query>(
  66. GET_ELIGIBILITY_CHECKERS,
  67. );
  68. expect(shippingEligibilityCheckers).toEqual([
  69. {
  70. args: [
  71. {
  72. description: 'Order is eligible only if its total is greater or equal to this value',
  73. label: 'Minimum order value',
  74. name: 'orderMinimum',
  75. type: 'int',
  76. ui: {
  77. component: 'currency-form-input',
  78. },
  79. },
  80. ],
  81. code: 'default-shipping-eligibility-checker',
  82. description: 'Default Shipping Eligibility Checker',
  83. },
  84. ]);
  85. });
  86. it('shippingCalculators', async () => {
  87. const { shippingCalculators } = await adminClient.query<GetCalculators.Query>(GET_CALCULATORS);
  88. expect(shippingCalculators).toEqual([
  89. {
  90. args: [
  91. {
  92. ui: {
  93. component: 'currency-form-input',
  94. },
  95. description: null,
  96. label: 'Shipping price',
  97. name: 'rate',
  98. type: 'int',
  99. },
  100. {
  101. label: 'Price includes tax',
  102. name: 'includesTax',
  103. type: 'string',
  104. description: null,
  105. ui: {
  106. component: 'select-form-input',
  107. options: [
  108. {
  109. label: [{ languageCode: LanguageCode.en, value: 'Includes tax' }],
  110. value: 'include',
  111. },
  112. {
  113. label: [{ languageCode: LanguageCode.en, value: 'Excludes tax' }],
  114. value: 'exclude',
  115. },
  116. {
  117. label: [
  118. { languageCode: LanguageCode.en, value: 'Auto (based on Channel)' },
  119. ],
  120. value: 'auto',
  121. },
  122. ],
  123. },
  124. },
  125. {
  126. ui: {
  127. component: 'number-form-input',
  128. suffix: '%',
  129. },
  130. description: null,
  131. label: 'Tax rate',
  132. name: 'taxRate',
  133. type: 'int',
  134. },
  135. ],
  136. code: 'default-shipping-calculator',
  137. description: 'Default Flat-Rate Shipping Calculator',
  138. },
  139. {
  140. args: [],
  141. code: 'calculator-with-metadata',
  142. description: 'Has metadata',
  143. },
  144. ]);
  145. });
  146. it('shippingMethods', async () => {
  147. const { shippingMethods } = await adminClient.query<GetShippingMethodList.Query>(
  148. GET_SHIPPING_METHOD_LIST,
  149. );
  150. expect(shippingMethods.totalItems).toEqual(2);
  151. expect(shippingMethods.items[0].code).toBe('standard-shipping');
  152. expect(shippingMethods.items[1].code).toBe('express-shipping');
  153. });
  154. it('shippingMethod', async () => {
  155. const { shippingMethod } = await adminClient.query<
  156. GetShippingMethod.Query,
  157. GetShippingMethod.Variables
  158. >(GET_SHIPPING_METHOD, {
  159. id: 'T_1',
  160. });
  161. expect(shippingMethod!.code).toBe('standard-shipping');
  162. });
  163. it('createShippingMethod', async () => {
  164. const { createShippingMethod } = await adminClient.query<
  165. CreateShippingMethod.Mutation,
  166. CreateShippingMethod.Variables
  167. >(CREATE_SHIPPING_METHOD, {
  168. input: {
  169. code: 'new-method',
  170. fulfillmentHandler: manualFulfillmentHandler.code,
  171. checker: {
  172. code: defaultShippingEligibilityChecker.code,
  173. arguments: [
  174. {
  175. name: 'orderMinimum',
  176. value: '0',
  177. },
  178. ],
  179. },
  180. calculator: {
  181. code: calculatorWithMetadata.code,
  182. arguments: [],
  183. },
  184. translations: [{ languageCode: LanguageCode.en, name: 'new method', description: '' }],
  185. },
  186. });
  187. expect(createShippingMethod).toEqual({
  188. id: 'T_3',
  189. code: 'new-method',
  190. name: 'new method',
  191. description: '',
  192. calculator: {
  193. code: 'calculator-with-metadata',
  194. args: [],
  195. },
  196. checker: {
  197. code: 'default-shipping-eligibility-checker',
  198. args: [
  199. {
  200. name: 'orderMinimum',
  201. value: '0',
  202. },
  203. ],
  204. },
  205. });
  206. });
  207. it('testShippingMethod', async () => {
  208. const { testShippingMethod } = await adminClient.query<
  209. TestShippingMethod.Query,
  210. TestShippingMethod.Variables
  211. >(TEST_SHIPPING_METHOD, {
  212. input: {
  213. calculator: {
  214. code: calculatorWithMetadata.code,
  215. arguments: [],
  216. },
  217. checker: {
  218. code: defaultShippingEligibilityChecker.code,
  219. arguments: [
  220. {
  221. name: 'orderMinimum',
  222. value: '0',
  223. },
  224. ],
  225. },
  226. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  227. shippingAddress: {
  228. streetLine1: '',
  229. countryCode: 'GB',
  230. },
  231. },
  232. });
  233. expect(testShippingMethod).toEqual({
  234. eligible: true,
  235. quote: {
  236. price: 100,
  237. priceWithTax: 100,
  238. metadata: TEST_METADATA,
  239. },
  240. });
  241. });
  242. it('testEligibleShippingMethods', async () => {
  243. const { testEligibleShippingMethods } = await adminClient.query<
  244. TestEligibleMethods.Query,
  245. TestEligibleMethods.Variables
  246. >(TEST_ELIGIBLE_SHIPPING_METHODS, {
  247. input: {
  248. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  249. shippingAddress: {
  250. streetLine1: '',
  251. countryCode: 'GB',
  252. },
  253. },
  254. });
  255. expect(testEligibleShippingMethods).toEqual([
  256. {
  257. id: 'T_3',
  258. name: 'new method',
  259. description: '',
  260. price: 100,
  261. priceWithTax: 100,
  262. metadata: TEST_METADATA,
  263. },
  264. {
  265. id: 'T_1',
  266. name: 'Standard Shipping',
  267. description: '',
  268. price: 500,
  269. priceWithTax: 500,
  270. metadata: null,
  271. },
  272. {
  273. id: 'T_2',
  274. name: 'Express Shipping',
  275. description: '',
  276. price: 1000,
  277. priceWithTax: 1000,
  278. metadata: null,
  279. },
  280. ]);
  281. });
  282. it('updateShippingMethod', async () => {
  283. const { updateShippingMethod } = await adminClient.query<
  284. UpdateShippingMethod.Mutation,
  285. UpdateShippingMethod.Variables
  286. >(UPDATE_SHIPPING_METHOD, {
  287. input: {
  288. id: 'T_3',
  289. translations: [{ languageCode: LanguageCode.en, name: 'changed method', description: '' }],
  290. },
  291. });
  292. expect(updateShippingMethod.name).toBe('changed method');
  293. });
  294. it('deleteShippingMethod', async () => {
  295. const listResult1 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  296. expect(listResult1.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3']);
  297. const { deleteShippingMethod } = await adminClient.query<
  298. DeleteShippingMethod.Mutation,
  299. DeleteShippingMethod.Variables
  300. >(DELETE_SHIPPING_METHOD, {
  301. id: 'T_3',
  302. });
  303. expect(deleteShippingMethod).toEqual({
  304. result: DeletionResult.DELETED,
  305. message: null,
  306. });
  307. const listResult2 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  308. expect(listResult2.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2']);
  309. });
  310. });
  311. const GET_SHIPPING_METHOD_LIST = gql`
  312. query GetShippingMethodList {
  313. shippingMethods {
  314. items {
  315. ...ShippingMethod
  316. }
  317. totalItems
  318. }
  319. }
  320. ${SHIPPING_METHOD_FRAGMENT}
  321. `;
  322. const GET_SHIPPING_METHOD = gql`
  323. query GetShippingMethod($id: ID!) {
  324. shippingMethod(id: $id) {
  325. ...ShippingMethod
  326. }
  327. }
  328. ${SHIPPING_METHOD_FRAGMENT}
  329. `;
  330. const DELETE_SHIPPING_METHOD = gql`
  331. mutation DeleteShippingMethod($id: ID!) {
  332. deleteShippingMethod(id: $id) {
  333. result
  334. message
  335. }
  336. }
  337. `;
  338. const GET_ELIGIBILITY_CHECKERS = gql`
  339. query GetEligibilityCheckers {
  340. shippingEligibilityCheckers {
  341. code
  342. description
  343. args {
  344. name
  345. type
  346. description
  347. label
  348. ui
  349. }
  350. }
  351. }
  352. `;
  353. const GET_CALCULATORS = gql`
  354. query GetCalculators {
  355. shippingCalculators {
  356. code
  357. description
  358. args {
  359. name
  360. type
  361. description
  362. label
  363. ui
  364. }
  365. }
  366. }
  367. `;
  368. const TEST_SHIPPING_METHOD = gql`
  369. query TestShippingMethod($input: TestShippingMethodInput!) {
  370. testShippingMethod(input: $input) {
  371. eligible
  372. quote {
  373. price
  374. priceWithTax
  375. metadata
  376. }
  377. }
  378. }
  379. `;
  380. export const TEST_ELIGIBLE_SHIPPING_METHODS = gql`
  381. query TestEligibleMethods($input: TestEligibleShippingMethodsInput!) {
  382. testEligibleShippingMethods(input: $input) {
  383. id
  384. name
  385. description
  386. price
  387. priceWithTax
  388. metadata
  389. }
  390. }
  391. `;