order-line-custom-fields.e2e-spec.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. import { mergeConfig, Product } from '@vendure/core';
  2. import { createTestEnvironment } from '@vendure/testing';
  3. import gql from 'graphql-tag';
  4. import path from 'path';
  5. import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
  6. import { initialData } from '../../../e2e-common/e2e-initial-data';
  7. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  8. import { fixPostgresTimezone } from './utils/fix-pg-timezone';
  9. // Since the predefined mutations don't support custom fields, we'll create our own
  10. // but still follow the typing pattern from the existing tests
  11. const ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS = gql`
  12. mutation AddItemToOrderWithCustomFields(
  13. $productVariantId: ID!
  14. $quantity: Int!
  15. $customFields: OrderLineCustomFieldsInput
  16. ) {
  17. addItemToOrder(
  18. productVariantId: $productVariantId
  19. quantity: $quantity
  20. customFields: $customFields
  21. ) {
  22. ... on Order {
  23. id
  24. lines {
  25. id
  26. quantity
  27. customFields {
  28. stringField
  29. intField
  30. booleanField
  31. nullableField
  32. relationField {
  33. id
  34. name
  35. }
  36. }
  37. }
  38. }
  39. ... on ErrorResult {
  40. errorCode
  41. message
  42. }
  43. }
  44. }
  45. `;
  46. const ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS = gql`
  47. mutation AdjustOrderLineWithCustomFields(
  48. $orderLineId: ID!
  49. $quantity: Int!
  50. $customFields: OrderLineCustomFieldsInput
  51. ) {
  52. adjustOrderLine(orderLineId: $orderLineId, quantity: $quantity, customFields: $customFields) {
  53. ... on Order {
  54. id
  55. lines {
  56. id
  57. quantity
  58. customFields {
  59. stringField
  60. intField
  61. booleanField
  62. nullableField
  63. relationField {
  64. id
  65. name
  66. }
  67. }
  68. }
  69. }
  70. ... on ErrorResult {
  71. errorCode
  72. message
  73. }
  74. }
  75. }
  76. `;
  77. const REMOVE_ALL_ORDER_LINES = gql`
  78. mutation RemoveAllOrderLines {
  79. removeAllOrderLines {
  80. ... on Order {
  81. id
  82. lines {
  83. id
  84. quantity
  85. }
  86. }
  87. ... on ErrorResult {
  88. errorCode
  89. message
  90. }
  91. }
  92. }
  93. `;
  94. fixPostgresTimezone();
  95. const customConfig = mergeConfig(testConfig(), {
  96. customFields: {
  97. OrderLine: [
  98. { name: 'stringField', type: 'string' },
  99. { name: 'intField', type: 'int' },
  100. { name: 'booleanField', type: 'boolean' },
  101. { name: 'nullableField', type: 'string', nullable: true },
  102. { name: 'relationField', type: 'relation', entity: Product },
  103. ],
  104. },
  105. });
  106. describe('OrderLine Custom Fields', () => {
  107. const { server, adminClient, shopClient } = createTestEnvironment(customConfig);
  108. beforeAll(async () => {
  109. await server.init({
  110. initialData,
  111. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
  112. customerCount: 1,
  113. });
  114. await adminClient.asSuperAdmin();
  115. }, TEST_SETUP_TIMEOUT_MS);
  116. afterAll(async () => {
  117. await server.destroy();
  118. });
  119. beforeEach(async () => {
  120. // Clear the shopping cart before each test to ensure test isolation
  121. await shopClient.query(REMOVE_ALL_ORDER_LINES);
  122. });
  123. describe('addItemToOrder', () => {
  124. it('can add order line with custom fields', async () => {
  125. const { addItemToOrder } = await shopClient.query(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
  126. productVariantId: 'T_1',
  127. quantity: 1,
  128. customFields: { stringField: 'test value', intField: 42, booleanField: true },
  129. });
  130. expect(addItemToOrder.lines[0].customFields).toEqual({
  131. stringField: 'test value',
  132. intField: 42,
  133. booleanField: true,
  134. nullableField: null,
  135. relationField: null,
  136. });
  137. });
  138. it('can add order line with relation custom field', async () => {
  139. const { addItemToOrder } = await shopClient.query(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
  140. productVariantId: 'T_2',
  141. quantity: 1,
  142. customFields: { relationFieldId: 'T_1' },
  143. });
  144. expect(addItemToOrder.lines[0].customFields.relationField.id).toBe('T_1');
  145. });
  146. });
  147. describe('adjustOrderLine - merging behavior', () => {
  148. it('should merge custom fields when updating partial fields', async () => {
  149. // Create a fresh order line for this test
  150. const { addItemToOrder } = await shopClient.query(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
  151. productVariantId: 'T_3',
  152. quantity: 1,
  153. customFields: {
  154. stringField: 'initial value',
  155. intField: 100,
  156. booleanField: false,
  157. nullableField: 'not null',
  158. },
  159. });
  160. const orderLineId = addItemToOrder.lines[0].id;
  161. const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
  162. orderLineId,
  163. quantity: 2,
  164. customFields: {
  165. stringField: 'updated value',
  166. },
  167. });
  168. const updatedLine = adjustOrderLine.lines.find(line => line.id === orderLineId);
  169. expect(updatedLine.customFields).toEqual({
  170. stringField: 'updated value', // updated
  171. intField: 100, // preserved
  172. booleanField: false, // preserved
  173. nullableField: 'not null', // preserved
  174. relationField: null, // preserved
  175. });
  176. });
  177. it('should allow updating multiple fields while preserving others', async () => {
  178. // Create a fresh order line for this test
  179. const { addItemToOrder } = await shopClient.query(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
  180. productVariantId: 'T_4',
  181. quantity: 1,
  182. customFields: {
  183. stringField: 'initial value',
  184. intField: 100,
  185. booleanField: false,
  186. nullableField: 'not null',
  187. },
  188. });
  189. const orderLineId = addItemToOrder.lines[0].id;
  190. const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
  191. orderLineId,
  192. quantity: 2,
  193. customFields: {
  194. intField: 200,
  195. booleanField: true,
  196. },
  197. });
  198. const updatedLine = adjustOrderLine.lines.find(line => line.id === orderLineId);
  199. expect(updatedLine.customFields).toEqual({
  200. stringField: 'initial value', // preserved
  201. intField: 200, // updated
  202. booleanField: true, // updated
  203. nullableField: 'not null', // preserved
  204. relationField: null, // preserved
  205. });
  206. });
  207. it('should allow unsetting fields using null', async () => {
  208. // Create a fresh order line for this test
  209. const { addItemToOrder } = await shopClient.query(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
  210. productVariantId: 'T_1',
  211. quantity: 1,
  212. customFields: {
  213. stringField: 'initial value',
  214. intField: 100,
  215. booleanField: false,
  216. nullableField: 'not null',
  217. },
  218. });
  219. const orderLineId = addItemToOrder.lines[0].id;
  220. const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
  221. orderLineId,
  222. quantity: 2,
  223. customFields: {
  224. nullableField: null,
  225. },
  226. });
  227. const updatedLine = adjustOrderLine.lines.find(line => line.id === orderLineId);
  228. expect(updatedLine.customFields).toEqual({
  229. stringField: 'initial value', // preserved
  230. intField: 100, // preserved
  231. booleanField: false, // preserved
  232. nullableField: null, // unset using null
  233. relationField: null, // preserved
  234. });
  235. });
  236. it('should handle relation field updates with merging', async () => {
  237. // Create a fresh order line for this test
  238. const { addItemToOrder } = await shopClient.query(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
  239. productVariantId: 'T_2',
  240. quantity: 1,
  241. customFields: {
  242. stringField: 'initial value',
  243. intField: 100,
  244. booleanField: false,
  245. nullableField: 'not null',
  246. },
  247. });
  248. const orderLineId = addItemToOrder.lines[0].id;
  249. const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
  250. orderLineId,
  251. quantity: 2,
  252. customFields: {
  253. relationFieldId: 'T_1',
  254. },
  255. });
  256. const updatedLine = adjustOrderLine.lines.find(line => line.id === orderLineId);
  257. expect(updatedLine.customFields).toEqual({
  258. stringField: 'initial value', // preserved
  259. intField: 100, // preserved
  260. booleanField: false, // preserved
  261. nullableField: 'not null', // preserved
  262. relationField: {
  263. id: 'T_1',
  264. name: 'Laptop',
  265. },
  266. });
  267. });
  268. it('should allow unsetting relation field using null', async () => {
  269. // Create a fresh order line for this test
  270. const { addItemToOrder } = await shopClient.query(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
  271. productVariantId: 'T_3',
  272. quantity: 1,
  273. customFields: {
  274. stringField: 'initial value',
  275. intField: 100,
  276. booleanField: false,
  277. nullableField: 'not null',
  278. relationFieldId: 'T_1',
  279. },
  280. });
  281. const orderLineId = addItemToOrder.lines[0].id;
  282. const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
  283. orderLineId,
  284. quantity: 2,
  285. customFields: {
  286. relationFieldId: null,
  287. },
  288. });
  289. const updatedLine = adjustOrderLine.lines.find(line => line.id === orderLineId);
  290. expect(updatedLine.customFields).toEqual({
  291. stringField: 'initial value', // preserved
  292. intField: 100, // preserved
  293. booleanField: false, // preserved
  294. nullableField: 'not null', // preserved
  295. relationField: null, // unset using null
  296. });
  297. });
  298. });
  299. describe('edge cases', () => {
  300. it('should handle empty custom fields object', async () => {
  301. const { addItemToOrder } = await shopClient.query(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
  302. productVariantId: 'T_4',
  303. quantity: 1,
  304. customFields: {},
  305. });
  306. const newLine = addItemToOrder.lines[0];
  307. expect(newLine.customFields).toEqual({
  308. stringField: null,
  309. intField: null,
  310. booleanField: null,
  311. nullableField: null,
  312. relationField: null,
  313. });
  314. });
  315. it('should handle adjustOrderLine with empty custom fields', async () => {
  316. const { addItemToOrder } = await shopClient.query(ADD_ITEM_TO_ORDER_WITH_CUSTOM_FIELDS, {
  317. productVariantId: 'T_1',
  318. quantity: 1,
  319. customFields: { stringField: 'will be preserved', intField: 999 },
  320. });
  321. const lineId = addItemToOrder.lines[0].id;
  322. const { adjustOrderLine } = await shopClient.query(ADJUST_ORDER_LINE_WITH_CUSTOM_FIELDS, {
  323. orderLineId: lineId,
  324. quantity: 2,
  325. customFields: {},
  326. });
  327. const updatedLine = adjustOrderLine.lines.find(line => line.id === lineId);
  328. expect(updatedLine.customFields).toEqual({
  329. stringField: 'will be preserved', // preserved when empty object passed
  330. intField: 999, // preserved when empty object passed
  331. booleanField: null, // default value for unset fields
  332. nullableField: null, // default value for unset fields
  333. relationField: null, // default value for unset fields
  334. });
  335. });
  336. });
  337. });