custom-field-relations.e2e-spec.ts 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463
  1. // this file relies on defintions that are only available at runtime, therefore we still use gql here.
  2. import {
  3. assertFound,
  4. Asset,
  5. Collection,
  6. Country,
  7. CustomFields,
  8. defaultShippingCalculator,
  9. defaultShippingEligibilityChecker,
  10. Facet,
  11. FacetValue,
  12. manualFulfillmentHandler,
  13. mergeConfig,
  14. Product,
  15. ProductOption,
  16. ProductOptionGroup,
  17. ProductVariant,
  18. RequestContext,
  19. ShippingMethod,
  20. TransactionalConnection,
  21. } from '@vendure/core';
  22. import { createTestEnvironment } from '@vendure/testing';
  23. import gql from 'graphql-tag';
  24. import path from 'path';
  25. import { Repository } from 'typeorm';
  26. import { afterAll, beforeAll, describe, expect, it } from 'vitest';
  27. import { initialData } from '../../../e2e-common/e2e-initial-data';
  28. import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
  29. import { testSuccessfulPaymentMethod } from './fixtures/test-payment-methods';
  30. import { TestPlugin1636_1664 } from './fixtures/test-plugins/issue-1636-1664/issue-1636-1664-plugin';
  31. import { PluginIssue2453 } from './fixtures/test-plugins/issue-2453/plugin-issue2453';
  32. import { TestCustomEntity, WithCustomEntity } from './fixtures/test-plugins/with-custom-entity';
  33. import { addItemToOrderDocument } from './graphql/shop-definitions';
  34. import { sortById } from './utils/test-order-utils';
  35. const entitiesWithCustomFields: Array<keyof CustomFields> = [
  36. 'Address',
  37. 'Administrator',
  38. 'Asset',
  39. 'Channel',
  40. 'Collection',
  41. 'Customer',
  42. 'CustomerGroup',
  43. 'Facet',
  44. 'FacetValue',
  45. 'Fulfillment',
  46. 'GlobalSettings',
  47. 'Order',
  48. 'OrderLine',
  49. 'PaymentMethod',
  50. 'Product',
  51. 'ProductOption',
  52. 'ProductOptionGroup',
  53. 'ProductVariant',
  54. 'Promotion',
  55. 'Region',
  56. 'Seller',
  57. 'ShippingMethod',
  58. 'TaxCategory',
  59. 'TaxRate',
  60. 'User',
  61. 'Zone',
  62. ];
  63. const customFieldConfig: CustomFields = {};
  64. for (const entity of entitiesWithCustomFields) {
  65. customFieldConfig[entity] = [
  66. { name: 'primitive', type: 'string', list: false, defaultValue: 'test' },
  67. { name: 'single', type: 'relation', entity: Asset, graphQLType: 'Asset', list: false },
  68. { name: 'multi', type: 'relation', entity: Asset, graphQLType: 'Asset', list: true },
  69. ];
  70. }
  71. customFieldConfig.Product?.push(
  72. { name: 'cfCollection', type: 'relation', entity: Collection, list: false },
  73. { name: 'cfCountry', type: 'relation', entity: Country, list: false },
  74. { name: 'cfFacetValue', type: 'relation', entity: FacetValue, list: false },
  75. { name: 'cfFacet', type: 'relation', entity: Facet, list: false },
  76. { name: 'cfProductOptionGroup', type: 'relation', entity: ProductOptionGroup, list: false },
  77. { name: 'cfProductOption', type: 'relation', entity: ProductOption, list: false },
  78. { name: 'cfProductVariant', type: 'relation', entity: ProductVariant, list: false },
  79. { name: 'cfProduct', type: 'relation', entity: Product, list: false },
  80. { name: 'cfShippingMethod', type: 'relation', entity: ShippingMethod, list: false },
  81. { name: 'cfInternalAsset', type: 'relation', entity: Asset, list: false, internal: true },
  82. );
  83. customFieldConfig.ProductVariant?.push({
  84. name: 'cfRelatedProducts',
  85. type: 'relation',
  86. entity: Product,
  87. list: true,
  88. internal: false,
  89. public: true,
  90. });
  91. const customConfig = mergeConfig(testConfig(), {
  92. paymentOptions: {
  93. paymentMethodHandlers: [testSuccessfulPaymentMethod],
  94. },
  95. dbConnectionOptions: {
  96. timezone: 'Z',
  97. },
  98. customFields: customFieldConfig,
  99. plugins: [TestPlugin1636_1664, WithCustomEntity, PluginIssue2453],
  100. });
  101. describe('Custom field relations', () => {
  102. const { server, adminClient, shopClient } = createTestEnvironment(customConfig);
  103. beforeAll(async () => {
  104. await server.init({
  105. initialData,
  106. productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
  107. customerCount: 3,
  108. });
  109. await adminClient.asSuperAdmin();
  110. }, TEST_SETUP_TIMEOUT_MS);
  111. afterAll(async () => {
  112. await server.destroy();
  113. });
  114. it('customFieldConfig query returns entity and scalar fields', async () => {
  115. const { globalSettings } = await adminClient.query(gql`
  116. query {
  117. globalSettings {
  118. serverConfig {
  119. customFieldConfig {
  120. Customer {
  121. ... on RelationCustomFieldConfig {
  122. name
  123. entity
  124. scalarFields
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }
  131. `);
  132. const single = globalSettings.serverConfig.customFieldConfig.Customer[1];
  133. expect(single.entity).toBe('Asset');
  134. expect(single.scalarFields).toEqual([
  135. 'id',
  136. 'createdAt',
  137. 'updatedAt',
  138. 'name',
  139. 'type',
  140. 'fileSize',
  141. 'mimeType',
  142. 'width',
  143. 'height',
  144. 'source',
  145. 'preview',
  146. ]);
  147. });
  148. describe('special data resolution', () => {
  149. let productId: string;
  150. const productCustomFieldRelationsSelection = `
  151. id
  152. customFields {
  153. cfCollection {
  154. languageCode
  155. name
  156. }
  157. cfCountry {
  158. languageCode
  159. name
  160. }
  161. cfFacetValue {
  162. languageCode
  163. name
  164. }
  165. cfFacet {
  166. languageCode
  167. name
  168. }
  169. cfProductOptionGroup {
  170. languageCode
  171. name
  172. }
  173. cfProductOption {
  174. languageCode
  175. name
  176. }
  177. cfProductVariant {
  178. languageCode
  179. name
  180. }
  181. cfProduct {
  182. languageCode
  183. name
  184. }
  185. cfShippingMethod {
  186. languageCode
  187. name
  188. }
  189. }`;
  190. function assertTranslatableCustomFieldValues(product: { customFields: any }) {
  191. expect(product.customFields.cfCollection).toEqual({
  192. languageCode: 'en',
  193. name: '__root_collection__',
  194. });
  195. expect(product.customFields.cfCountry).toEqual({ languageCode: 'en', name: 'Australia' });
  196. expect(product.customFields.cfFacetValue).toEqual({
  197. languageCode: 'en',
  198. name: 'electronics',
  199. });
  200. expect(product.customFields.cfFacet).toEqual({ languageCode: 'en', name: 'category' });
  201. expect(product.customFields.cfProductOptionGroup).toEqual({
  202. languageCode: 'en',
  203. name: 'screen size',
  204. });
  205. expect(product.customFields.cfProductOption).toEqual({
  206. languageCode: 'en',
  207. name: '13 inch',
  208. });
  209. expect(product.customFields.cfProductVariant).toEqual({
  210. languageCode: 'en',
  211. name: 'Laptop 13 inch 8GB',
  212. });
  213. expect(product.customFields.cfProduct).toEqual({ languageCode: 'en', name: 'Laptop' });
  214. expect(product.customFields.cfShippingMethod).toEqual({
  215. languageCode: 'en',
  216. name: 'Standard Shipping',
  217. });
  218. }
  219. it('translatable entities get translated', async () => {
  220. const { createProduct } = await adminClient.query(gql`
  221. mutation {
  222. createProduct(
  223. input: {
  224. translations: [
  225. {
  226. languageCode: en
  227. name: "Test product"
  228. description: ""
  229. slug: "test-product"
  230. }
  231. ]
  232. customFields: {
  233. cfCollectionId: "T_1"
  234. cfCountryId: "T_1"
  235. cfFacetValueId: "T_1"
  236. cfFacetId: "T_1"
  237. cfProductOptionGroupId: "T_1"
  238. cfProductOptionId: "T_1"
  239. cfProductVariantId: "T_1"
  240. cfProductId: "T_1"
  241. cfShippingMethodId: "T_1"
  242. }
  243. }
  244. ) { ${productCustomFieldRelationsSelection} }
  245. }
  246. `);
  247. productId = createProduct.id;
  248. assertTranslatableCustomFieldValues(createProduct);
  249. });
  250. it('translatable entities get translated on findOneInChannel', async () => {
  251. const { product } = await adminClient.query(gql`
  252. query {
  253. product(id: "${productId}") { ${productCustomFieldRelationsSelection} }
  254. }
  255. `);
  256. assertTranslatableCustomFieldValues(product);
  257. });
  258. // https://github.com/vendure-ecommerce/vendure/issues/2453
  259. it('translatable eager-loaded relation works (issue 2453)', async () => {
  260. const { collections } = await adminClient.query(gql`
  261. query {
  262. collections(options: { sort: { name: DESC } }) {
  263. totalItems
  264. items {
  265. id
  266. name
  267. customFields {
  268. campaign {
  269. name
  270. languageCode
  271. }
  272. }
  273. }
  274. }
  275. }
  276. `);
  277. expect(collections.totalItems).toBe(3);
  278. expect(collections.items.find((c: any) => c.id === 'T_3')).toEqual({
  279. customFields: {
  280. campaign: {
  281. languageCode: 'en',
  282. name: 'Clearance Up to 70% Off frames',
  283. },
  284. },
  285. id: 'T_3',
  286. name: 'children collection',
  287. });
  288. });
  289. it('ProductVariant prices get resolved', async () => {
  290. const { product } = await adminClient.query(gql`
  291. query {
  292. product(id: "${productId}") {
  293. id
  294. customFields {
  295. cfProductVariant {
  296. price
  297. currencyCode
  298. priceWithTax
  299. }
  300. }
  301. }
  302. }`);
  303. expect(product.customFields.cfProductVariant).toEqual({
  304. price: 129900,
  305. currencyCode: 'USD',
  306. priceWithTax: 155880,
  307. });
  308. });
  309. });
  310. it('ProductVariant without a specified property value returns null', async () => {
  311. const { createProduct } = await adminClient.query(gql`
  312. mutation {
  313. createProduct(
  314. input: {
  315. translations: [
  316. {
  317. languageCode: en
  318. name: "Product with empty custom fields"
  319. description: ""
  320. slug: "product-with-empty-custom-fields"
  321. }
  322. ]
  323. }
  324. ) {
  325. id
  326. }
  327. }
  328. `);
  329. const { product } = await adminClient.query(gql`
  330. query {
  331. product(id: "${createProduct.id}") {
  332. id
  333. customFields {
  334. cfProductVariant{
  335. price
  336. currencyCode
  337. priceWithTax
  338. }
  339. }
  340. }
  341. }`);
  342. expect(product.customFields.cfProductVariant).toEqual(null);
  343. });
  344. describe('entity-specific implementation', () => {
  345. function assertCustomFieldIds(customFields: any, single: string, multi: string[]) {
  346. expect(customFields.single).toEqual({ id: single });
  347. expect(customFields.multi.sort(sortById)).toEqual(multi.map(id => ({ id })));
  348. }
  349. const customFieldsSelection = `
  350. customFields {
  351. primitive
  352. single {
  353. id
  354. }
  355. multi {
  356. id
  357. }
  358. }`;
  359. describe('Address entity', () => {
  360. it('admin createCustomerAddress', async () => {
  361. const { createCustomerAddress } = await adminClient.query(gql`
  362. mutation {
  363. createCustomerAddress(
  364. customerId: "T_1"
  365. input: {
  366. countryCode: "GB"
  367. streetLine1: "Test Street"
  368. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  369. }
  370. ) {
  371. id
  372. ${customFieldsSelection}
  373. }
  374. }
  375. `);
  376. assertCustomFieldIds(createCustomerAddress.customFields, 'T_1', ['T_1', 'T_2']);
  377. });
  378. it('shop createCustomerAddress', async () => {
  379. await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
  380. const { createCustomerAddress } = await shopClient.query(gql`
  381. mutation {
  382. createCustomerAddress(
  383. input: {
  384. countryCode: "GB"
  385. streetLine1: "Test Street"
  386. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  387. }
  388. ) {
  389. id
  390. ${customFieldsSelection}
  391. }
  392. }
  393. `);
  394. assertCustomFieldIds(createCustomerAddress.customFields, 'T_1', ['T_1', 'T_2']);
  395. });
  396. it('admin updateCustomerAddress', async () => {
  397. const { updateCustomerAddress } = await adminClient.query(gql`
  398. mutation {
  399. updateCustomerAddress(
  400. input: { id: "T_1", customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] } }
  401. ) {
  402. id
  403. ${customFieldsSelection}
  404. }
  405. }
  406. `);
  407. assertCustomFieldIds(updateCustomerAddress.customFields, 'T_2', ['T_3', 'T_4']);
  408. });
  409. it('shop updateCustomerAddress', async () => {
  410. const { updateCustomerAddress } = await shopClient.query(gql`
  411. mutation {
  412. updateCustomerAddress(
  413. input: { id: "T_1", customFields: { singleId: "T_3", multiIds: ["T_4", "T_2"] } }
  414. ) {
  415. id
  416. ${customFieldsSelection}
  417. }
  418. }
  419. `);
  420. assertCustomFieldIds(updateCustomerAddress.customFields, 'T_3', ['T_2', 'T_4']);
  421. });
  422. });
  423. describe('Collection entity', () => {
  424. let collectionId: string;
  425. it('admin createCollection', async () => {
  426. const { createCollection } = await adminClient.query(gql`
  427. mutation {
  428. createCollection(
  429. input: {
  430. translations: [
  431. { languageCode: en, name: "Test", description: "test", slug: "test" }
  432. ]
  433. filters: []
  434. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  435. }
  436. ) {
  437. id
  438. ${customFieldsSelection}
  439. }
  440. }
  441. `);
  442. assertCustomFieldIds(createCollection.customFields, 'T_1', ['T_1', 'T_2']);
  443. collectionId = createCollection.id;
  444. });
  445. it('admin updateCollection', async () => {
  446. const { updateCollection } = await adminClient.query(gql`
  447. mutation {
  448. updateCollection(
  449. input: {
  450. id: "${collectionId}"
  451. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  452. }
  453. ) {
  454. id
  455. ${customFieldsSelection}
  456. }
  457. }
  458. `);
  459. assertCustomFieldIds(updateCollection.customFields, 'T_2', ['T_3', 'T_4']);
  460. });
  461. // https://github.com/vendure-ecommerce/vendure/issues/2840
  462. it('updating custom field relation on Collection does not delete primitive values', async () => {
  463. const { updateCollection } = await adminClient.query(gql`
  464. mutation {
  465. updateCollection(
  466. input: {
  467. id: "${collectionId}"
  468. customFields: { singleId: "T_3" }
  469. }
  470. ) {
  471. id
  472. ${customFieldsSelection}
  473. }
  474. }
  475. `);
  476. expect(updateCollection.customFields.single).toEqual({ id: 'T_3' });
  477. expect(updateCollection.customFields.primitive).toBe('test');
  478. });
  479. });
  480. describe('Customer entity', () => {
  481. let customerId: string;
  482. it('admin createCustomer', async () => {
  483. const { createCustomer } = await adminClient.query(gql`
  484. mutation {
  485. createCustomer(
  486. input: {
  487. emailAddress: "test@test.com"
  488. firstName: "Test"
  489. lastName: "Person"
  490. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  491. }
  492. ) {
  493. ... on Customer {
  494. id
  495. ${customFieldsSelection}
  496. }
  497. }
  498. }
  499. `);
  500. assertCustomFieldIds(createCustomer.customFields, 'T_1', ['T_1', 'T_2']);
  501. customerId = createCustomer.id;
  502. });
  503. it('admin updateCustomer', async () => {
  504. const { updateCustomer } = await adminClient.query(gql`
  505. mutation {
  506. updateCustomer(
  507. input: {
  508. id: "${customerId}"
  509. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  510. }
  511. ) {
  512. ...on Customer {
  513. id
  514. ${customFieldsSelection}
  515. }
  516. }
  517. }
  518. `);
  519. assertCustomFieldIds(updateCustomer.customFields, 'T_2', ['T_3', 'T_4']);
  520. });
  521. it('shop updateCustomer', async () => {
  522. const { updateCustomer } = await shopClient.query(gql`
  523. mutation {
  524. updateCustomer(input: { customFields: { singleId: "T_4", multiIds: ["T_2", "T_4"] } }) {
  525. id
  526. ${customFieldsSelection}
  527. }
  528. }
  529. `);
  530. assertCustomFieldIds(updateCustomer.customFields, 'T_4', ['T_2', 'T_4']);
  531. });
  532. });
  533. describe('Facet entity', () => {
  534. let facetId: string;
  535. it('admin createFacet', async () => {
  536. const { createFacet } = await adminClient.query(gql`
  537. mutation {
  538. createFacet(
  539. input: {
  540. code: "test"
  541. isPrivate: false
  542. translations: [{ languageCode: en, name: "test" }]
  543. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  544. }
  545. ) {
  546. id
  547. ${customFieldsSelection}
  548. }
  549. }
  550. `);
  551. assertCustomFieldIds(createFacet.customFields, 'T_1', ['T_1', 'T_2']);
  552. facetId = createFacet.id;
  553. });
  554. it('admin updateFacet', async () => {
  555. const { updateFacet } = await adminClient.query(gql`
  556. mutation {
  557. updateFacet(
  558. input: {
  559. id: "${facetId}"
  560. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  561. }
  562. ) {
  563. id
  564. ${customFieldsSelection}
  565. }
  566. }
  567. `);
  568. assertCustomFieldIds(updateFacet.customFields, 'T_2', ['T_3', 'T_4']);
  569. });
  570. // https://github.com/vendure-ecommerce/vendure/issues/2840
  571. it('updating custom field relation on Facet does not delete primitive values', async () => {
  572. const { updateFacet } = await adminClient.query(gql`
  573. mutation {
  574. updateFacet(
  575. input: {
  576. id: "${facetId}"
  577. customFields: { singleId: "T_3" }
  578. }
  579. ) {
  580. id
  581. ${customFieldsSelection}
  582. }
  583. }
  584. `);
  585. expect(updateFacet.customFields.single).toEqual({ id: 'T_3' });
  586. expect(updateFacet.customFields.primitive).toBe('test');
  587. });
  588. });
  589. describe('FacetValue entity', () => {
  590. let facetValueId: string;
  591. it('admin createFacetValues', async () => {
  592. const { createFacetValues } = await adminClient.query(gql`
  593. mutation {
  594. createFacetValues(
  595. input: {
  596. code: "test"
  597. facetId: "T_1"
  598. translations: [{ languageCode: en, name: "test" }]
  599. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  600. }
  601. ) {
  602. id
  603. ${customFieldsSelection}
  604. }
  605. }
  606. `);
  607. assertCustomFieldIds(createFacetValues[0].customFields, 'T_1', ['T_1', 'T_2']);
  608. facetValueId = createFacetValues[0].id;
  609. });
  610. it('admin updateFacetValues', async () => {
  611. const { updateFacetValues } = await adminClient.query(gql`
  612. mutation {
  613. updateFacetValues(
  614. input: {
  615. id: "${facetValueId}"
  616. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  617. }
  618. ) {
  619. id
  620. ${customFieldsSelection}
  621. }
  622. }
  623. `);
  624. assertCustomFieldIds(updateFacetValues[0].customFields, 'T_2', ['T_3', 'T_4']);
  625. });
  626. // https://github.com/vendure-ecommerce/vendure/issues/2840
  627. it('updating custom field relation on FacetValue does not delete primitive values', async () => {
  628. const { updateFacetValues } = await adminClient.query(gql`
  629. mutation {
  630. updateFacetValues(
  631. input: {
  632. id: "${facetValueId}"
  633. customFields: { singleId: "T_3" }
  634. }
  635. ) {
  636. id
  637. ${customFieldsSelection}
  638. }
  639. }
  640. `);
  641. expect(updateFacetValues[0].customFields.single).toEqual({ id: 'T_3' });
  642. expect(updateFacetValues[0].customFields.primitive).toBe('test');
  643. });
  644. });
  645. describe('GlobalSettings entity', () => {
  646. it('admin updateGlobalSettings', async () => {
  647. const { updateGlobalSettings } = await adminClient.query(gql`
  648. mutation {
  649. updateGlobalSettings(
  650. input: {
  651. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  652. }
  653. ) {
  654. ... on GlobalSettings {
  655. id
  656. ${customFieldsSelection}
  657. }
  658. }
  659. }
  660. `);
  661. assertCustomFieldIds(updateGlobalSettings.customFields, 'T_2', ['T_3', 'T_4']);
  662. });
  663. });
  664. describe('Order entity', () => {
  665. let orderId: string;
  666. beforeAll(async () => {
  667. const { addItemToOrder } = await shopClient.query(addItemToOrderDocument, {
  668. productVariantId: 'T_1',
  669. quantity: 1,
  670. });
  671. orderId = (addItemToOrder as any).id;
  672. });
  673. it('shop setOrderCustomFields', async () => {
  674. const { setOrderCustomFields } = await shopClient.query(gql`
  675. mutation {
  676. setOrderCustomFields(
  677. input: {
  678. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  679. }
  680. ) {
  681. ... on Order {
  682. id
  683. ${customFieldsSelection}
  684. }
  685. }
  686. }
  687. `);
  688. assertCustomFieldIds(setOrderCustomFields.customFields, 'T_2', ['T_3', 'T_4']);
  689. });
  690. it('admin setOrderCustomFields', async () => {
  691. const { setOrderCustomFields } = await adminClient.query(gql`
  692. mutation {
  693. setOrderCustomFields(
  694. input: {
  695. id: "${orderId}"
  696. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  697. }
  698. ) {
  699. ... on Order {
  700. id
  701. ${customFieldsSelection}
  702. }
  703. }
  704. }
  705. `);
  706. assertCustomFieldIds(setOrderCustomFields.customFields, 'T_1', ['T_1', 'T_2']);
  707. });
  708. // https://github.com/vendure-ecommerce/vendure/issues/1664#issuecomment-1320872627
  709. it('admin order query with eager-loaded custom field relation', async () => {
  710. const { order } = await adminClient.query(gql`
  711. query {
  712. order(id: 1) {
  713. id
  714. customFields {
  715. productOwner {
  716. id
  717. }
  718. }
  719. }
  720. }
  721. `);
  722. // we're just making sure it does not throw here.
  723. expect(order).toEqual({
  724. customFields: {
  725. productOwner: null,
  726. },
  727. id: 'T_1',
  728. });
  729. });
  730. });
  731. describe('OrderLine entity', () => {
  732. it('shop addItemToOrder', async () => {
  733. const { addItemToOrder } = await shopClient.query(
  734. gql`mutation {
  735. addItemToOrder(productVariantId: "T_1", quantity: 1, customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }) {
  736. ... on Order {
  737. id
  738. ${customFieldsSelection}
  739. }
  740. }
  741. }`,
  742. );
  743. assertCustomFieldIds(addItemToOrder.customFields, 'T_1', ['T_1', 'T_2']);
  744. });
  745. });
  746. describe('Product, ProductVariant entity', () => {
  747. let productId: string;
  748. it('admin createProduct', async () => {
  749. const { createProduct } = await adminClient.query(gql`
  750. mutation {
  751. createProduct(
  752. input: {
  753. translations: [{ languageCode: en, name: "test" slug: "test" description: "test" }]
  754. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  755. }
  756. ) {
  757. id
  758. ${customFieldsSelection}
  759. }
  760. }
  761. `);
  762. assertCustomFieldIds(createProduct.customFields, 'T_1', ['T_1', 'T_2']);
  763. productId = createProduct.id;
  764. });
  765. it('admin updateProduct', async () => {
  766. const { updateProduct } = await adminClient.query(gql`
  767. mutation {
  768. updateProduct(
  769. input: {
  770. id: "${productId}"
  771. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  772. }
  773. ) {
  774. id
  775. ${customFieldsSelection}
  776. }
  777. }
  778. `);
  779. assertCustomFieldIds(updateProduct.customFields, 'T_2', ['T_3', 'T_4']);
  780. });
  781. // https://github.com/vendure-ecommerce/vendure/issues/2840
  782. it('updating custom field relation on Product does not delete primitive values', async () => {
  783. const { updateProduct } = await adminClient.query(gql`
  784. mutation {
  785. updateProduct(
  786. input: {
  787. id: "${productId}"
  788. customFields: { singleId: "T_3" }
  789. }
  790. ) {
  791. id
  792. ${customFieldsSelection}
  793. }
  794. }
  795. `);
  796. expect(updateProduct.customFields.single).toEqual({ id: 'T_3' });
  797. expect(updateProduct.customFields.primitive).toBe('test');
  798. });
  799. let productVariantId: string;
  800. it('admin createProductVariant', async () => {
  801. const { createProductVariants } = await adminClient.query(gql`
  802. mutation {
  803. createProductVariants(
  804. input: [{
  805. sku: "TEST01"
  806. productId: "${productId}"
  807. translations: [{ languageCode: en, name: "test" }]
  808. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  809. }]
  810. ) {
  811. id
  812. ${customFieldsSelection}
  813. }
  814. }
  815. `);
  816. assertCustomFieldIds(createProductVariants[0].customFields, 'T_1', ['T_1', 'T_2']);
  817. productVariantId = createProductVariants[0].id;
  818. });
  819. it('admin updateProductVariant', async () => {
  820. const { updateProductVariants } = await adminClient.query(gql`
  821. mutation {
  822. updateProductVariants(
  823. input: [{
  824. id: "${productVariantId}"
  825. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  826. }]
  827. ) {
  828. id
  829. ${customFieldsSelection}
  830. }
  831. }
  832. `);
  833. assertCustomFieldIds(updateProductVariants[0].customFields, 'T_2', ['T_3', 'T_4']);
  834. });
  835. // https://github.com/vendure-ecommerce/vendure/issues/2840
  836. it('updating custom field relation on ProductVariant does not delete primitive values', async () => {
  837. const { updateProductVariants } = await adminClient.query(gql`
  838. mutation {
  839. updateProductVariants(
  840. input: [{
  841. id: "${productVariantId}"
  842. customFields: { singleId: "T_3" }
  843. }]
  844. ) {
  845. id
  846. ${customFieldsSelection}
  847. }
  848. }
  849. `);
  850. expect(updateProductVariants[0].customFields.single).toEqual({ id: 'T_3' });
  851. expect(updateProductVariants[0].customFields.primitive).toBe('test');
  852. });
  853. describe('issue 1664', () => {
  854. // https://github.com/vendure-ecommerce/vendure/issues/1664
  855. it('successfully gets product by id with eager-loading custom field relation', async () => {
  856. const { product } = await shopClient.query(gql`
  857. query {
  858. product(id: "T_1") {
  859. id
  860. customFields {
  861. cfVendor {
  862. featuredProduct {
  863. id
  864. }
  865. }
  866. }
  867. }
  868. }
  869. `);
  870. expect(product).toBeDefined();
  871. });
  872. // https://github.com/vendure-ecommerce/vendure/issues/1664
  873. it('successfully gets product by id with nested eager-loading custom field relation', async () => {
  874. const { customer } = await adminClient.query(gql`
  875. query {
  876. customer(id: "T_1") {
  877. id
  878. firstName
  879. lastName
  880. emailAddress
  881. phoneNumber
  882. user {
  883. customFields {
  884. cfVendor {
  885. id
  886. }
  887. }
  888. }
  889. }
  890. }
  891. `);
  892. expect(customer).toBeDefined();
  893. });
  894. // https://github.com/vendure-ecommerce/vendure/issues/1664
  895. it('successfully gets product.variants with nested custom field relation', async () => {
  896. await adminClient.query(gql`
  897. mutation {
  898. updateProductVariants(
  899. input: [{ id: "T_1", customFields: { cfRelatedProductsIds: ["T_2"] } }]
  900. ) {
  901. id
  902. }
  903. }
  904. `);
  905. const { product } = await adminClient.query(gql`
  906. query {
  907. product(id: "T_1") {
  908. variants {
  909. id
  910. customFields {
  911. cfRelatedProducts {
  912. featuredAsset {
  913. id
  914. }
  915. }
  916. }
  917. }
  918. }
  919. }
  920. `);
  921. expect(product).toBeDefined();
  922. expect(product.variants[0].customFields.cfRelatedProducts).toEqual([
  923. {
  924. featuredAsset: { id: 'T_2' },
  925. },
  926. ]);
  927. });
  928. it('successfully gets product by slug with eager-loading custom field relation', async () => {
  929. const { product } = await shopClient.query(gql`
  930. query {
  931. product(slug: "laptop") {
  932. id
  933. customFields {
  934. cfVendor {
  935. featuredProduct {
  936. id
  937. }
  938. }
  939. }
  940. }
  941. }
  942. `);
  943. expect(product).toBeDefined();
  944. });
  945. it('does not error on custom field relation with eager custom field relation', async () => {
  946. const { product } = await adminClient.query(gql`
  947. query {
  948. product(slug: "laptop") {
  949. name
  950. customFields {
  951. owner {
  952. id
  953. code
  954. customFields {
  955. profile {
  956. id
  957. name
  958. }
  959. }
  960. }
  961. }
  962. }
  963. }
  964. `);
  965. expect(product).toBeDefined();
  966. });
  967. });
  968. });
  969. describe('ProductOptionGroup, ProductOption entity', () => {
  970. let productOptionGroupId: string;
  971. it('admin createProductOptionGroup', async () => {
  972. const { createProductOptionGroup } = await adminClient.query(gql`
  973. mutation {
  974. createProductOptionGroup(
  975. input: {
  976. code: "test"
  977. options: []
  978. translations: [{ languageCode: en, name: "test" }]
  979. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  980. }
  981. ) {
  982. id
  983. ${customFieldsSelection}
  984. }
  985. }
  986. `);
  987. assertCustomFieldIds(createProductOptionGroup.customFields, 'T_1', ['T_1', 'T_2']);
  988. productOptionGroupId = createProductOptionGroup.id;
  989. });
  990. it('admin updateProductOptionGroup', async () => {
  991. const { updateProductOptionGroup } = await adminClient.query(gql`
  992. mutation {
  993. updateProductOptionGroup(
  994. input: {
  995. id: "${productOptionGroupId}"
  996. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  997. }
  998. ) {
  999. id
  1000. ${customFieldsSelection}
  1001. }
  1002. }
  1003. `);
  1004. assertCustomFieldIds(updateProductOptionGroup.customFields, 'T_2', ['T_3', 'T_4']);
  1005. });
  1006. // https://github.com/vendure-ecommerce/vendure/issues/2840
  1007. it('updating custom field relation on ProductOptionGroup does not delete primitive values', async () => {
  1008. const { updateProductOptionGroup } = await adminClient.query(gql`
  1009. mutation {
  1010. updateProductOptionGroup(
  1011. input: {
  1012. id: "${productOptionGroupId}"
  1013. customFields: { singleId: "T_3" }
  1014. }
  1015. ) {
  1016. id
  1017. ${customFieldsSelection}
  1018. }
  1019. }
  1020. `);
  1021. expect(updateProductOptionGroup.customFields.single).toEqual({ id: 'T_3' });
  1022. expect(updateProductOptionGroup.customFields.primitive).toBe('test');
  1023. });
  1024. let productOptionId: string;
  1025. it('admin createProductOption', async () => {
  1026. const { createProductOption } = await adminClient.query(gql`
  1027. mutation {
  1028. createProductOption(
  1029. input: {
  1030. productOptionGroupId: "${productOptionGroupId}"
  1031. code: "test-option"
  1032. translations: [{ languageCode: en, name: "test-option" }]
  1033. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  1034. }
  1035. ) {
  1036. id
  1037. ${customFieldsSelection}
  1038. }
  1039. }
  1040. `);
  1041. assertCustomFieldIds(createProductOption.customFields, 'T_1', ['T_1', 'T_2']);
  1042. productOptionId = createProductOption.id;
  1043. });
  1044. it('admin updateProductOption', async () => {
  1045. const { updateProductOption } = await adminClient.query(gql`
  1046. mutation {
  1047. updateProductOption(
  1048. input: {
  1049. id: "${productOptionId}"
  1050. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  1051. }
  1052. ) {
  1053. id
  1054. ${customFieldsSelection}
  1055. }
  1056. }
  1057. `);
  1058. assertCustomFieldIds(updateProductOption.customFields, 'T_2', ['T_3', 'T_4']);
  1059. });
  1060. // https://github.com/vendure-ecommerce/vendure/issues/2840
  1061. it('updating custom field relation on ProductOption does not delete primitive values', async () => {
  1062. const { updateProductOption } = await adminClient.query(gql`
  1063. mutation {
  1064. updateProductOption(
  1065. input: {
  1066. id: "${productOptionId}"
  1067. customFields: { singleId: "T_3" }
  1068. }
  1069. ) {
  1070. id
  1071. ${customFieldsSelection}
  1072. }
  1073. }
  1074. `);
  1075. expect(updateProductOption.customFields.single).toEqual({ id: 'T_3' });
  1076. expect(updateProductOption.customFields.primitive).toBe('test');
  1077. });
  1078. });
  1079. describe('ShippingMethod entity', () => {
  1080. let shippingMethodId: string;
  1081. it('admin createShippingMethod', async () => {
  1082. const { createShippingMethod } = await adminClient.query(gql`
  1083. mutation {
  1084. createShippingMethod(
  1085. input: {
  1086. code: "test"
  1087. calculator: {
  1088. code: "${defaultShippingCalculator.code}"
  1089. arguments: [
  1090. { name: "rate" value: "10"},
  1091. { name: "includesTax" value: "true"},
  1092. { name: "taxRate" value: "10"},
  1093. ]
  1094. }
  1095. checker: {
  1096. code: "${defaultShippingEligibilityChecker.code}"
  1097. arguments: [
  1098. { name: "orderMinimum" value: "0"},
  1099. ]
  1100. }
  1101. fulfillmentHandler: "${manualFulfillmentHandler.code}"
  1102. translations: [{ languageCode: en, name: "test" description: "test" }]
  1103. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
  1104. }
  1105. ) {
  1106. id
  1107. ${customFieldsSelection}
  1108. }
  1109. }
  1110. `);
  1111. assertCustomFieldIds(createShippingMethod.customFields, 'T_1', ['T_1', 'T_2']);
  1112. shippingMethodId = createShippingMethod.id;
  1113. });
  1114. it('admin updateShippingMethod', async () => {
  1115. const { updateShippingMethod } = await adminClient.query(gql`
  1116. mutation {
  1117. updateShippingMethod(
  1118. input: {
  1119. id: "${shippingMethodId}"
  1120. translations: []
  1121. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  1122. }
  1123. ) {
  1124. id
  1125. ${customFieldsSelection}
  1126. }
  1127. }
  1128. `);
  1129. assertCustomFieldIds(updateShippingMethod.customFields, 'T_2', ['T_3', 'T_4']);
  1130. });
  1131. // https://github.com/vendure-ecommerce/vendure/issues/2840
  1132. it('updating custom field relation on ShippingMethod does not delete primitive values', async () => {
  1133. const { updateShippingMethod } = await adminClient.query(gql`
  1134. mutation {
  1135. updateShippingMethod(
  1136. input: {
  1137. id: "${shippingMethodId}"
  1138. translations: []
  1139. customFields: { singleId: "T_3" }
  1140. }
  1141. ) {
  1142. id
  1143. ${customFieldsSelection}
  1144. }
  1145. }
  1146. `);
  1147. expect(updateShippingMethod.customFields.single).toEqual({ id: 'T_3' });
  1148. expect(updateShippingMethod.customFields.primitive).toBe('test');
  1149. });
  1150. it('shop eligibleShippingMethods (ShippingMethodQuote)', async () => {
  1151. const { eligibleShippingMethods } = await shopClient.query(gql`
  1152. query {
  1153. eligibleShippingMethods {
  1154. id
  1155. name
  1156. code
  1157. description
  1158. ${customFieldsSelection}
  1159. }
  1160. }
  1161. `);
  1162. const testShippingMethodQuote = eligibleShippingMethods.find(
  1163. (quote: any) => quote.code === 'test',
  1164. );
  1165. assertCustomFieldIds(testShippingMethodQuote.customFields, 'T_3', ['T_3', 'T_4']);
  1166. });
  1167. });
  1168. describe('PaymentMethod entity', () => {
  1169. let paymentMethodId: string;
  1170. it('admin createShippingMethod', async () => {
  1171. const { createPaymentMethod } = await adminClient.query(gql`
  1172. mutation {
  1173. createPaymentMethod(
  1174. input: {
  1175. code: "test"
  1176. enabled: true
  1177. handler: {
  1178. code: "${testSuccessfulPaymentMethod.code}"
  1179. arguments: []
  1180. }
  1181. customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] },
  1182. translations: [{ languageCode: en, name: "test" }]
  1183. }
  1184. ) {
  1185. id
  1186. ${customFieldsSelection}
  1187. }
  1188. }
  1189. `);
  1190. assertCustomFieldIds(createPaymentMethod.customFields, 'T_1', ['T_1', 'T_2']);
  1191. paymentMethodId = createPaymentMethod.id;
  1192. });
  1193. it('admin updatePaymentMethod', async () => {
  1194. const { updatePaymentMethod } = await adminClient.query(gql`
  1195. mutation {
  1196. updatePaymentMethod(
  1197. input: {
  1198. id: "${paymentMethodId}"
  1199. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] },
  1200. }
  1201. ) {
  1202. id
  1203. ${customFieldsSelection}
  1204. }
  1205. }
  1206. `);
  1207. assertCustomFieldIds(updatePaymentMethod.customFields, 'T_2', ['T_3', 'T_4']);
  1208. });
  1209. // https://github.com/vendure-ecommerce/vendure/issues/2840
  1210. it('updating custom field relation on PaymentMethod does not delete primitive values', async () => {
  1211. const { updatePaymentMethod } = await adminClient.query(gql`
  1212. mutation {
  1213. updatePaymentMethod(
  1214. input: {
  1215. id: "${paymentMethodId}"
  1216. customFields: { singleId: "T_3" }
  1217. }
  1218. ) {
  1219. id
  1220. ${customFieldsSelection}
  1221. }
  1222. }
  1223. `);
  1224. expect(updatePaymentMethod.customFields.single).toEqual({ id: 'T_3' });
  1225. expect(updatePaymentMethod.customFields.primitive).toBe('test');
  1226. });
  1227. it('shop eligiblePaymentMethods (PaymentMethodQuote)', async () => {
  1228. const { eligiblePaymentMethods } = await shopClient.query(gql`
  1229. query {
  1230. eligiblePaymentMethods {
  1231. id
  1232. name
  1233. description
  1234. ${customFieldsSelection}
  1235. }
  1236. }
  1237. `);
  1238. assertCustomFieldIds(eligiblePaymentMethods[0].customFields, 'T_3', ['T_3', 'T_4']);
  1239. });
  1240. });
  1241. describe('Asset entity', () => {
  1242. it('set custom field relations on Asset', async () => {
  1243. const { updateAsset } = await adminClient.query(gql`
  1244. mutation {
  1245. updateAsset(
  1246. input: {
  1247. id: "T_1",
  1248. customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
  1249. }
  1250. ) {
  1251. id
  1252. ${customFieldsSelection}
  1253. }
  1254. }
  1255. `);
  1256. assertCustomFieldIds(updateAsset.customFields, 'T_2', ['T_3', 'T_4']);
  1257. });
  1258. it('findOne on Asset', async () => {
  1259. const { asset } = await adminClient.query(gql`
  1260. query {
  1261. asset(id: "T_1") {
  1262. id
  1263. ${customFieldsSelection}
  1264. }
  1265. }
  1266. `);
  1267. expect(asset.customFields.single.id).toBe('T_2');
  1268. expect(asset.customFields.multi.length).toEqual(2);
  1269. });
  1270. // https://github.com/vendure-ecommerce/vendure/issues/1636
  1271. it('calling TransactionalConnection.findOneInChannel() returns custom field relations', async () => {
  1272. TestPlugin1636_1664.testResolverSpy.mockReset();
  1273. await shopClient.query(gql`
  1274. query {
  1275. getAssetTest(id: "T_1")
  1276. }
  1277. `);
  1278. const args = TestPlugin1636_1664.testResolverSpy.mock.calls[0];
  1279. expect(args[0].customFields.single.id).toEqual(2);
  1280. expect(args[0].customFields.multi.length).toEqual(2);
  1281. });
  1282. });
  1283. });
  1284. it('null values', async () => {
  1285. const { updateCustomerAddress } = await adminClient.query(gql`
  1286. mutation {
  1287. updateCustomerAddress(
  1288. input: { id: "T_1", customFields: { singleId: null, multiIds: ["T_1", "null"] } }
  1289. ) {
  1290. id
  1291. customFields {
  1292. single {
  1293. id
  1294. }
  1295. multi {
  1296. id
  1297. }
  1298. }
  1299. }
  1300. }
  1301. `);
  1302. expect(updateCustomerAddress.customFields.single).toEqual(null);
  1303. expect(updateCustomerAddress.customFields.multi).toEqual([{ id: 'T_1' }]);
  1304. });
  1305. describe('bi-direction relations', () => {
  1306. let customEntityRepository: Repository<TestCustomEntity>;
  1307. let customEntity: TestCustomEntity;
  1308. let collectionIdInternal: number;
  1309. beforeAll(async () => {
  1310. customEntityRepository = server.app
  1311. .get(TransactionalConnection)
  1312. .getRepository(RequestContext.empty(), TestCustomEntity);
  1313. const customEntityId = (await customEntityRepository.save({})).id;
  1314. const { createCollection } = await adminClient.query(gql`
  1315. mutation {
  1316. createCollection(
  1317. input: {
  1318. translations: [
  1319. { languageCode: en, name: "Test", description: "test", slug: "test" }
  1320. ]
  1321. filters: []
  1322. customFields: { customEntityListIds: [${customEntityId}] customEntityId: ${customEntityId} }
  1323. }
  1324. ) {
  1325. id
  1326. }
  1327. }
  1328. `);
  1329. collectionIdInternal = parseInt(createCollection.id.replace('T_', ''), 10);
  1330. customEntity = await assertFound(
  1331. customEntityRepository.findOne({
  1332. where: { id: customEntityId },
  1333. relations: {
  1334. customEntityInverse: true,
  1335. customEntityListInverse: true,
  1336. },
  1337. }),
  1338. );
  1339. });
  1340. it('can create inverse relation for list=false', () => {
  1341. expect(customEntity.customEntityInverse).toEqual([
  1342. expect.objectContaining({ id: collectionIdInternal }),
  1343. ]);
  1344. });
  1345. it('can create inverse relation for list=true', () => {
  1346. expect(customEntity.customEntityListInverse).toEqual([
  1347. expect.objectContaining({ id: collectionIdInternal }),
  1348. ]);
  1349. });
  1350. });
  1351. });