shipping-method.e2e-spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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. dataDir: path.join(__dirname, '__data__'),
  52. initialData,
  53. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  54. customerCount: 1,
  55. });
  56. await adminClient.asSuperAdmin();
  57. }, TEST_SETUP_TIMEOUT_MS);
  58. afterAll(async () => {
  59. await server.destroy();
  60. });
  61. it('shippingEligibilityCheckers', async () => {
  62. const { shippingEligibilityCheckers } = await adminClient.query<GetEligibilityCheckers.Query>(
  63. GET_ELIGIBILITY_CHECKERS,
  64. );
  65. expect(shippingEligibilityCheckers).toEqual([
  66. {
  67. args: [
  68. {
  69. config: {
  70. inputType: 'money',
  71. },
  72. description: 'Order is eligible only if its total is greater or equal to this value',
  73. label: 'Minimum order value',
  74. name: 'orderMinimum',
  75. type: 'int',
  76. },
  77. ],
  78. code: 'default-shipping-eligibility-checker',
  79. description: 'Default Shipping Eligibility Checker',
  80. },
  81. ]);
  82. });
  83. it('shippingCalculators', async () => {
  84. const { shippingCalculators } = await adminClient.query<GetCalculators.Query>(GET_CALCULATORS);
  85. expect(shippingCalculators).toEqual([
  86. {
  87. args: [
  88. {
  89. config: {
  90. inputType: 'money',
  91. },
  92. description: null,
  93. label: 'Shipping price',
  94. name: 'rate',
  95. type: 'int',
  96. },
  97. {
  98. config: {
  99. inputType: 'percentage',
  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. type: 'int',
  148. value: '0',
  149. },
  150. ],
  151. },
  152. calculator: {
  153. code: calculatorWithMetadata.code,
  154. arguments: [],
  155. },
  156. },
  157. });
  158. expect(createShippingMethod).toEqual({
  159. id: 'T_3',
  160. code: 'new-method',
  161. description: 'new method',
  162. calculator: {
  163. code: 'calculator-with-metadata',
  164. },
  165. checker: {
  166. code: 'default-shipping-eligibility-checker',
  167. },
  168. });
  169. });
  170. it('testShippingMethod', async () => {
  171. const { testShippingMethod } = await adminClient.query<
  172. TestShippingMethod.Query,
  173. TestShippingMethod.Variables
  174. >(TEST_SHIPPING_METHOD, {
  175. input: {
  176. calculator: {
  177. code: calculatorWithMetadata.code,
  178. arguments: [],
  179. },
  180. checker: {
  181. code: defaultShippingEligibilityChecker.code,
  182. arguments: [
  183. {
  184. name: 'orderMinimum',
  185. type: 'int',
  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 SHIPPING_METHOD_FRAGMENT = gql`
  273. fragment ShippingMethod on ShippingMethod {
  274. id
  275. code
  276. description
  277. calculator {
  278. code
  279. }
  280. checker {
  281. code
  282. }
  283. }
  284. `;
  285. const GET_SHIPPING_METHOD_LIST = gql`
  286. query GetShippingMethodList {
  287. shippingMethods {
  288. items {
  289. ...ShippingMethod
  290. }
  291. totalItems
  292. }
  293. }
  294. ${SHIPPING_METHOD_FRAGMENT}
  295. `;
  296. const GET_SHIPPING_METHOD = gql`
  297. query GetShippingMethod($id: ID!) {
  298. shippingMethod(id: $id) {
  299. ...ShippingMethod
  300. }
  301. }
  302. ${SHIPPING_METHOD_FRAGMENT}
  303. `;
  304. const CREATE_SHIPPING_METHOD = gql`
  305. mutation CreateShippingMethod($input: CreateShippingMethodInput!) {
  306. createShippingMethod(input: $input) {
  307. ...ShippingMethod
  308. }
  309. }
  310. ${SHIPPING_METHOD_FRAGMENT}
  311. `;
  312. const UPDATE_SHIPPING_METHOD = gql`
  313. mutation UpdateShippingMethod($input: UpdateShippingMethodInput!) {
  314. updateShippingMethod(input: $input) {
  315. ...ShippingMethod
  316. }
  317. }
  318. ${SHIPPING_METHOD_FRAGMENT}
  319. `;
  320. const DELETE_SHIPPING_METHOD = gql`
  321. mutation DeleteShippingMethod($id: ID!) {
  322. deleteShippingMethod(id: $id) {
  323. result
  324. message
  325. }
  326. }
  327. `;
  328. const GET_ELIGIBILITY_CHECKERS = gql`
  329. query GetEligibilityCheckers {
  330. shippingEligibilityCheckers {
  331. code
  332. description
  333. args {
  334. name
  335. type
  336. description
  337. label
  338. config
  339. }
  340. }
  341. }
  342. `;
  343. const GET_CALCULATORS = gql`
  344. query GetCalculators {
  345. shippingCalculators {
  346. code
  347. description
  348. args {
  349. name
  350. type
  351. description
  352. label
  353. config
  354. }
  355. }
  356. }
  357. `;
  358. const TEST_SHIPPING_METHOD = gql`
  359. query TestShippingMethod($input: TestShippingMethodInput!) {
  360. testShippingMethod(input: $input) {
  361. eligible
  362. quote {
  363. price
  364. priceWithTax
  365. metadata
  366. }
  367. }
  368. }
  369. `;
  370. export const TEST_ELIGIBLE_SHIPPING_METHODS = gql`
  371. query TestEligibleMethods($input: TestEligibleShippingMethodsInput!) {
  372. testEligibleShippingMethods(input: $input) {
  373. id
  374. description
  375. price
  376. priceWithTax
  377. metadata
  378. }
  379. }
  380. `;