shipping-method.e2e-spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 { TEST_SETUP_TIMEOUT_MS, testConfig } 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. type: 'int',
  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. type: 'int',
  185. value: '0',
  186. },
  187. ],
  188. },
  189. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  190. shippingAddress: {
  191. streetLine1: '',
  192. countryCode: 'GB',
  193. },
  194. },
  195. });
  196. expect(testShippingMethod).toEqual({
  197. eligible: true,
  198. quote: {
  199. price: 100,
  200. priceWithTax: 100,
  201. metadata: TEST_METADATA,
  202. },
  203. });
  204. });
  205. it('testEligibleShippingMethods', async () => {
  206. const { testEligibleShippingMethods } = await adminClient.query<
  207. TestEligibleMethods.Query,
  208. TestEligibleMethods.Variables
  209. >(TEST_ELIGIBLE_SHIPPING_METHODS, {
  210. input: {
  211. lines: [{ productVariantId: 'T_1', quantity: 1 }],
  212. shippingAddress: {
  213. streetLine1: '',
  214. countryCode: 'GB',
  215. },
  216. },
  217. });
  218. expect(testEligibleShippingMethods).toEqual([
  219. {
  220. id: 'T_3',
  221. description: 'new method',
  222. price: 100,
  223. priceWithTax: 100,
  224. metadata: TEST_METADATA,
  225. },
  226. {
  227. id: 'T_1',
  228. description: 'Standard Shipping',
  229. price: 500,
  230. priceWithTax: 500,
  231. metadata: null,
  232. },
  233. {
  234. id: 'T_2',
  235. description: 'Express Shipping',
  236. price: 1000,
  237. priceWithTax: 1000,
  238. metadata: null,
  239. },
  240. ]);
  241. });
  242. it('updateShippingMethod', async () => {
  243. const { updateShippingMethod } = await adminClient.query<
  244. UpdateShippingMethod.Mutation,
  245. UpdateShippingMethod.Variables
  246. >(UPDATE_SHIPPING_METHOD, {
  247. input: {
  248. id: 'T_3',
  249. description: 'changed method',
  250. },
  251. });
  252. expect(updateShippingMethod.description).toBe('changed method');
  253. });
  254. it('deleteShippingMethod', async () => {
  255. const listResult1 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  256. expect(listResult1.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2', 'T_3']);
  257. const { deleteShippingMethod } = await adminClient.query<
  258. DeleteShippingMethod.Mutation,
  259. DeleteShippingMethod.Variables
  260. >(DELETE_SHIPPING_METHOD, {
  261. id: 'T_3',
  262. });
  263. expect(deleteShippingMethod).toEqual({
  264. result: DeletionResult.DELETED,
  265. message: null,
  266. });
  267. const listResult2 = await adminClient.query<GetShippingMethodList.Query>(GET_SHIPPING_METHOD_LIST);
  268. expect(listResult2.shippingMethods.items.map(i => i.id)).toEqual(['T_1', 'T_2']);
  269. });
  270. });
  271. const SHIPPING_METHOD_FRAGMENT = gql`
  272. fragment ShippingMethod on ShippingMethod {
  273. id
  274. code
  275. description
  276. calculator {
  277. code
  278. }
  279. checker {
  280. code
  281. }
  282. }
  283. `;
  284. const GET_SHIPPING_METHOD_LIST = gql`
  285. query GetShippingMethodList {
  286. shippingMethods {
  287. items {
  288. ...ShippingMethod
  289. }
  290. totalItems
  291. }
  292. }
  293. ${SHIPPING_METHOD_FRAGMENT}
  294. `;
  295. const GET_SHIPPING_METHOD = gql`
  296. query GetShippingMethod($id: ID!) {
  297. shippingMethod(id: $id) {
  298. ...ShippingMethod
  299. }
  300. }
  301. ${SHIPPING_METHOD_FRAGMENT}
  302. `;
  303. const CREATE_SHIPPING_METHOD = gql`
  304. mutation CreateShippingMethod($input: CreateShippingMethodInput!) {
  305. createShippingMethod(input: $input) {
  306. ...ShippingMethod
  307. }
  308. }
  309. ${SHIPPING_METHOD_FRAGMENT}
  310. `;
  311. const UPDATE_SHIPPING_METHOD = gql`
  312. mutation UpdateShippingMethod($input: UpdateShippingMethodInput!) {
  313. updateShippingMethod(input: $input) {
  314. ...ShippingMethod
  315. }
  316. }
  317. ${SHIPPING_METHOD_FRAGMENT}
  318. `;
  319. const DELETE_SHIPPING_METHOD = gql`
  320. mutation DeleteShippingMethod($id: ID!) {
  321. deleteShippingMethod(id: $id) {
  322. result
  323. message
  324. }
  325. }
  326. `;
  327. const GET_ELIGIBILITY_CHECKERS = gql`
  328. query GetEligibilityCheckers {
  329. shippingEligibilityCheckers {
  330. code
  331. description
  332. args {
  333. name
  334. type
  335. description
  336. label
  337. config
  338. }
  339. }
  340. }
  341. `;
  342. const GET_CALCULATORS = gql`
  343. query GetCalculators {
  344. shippingCalculators {
  345. code
  346. description
  347. args {
  348. name
  349. type
  350. description
  351. label
  352. config
  353. }
  354. }
  355. }
  356. `;
  357. const TEST_SHIPPING_METHOD = gql`
  358. query TestShippingMethod($input: TestShippingMethodInput!) {
  359. testShippingMethod(input: $input) {
  360. eligible
  361. quote {
  362. price
  363. priceWithTax
  364. metadata
  365. }
  366. }
  367. }
  368. `;
  369. export const TEST_ELIGIBLE_SHIPPING_METHODS = gql`
  370. query TestEligibleMethods($input: TestEligibleShippingMethodsInput!) {
  371. testEligibleShippingMethods(input: $input) {
  372. id
  373. description
  374. price
  375. priceWithTax
  376. metadata
  377. }
  378. }
  379. `;