shipping-method.e2e-spec.ts 12 KB

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