shipping-method.e2e-spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 } 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. },
  195. checker: {
  196. code: 'default-shipping-eligibility-checker',
  197. },
  198. });
  199. });
  200. it('testShippingMethod', async () => {
  201. const { testShippingMethod } = await adminClient.query<
  202. TestShippingMethod.Query,
  203. TestShippingMethod.Variables
  204. >(TEST_SHIPPING_METHOD, {
  205. input: {
  206. calculator: {
  207. code: calculatorWithMetadata.code,
  208. arguments: [],
  209. },
  210. checker: {
  211. code: defaultShippingEligibilityChecker.code,
  212. arguments: [
  213. {
  214. name: 'orderMinimum',
  215. value: '0',
  216. },
  217. ],
  218. },
  219. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  220. shippingAddress: {
  221. streetLine1: '',
  222. countryCode: 'GB',
  223. },
  224. },
  225. });
  226. expect(testShippingMethod).toEqual({
  227. eligible: true,
  228. quote: {
  229. price: 100,
  230. priceWithTax: 100,
  231. metadata: TEST_METADATA,
  232. },
  233. });
  234. });
  235. it('testEligibleShippingMethods', async () => {
  236. const { testEligibleShippingMethods } = await adminClient.query<
  237. TestEligibleMethods.Query,
  238. TestEligibleMethods.Variables
  239. >(TEST_ELIGIBLE_SHIPPING_METHODS, {
  240. input: {
  241. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  242. shippingAddress: {
  243. streetLine1: '',
  244. countryCode: 'GB',
  245. },
  246. },
  247. });
  248. expect(testEligibleShippingMethods).toEqual([
  249. {
  250. id: 'T_3',
  251. name: 'new method',
  252. description: '',
  253. price: 100,
  254. priceWithTax: 100,
  255. metadata: TEST_METADATA,
  256. },
  257. {
  258. id: 'T_1',
  259. name: 'Standard Shipping',
  260. description: '',
  261. price: 500,
  262. priceWithTax: 500,
  263. metadata: null,
  264. },
  265. {
  266. id: 'T_2',
  267. name: 'Express Shipping',
  268. description: '',
  269. price: 1000,
  270. priceWithTax: 1000,
  271. metadata: null,
  272. },
  273. ]);
  274. });
  275. it('updateShippingMethod', async () => {
  276. const { updateShippingMethod } = await adminClient.query<
  277. UpdateShippingMethod.Mutation,
  278. UpdateShippingMethod.Variables
  279. >(UPDATE_SHIPPING_METHOD, {
  280. input: {
  281. id: 'T_3',
  282. translations: [{ languageCode: LanguageCode.en, name: 'changed method', description: '' }],
  283. },
  284. });
  285. expect(updateShippingMethod.name).toBe('changed method');
  286. });
  287. it('deleteShippingMethod', async () => {
  288. const listResult1 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  289. expect(listResult1.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3']);
  290. const { deleteShippingMethod } = await adminClient.query<
  291. DeleteShippingMethod.Mutation,
  292. DeleteShippingMethod.Variables
  293. >(DELETE_SHIPPING_METHOD, {
  294. id: 'T_3',
  295. });
  296. expect(deleteShippingMethod).toEqual({
  297. result: DeletionResult.DELETED,
  298. message: null,
  299. });
  300. const listResult2 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  301. expect(listResult2.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2']);
  302. });
  303. });
  304. const GET_SHIPPING_METHOD_LIST = gql`
  305. query GetShippingMethodList {
  306. shippingMethods {
  307. items {
  308. ...ShippingMethod
  309. }
  310. totalItems
  311. }
  312. }
  313. ${SHIPPING_METHOD_FRAGMENT}
  314. `;
  315. const GET_SHIPPING_METHOD = gql`
  316. query GetShippingMethod($id: ID!) {
  317. shippingMethod(id: $id) {
  318. ...ShippingMethod
  319. }
  320. }
  321. ${SHIPPING_METHOD_FRAGMENT}
  322. `;
  323. const UPDATE_SHIPPING_METHOD = gql`
  324. mutation UpdateShippingMethod($input: UpdateShippingMethodInput!) {
  325. updateShippingMethod(input: $input) {
  326. ...ShippingMethod
  327. }
  328. }
  329. ${SHIPPING_METHOD_FRAGMENT}
  330. `;
  331. const DELETE_SHIPPING_METHOD = gql`
  332. mutation DeleteShippingMethod($id: ID!) {
  333. deleteShippingMethod(id: $id) {
  334. result
  335. message
  336. }
  337. }
  338. `;
  339. const GET_ELIGIBILITY_CHECKERS = gql`
  340. query GetEligibilityCheckers {
  341. shippingEligibilityCheckers {
  342. code
  343. description
  344. args {
  345. name
  346. type
  347. description
  348. label
  349. ui
  350. }
  351. }
  352. }
  353. `;
  354. const GET_CALCULATORS = gql`
  355. query GetCalculators {
  356. shippingCalculators {
  357. code
  358. description
  359. args {
  360. name
  361. type
  362. description
  363. label
  364. ui
  365. }
  366. }
  367. }
  368. `;
  369. const TEST_SHIPPING_METHOD = gql`
  370. query TestShippingMethod($input: TestShippingMethodInput!) {
  371. testShippingMethod(input: $input) {
  372. eligible
  373. quote {
  374. price
  375. priceWithTax
  376. metadata
  377. }
  378. }
  379. }
  380. `;
  381. export const TEST_ELIGIBLE_SHIPPING_METHODS = gql`
  382. query TestEligibleMethods($input: TestEligibleShippingMethodsInput!) {
  383. testEligibleShippingMethods(input: $input) {
  384. id
  385. name
  386. description
  387. price
  388. priceWithTax
  389. metadata
  390. }
  391. }
  392. `;