shipping-method.e2e-spec.ts 12 KB

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