shipping-method.e2e-spec.ts 12 KB

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