shipping-method.e2e-spec.ts 12 KB

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