shipping-method.e2e-spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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 { dataDir, TEST_SETUP_TIMEOUT_MS, testConfig } from './config/test-config';
  11. import { initialData } from './fixtures/e2e-initial-data';
  12. import {
  13. CreateShippingMethod,
  14. DeleteShippingMethod,
  15. DeletionResult,
  16. GetCalculators,
  17. GetEligibilityCheckers,
  18. GetShippingMethod,
  19. GetShippingMethodList,
  20. LanguageCode,
  21. TestEligibleMethods,
  22. TestShippingMethod,
  23. UpdateShippingMethod,
  24. } from './graphql/generated-e2e-admin-types';
  25. const TEST_METADATA = {
  26. foo: 'bar',
  27. baz: [1, 2, 3],
  28. };
  29. const calculatorWithMetadata = new ShippingCalculator({
  30. code: 'calculator-with-metadata',
  31. description: [{ languageCode: LanguageCode.en, value: 'Has metadata' }],
  32. args: {},
  33. calculate: order => {
  34. return {
  35. price: 100,
  36. priceWithTax: 100,
  37. metadata: TEST_METADATA,
  38. };
  39. },
  40. });
  41. describe('ShippingMethod resolver', () => {
  42. const { server, adminClient, shopClient } = createTestEnvironment({
  43. ...testConfig,
  44. shippingOptions: {
  45. shippingEligibilityCheckers: [defaultShippingEligibilityChecker],
  46. shippingCalculators: [defaultShippingCalculator, calculatorWithMetadata],
  47. },
  48. });
  49. beforeAll(async () => {
  50. const token = await server.init({
  51. dataDir,
  52. initialData,
  53. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  54. customerCount: 1,
  55. });
  56. await adminClient.init();
  57. await adminClient.asSuperAdmin();
  58. await shopClient.init();
  59. }, TEST_SETUP_TIMEOUT_MS);
  60. afterAll(async () => {
  61. await server.destroy();
  62. });
  63. it('shippingEligibilityCheckers', async () => {
  64. const { shippingEligibilityCheckers } = await adminClient.query<GetEligibilityCheckers.Query>(
  65. GET_ELIGIBILITY_CHECKERS,
  66. );
  67. expect(shippingEligibilityCheckers).toEqual([
  68. {
  69. args: [
  70. {
  71. config: {
  72. inputType: 'money',
  73. },
  74. description: 'Order is eligible only if its total is greater or equal to this value',
  75. label: 'Minimum order value',
  76. name: 'orderMinimum',
  77. type: 'int',
  78. },
  79. ],
  80. code: 'default-shipping-eligibility-checker',
  81. description: 'Default Shipping Eligibility Checker',
  82. },
  83. ]);
  84. });
  85. it('shippingCalculators', async () => {
  86. const { shippingCalculators } = await adminClient.query<GetCalculators.Query>(GET_CALCULATORS);
  87. expect(shippingCalculators).toEqual([
  88. {
  89. args: [
  90. {
  91. config: {
  92. inputType: 'money',
  93. },
  94. description: null,
  95. label: 'Shipping price',
  96. name: 'rate',
  97. type: 'int',
  98. },
  99. {
  100. config: {
  101. inputType: 'percentage',
  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. type: 'int',
  150. value: '0',
  151. },
  152. ],
  153. },
  154. calculator: {
  155. code: calculatorWithMetadata.code,
  156. arguments: [],
  157. },
  158. },
  159. });
  160. expect(createShippingMethod).toEqual({
  161. id: 'T_3',
  162. code: 'new-method',
  163. description: 'new method',
  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. type: 'int',
  188. value: '0',
  189. },
  190. ],
  191. },
  192. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  193. shippingAddress: {
  194. streetLine1: '',
  195. countryCode: 'GB',
  196. },
  197. },
  198. });
  199. expect(testShippingMethod).toEqual({
  200. eligible: true,
  201. quote: {
  202. price: 100,
  203. priceWithTax: 100,
  204. metadata: TEST_METADATA,
  205. },
  206. });
  207. });
  208. it('testEligibleShippingMethods', async () => {
  209. const { testEligibleShippingMethods } = await adminClient.query<
  210. TestEligibleMethods.Query,
  211. TestEligibleMethods.Variables
  212. >(TEST_ELIGIBLE_SHIPPING_METHODS, {
  213. input: {
  214. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  215. shippingAddress: {
  216. streetLine1: '',
  217. countryCode: 'GB',
  218. },
  219. },
  220. });
  221. expect(testEligibleShippingMethods).toEqual([
  222. {
  223. id: 'T_3',
  224. description: 'new method',
  225. price: 100,
  226. priceWithTax: 100,
  227. metadata: TEST_METADATA,
  228. },
  229. {
  230. id: 'T_1',
  231. description: 'Standard Shipping',
  232. price: 500,
  233. priceWithTax: 500,
  234. metadata: null,
  235. },
  236. {
  237. id: 'T_2',
  238. description: 'Express Shipping',
  239. price: 1000,
  240. priceWithTax: 1000,
  241. metadata: null,
  242. },
  243. ]);
  244. });
  245. it('updateShippingMethod', async () => {
  246. const { updateShippingMethod } = await adminClient.query<
  247. UpdateShippingMethod.Mutation,
  248. UpdateShippingMethod.Variables
  249. >(UPDATE_SHIPPING_METHOD, {
  250. input: {
  251. id: 'T_3',
  252. description: 'changed method',
  253. },
  254. });
  255. expect(updateShippingMethod.description).toBe('changed method');
  256. });
  257. it('deleteShippingMethod', async () => {
  258. const listResult1 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  259. expect(listResult1.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3']);
  260. const { deleteShippingMethod } = await adminClient.query<
  261. DeleteShippingMethod.Mutation,
  262. DeleteShippingMethod.Variables
  263. >(DELETE_SHIPPING_METHOD, {
  264. id: 'T_3',
  265. });
  266. expect(deleteShippingMethod).toEqual({
  267. result: DeletionResult.DELETED,
  268. message: null,
  269. });
  270. const listResult2 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  271. expect(listResult2.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2']);
  272. });
  273. });
  274. const SHIPPING_METHOD_FRAGMENT = gql`
  275. fragment ShippingMethod on ShippingMethod {
  276. id
  277. code
  278. description
  279. calculator {
  280. code
  281. }
  282. checker {
  283. code
  284. }
  285. }
  286. `;
  287. const GET_SHIPPING_METHOD_LIST = gql`
  288. query GetShippingMethodList {
  289. shippingMethods {
  290. items {
  291. ...ShippingMethod
  292. }
  293. totalItems
  294. }
  295. }
  296. ${SHIPPING_METHOD_FRAGMENT}
  297. `;
  298. const GET_SHIPPING_METHOD = gql`
  299. query GetShippingMethod($id: ID!) {
  300. shippingMethod(id: $id) {
  301. ...ShippingMethod
  302. }
  303. }
  304. ${SHIPPING_METHOD_FRAGMENT}
  305. `;
  306. const CREATE_SHIPPING_METHOD = gql`
  307. mutation CreateShippingMethod($input: CreateShippingMethodInput!) {
  308. createShippingMethod(input: $input) {
  309. ...ShippingMethod
  310. }
  311. }
  312. ${SHIPPING_METHOD_FRAGMENT}
  313. `;
  314. const UPDATE_SHIPPING_METHOD = gql`
  315. mutation UpdateShippingMethod($input: UpdateShippingMethodInput!) {
  316. updateShippingMethod(input: $input) {
  317. ...ShippingMethod
  318. }
  319. }
  320. ${SHIPPING_METHOD_FRAGMENT}
  321. `;
  322. const DELETE_SHIPPING_METHOD = gql`
  323. mutation DeleteShippingMethod($id: ID!) {
  324. deleteShippingMethod(id: $id) {
  325. result
  326. message
  327. }
  328. }
  329. `;
  330. const GET_ELIGIBILITY_CHECKERS = gql`
  331. query GetEligibilityCheckers {
  332. shippingEligibilityCheckers {
  333. code
  334. description
  335. args {
  336. name
  337. type
  338. description
  339. label
  340. config
  341. }
  342. }
  343. }
  344. `;
  345. const GET_CALCULATORS = gql`
  346. query GetCalculators {
  347. shippingCalculators {
  348. code
  349. description
  350. args {
  351. name
  352. type
  353. description
  354. label
  355. config
  356. }
  357. }
  358. }
  359. `;
  360. const TEST_SHIPPING_METHOD = gql`
  361. query TestShippingMethod($input: TestShippingMethodInput!) {
  362. testShippingMethod(input: $input) {
  363. eligible
  364. quote {
  365. price
  366. priceWithTax
  367. metadata
  368. }
  369. }
  370. }
  371. `;
  372. export const TEST_ELIGIBLE_SHIPPING_METHODS = gql`
  373. query TestEligibleMethods($input: TestEligibleShippingMethodsInput!) {
  374. testEligibleShippingMethods(input: $input) {
  375. id
  376. description
  377. price
  378. priceWithTax
  379. metadata
  380. }
  381. }
  382. `;