shipping-method.e2e-spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. description: 'new method',
  144. checker: {
  145. code: defaultShippingEligibilityChecker.code,
  146. arguments: [
  147. {
  148. name: 'orderMinimum',
  149. value: '0',
  150. },
  151. ],
  152. },
  153. calculator: {
  154. code: calculatorWithMetadata.code,
  155. arguments: [],
  156. },
  157. },
  158. });
  159. expect(createShippingMethod).toEqual({
  160. id: 'T_3',
  161. code: 'new-method',
  162. description: 'new method',
  163. calculator: {
  164. code: 'calculator-with-metadata',
  165. },
  166. checker: {
  167. code: 'default-shipping-eligibility-checker',
  168. },
  169. });
  170. });
  171. it('testShippingMethod', async () => {
  172. const { testShippingMethod } = await adminClient.query<
  173. TestShippingMethod.Query,
  174. TestShippingMethod.Variables
  175. >(TEST_SHIPPING_METHOD, {
  176. input: {
  177. calculator: {
  178. code: calculatorWithMetadata.code,
  179. arguments: [],
  180. },
  181. checker: {
  182. code: defaultShippingEligibilityChecker.code,
  183. arguments: [
  184. {
  185. name: 'orderMinimum',
  186. value: '0',
  187. },
  188. ],
  189. },
  190. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  191. shippingAddress: {
  192. streetLine1: '',
  193. countryCode: 'GB',
  194. },
  195. },
  196. });
  197. expect(testShippingMethod).toEqual({
  198. eligible: true,
  199. quote: {
  200. price: 100,
  201. priceWithTax: 100,
  202. metadata: TEST_METADATA,
  203. },
  204. });
  205. });
  206. it('testEligibleShippingMethods', async () => {
  207. const { testEligibleShippingMethods } = await adminClient.query<
  208. TestEligibleMethods.Query,
  209. TestEligibleMethods.Variables
  210. >(TEST_ELIGIBLE_SHIPPING_METHODS, {
  211. input: {
  212. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  213. shippingAddress: {
  214. streetLine1: '',
  215. countryCode: 'GB',
  216. },
  217. },
  218. });
  219. expect(testEligibleShippingMethods).toEqual([
  220. {
  221. id: 'T_3',
  222. description: 'new method',
  223. price: 100,
  224. priceWithTax: 100,
  225. metadata: TEST_METADATA,
  226. },
  227. {
  228. id: 'T_1',
  229. description: 'Standard Shipping',
  230. price: 500,
  231. priceWithTax: 500,
  232. metadata: null,
  233. },
  234. {
  235. id: 'T_2',
  236. description: 'Express Shipping',
  237. price: 1000,
  238. priceWithTax: 1000,
  239. metadata: null,
  240. },
  241. ]);
  242. });
  243. it('updateShippingMethod', async () => {
  244. const { updateShippingMethod } = await adminClient.query<
  245. UpdateShippingMethod.Mutation,
  246. UpdateShippingMethod.Variables
  247. >(UPDATE_SHIPPING_METHOD, {
  248. input: {
  249. id: 'T_3',
  250. description: 'changed method',
  251. },
  252. });
  253. expect(updateShippingMethod.description).toBe('changed method');
  254. });
  255. it('deleteShippingMethod', async () => {
  256. const listResult1 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  257. expect(listResult1.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3']);
  258. const { deleteShippingMethod } = await adminClient.query<
  259. DeleteShippingMethod.Mutation,
  260. DeleteShippingMethod.Variables
  261. >(DELETE_SHIPPING_METHOD, {
  262. id: 'T_3',
  263. });
  264. expect(deleteShippingMethod).toEqual({
  265. result: DeletionResult.DELETED,
  266. message: null,
  267. });
  268. const listResult2 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  269. expect(listResult2.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2']);
  270. });
  271. });
  272. const GET_SHIPPING_METHOD_LIST = gql`
  273. query GetShippingMethodList {
  274. shippingMethods {
  275. items {
  276. ...ShippingMethod
  277. }
  278. totalItems
  279. }
  280. }
  281. ${SHIPPING_METHOD_FRAGMENT}
  282. `;
  283. const GET_SHIPPING_METHOD = gql`
  284. query GetShippingMethod($id: ID!) {
  285. shippingMethod(id: $id) {
  286. ...ShippingMethod
  287. }
  288. }
  289. ${SHIPPING_METHOD_FRAGMENT}
  290. `;
  291. const UPDATE_SHIPPING_METHOD = gql`
  292. mutation UpdateShippingMethod($input: UpdateShippingMethodInput!) {
  293. updateShippingMethod(input: $input) {
  294. ...ShippingMethod
  295. }
  296. }
  297. ${SHIPPING_METHOD_FRAGMENT}
  298. `;
  299. const DELETE_SHIPPING_METHOD = gql`
  300. mutation DeleteShippingMethod($id: ID!) {
  301. deleteShippingMethod(id: $id) {
  302. result
  303. message
  304. }
  305. }
  306. `;
  307. const GET_ELIGIBILITY_CHECKERS = gql`
  308. query GetEligibilityCheckers {
  309. shippingEligibilityCheckers {
  310. code
  311. description
  312. args {
  313. name
  314. type
  315. description
  316. label
  317. ui
  318. }
  319. }
  320. }
  321. `;
  322. const GET_CALCULATORS = gql`
  323. query GetCalculators {
  324. shippingCalculators {
  325. code
  326. description
  327. args {
  328. name
  329. type
  330. description
  331. label
  332. ui
  333. }
  334. }
  335. }
  336. `;
  337. const TEST_SHIPPING_METHOD = gql`
  338. query TestShippingMethod($input: TestShippingMethodInput!) {
  339. testShippingMethod(input: $input) {
  340. eligible
  341. quote {
  342. price
  343. priceWithTax
  344. metadata
  345. }
  346. }
  347. }
  348. `;
  349. export const TEST_ELIGIBLE_SHIPPING_METHODS = gql`
  350. query TestEligibleMethods($input: TestEligibleShippingMethodsInput!) {
  351. testEligibleShippingMethods(input: $input) {
  352. id
  353. description
  354. price
  355. priceWithTax
  356. metadata
  357. }
  358. }
  359. `;