| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463 |
- // this file relies on defintions that are only available at runtime, therefore we still use gql here.
- import {
- assertFound,
- Asset,
- Collection,
- Country,
- CustomFields,
- defaultShippingCalculator,
- defaultShippingEligibilityChecker,
- Facet,
- FacetValue,
- manualFulfillmentHandler,
- mergeConfig,
- Product,
- ProductOption,
- ProductOptionGroup,
- ProductVariant,
- RequestContext,
- ShippingMethod,
- TransactionalConnection,
- } from '@vendure/core';
- import { createTestEnvironment } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { Repository } from 'typeorm';
- import { afterAll, beforeAll, describe, expect, it } from 'vitest';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import { testSuccessfulPaymentMethod } from './fixtures/test-payment-methods';
- import { TestPlugin1636_1664 } from './fixtures/test-plugins/issue-1636-1664/issue-1636-1664-plugin';
- import { PluginIssue2453 } from './fixtures/test-plugins/issue-2453/plugin-issue2453';
- import { TestCustomEntity, WithCustomEntity } from './fixtures/test-plugins/with-custom-entity';
- import { addItemToOrderDocument } from './graphql/shop-definitions';
- import { sortById } from './utils/test-order-utils';
- const entitiesWithCustomFields: Array<keyof CustomFields> = [
- 'Address',
- 'Administrator',
- 'Asset',
- 'Channel',
- 'Collection',
- 'Customer',
- 'CustomerGroup',
- 'Facet',
- 'FacetValue',
- 'Fulfillment',
- 'GlobalSettings',
- 'Order',
- 'OrderLine',
- 'PaymentMethod',
- 'Product',
- 'ProductOption',
- 'ProductOptionGroup',
- 'ProductVariant',
- 'Promotion',
- 'Region',
- 'Seller',
- 'ShippingMethod',
- 'TaxCategory',
- 'TaxRate',
- 'User',
- 'Zone',
- ];
- const customFieldConfig: CustomFields = {};
- for (const entity of entitiesWithCustomFields) {
- customFieldConfig[entity] = [
- { name: 'primitive', type: 'string', list: false, defaultValue: 'test' },
- { name: 'single', type: 'relation', entity: Asset, graphQLType: 'Asset', list: false },
- { name: 'multi', type: 'relation', entity: Asset, graphQLType: 'Asset', list: true },
- ];
- }
- customFieldConfig.Product?.push(
- { name: 'cfCollection', type: 'relation', entity: Collection, list: false },
- { name: 'cfCountry', type: 'relation', entity: Country, list: false },
- { name: 'cfFacetValue', type: 'relation', entity: FacetValue, list: false },
- { name: 'cfFacet', type: 'relation', entity: Facet, list: false },
- { name: 'cfProductOptionGroup', type: 'relation', entity: ProductOptionGroup, list: false },
- { name: 'cfProductOption', type: 'relation', entity: ProductOption, list: false },
- { name: 'cfProductVariant', type: 'relation', entity: ProductVariant, list: false },
- { name: 'cfProduct', type: 'relation', entity: Product, list: false },
- { name: 'cfShippingMethod', type: 'relation', entity: ShippingMethod, list: false },
- { name: 'cfInternalAsset', type: 'relation', entity: Asset, list: false, internal: true },
- );
- customFieldConfig.ProductVariant?.push({
- name: 'cfRelatedProducts',
- type: 'relation',
- entity: Product,
- list: true,
- internal: false,
- public: true,
- });
- const customConfig = mergeConfig(testConfig(), {
- paymentOptions: {
- paymentMethodHandlers: [testSuccessfulPaymentMethod],
- },
- dbConnectionOptions: {
- timezone: 'Z',
- },
- customFields: customFieldConfig,
- plugins: [TestPlugin1636_1664, WithCustomEntity, PluginIssue2453],
- });
- describe('Custom field relations', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(customConfig);
- beforeAll(async () => {
- await server.init({
- initialData,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-full.csv'),
- customerCount: 3,
- });
- await adminClient.asSuperAdmin();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('customFieldConfig query returns entity and scalar fields', async () => {
- const { globalSettings } = await adminClient.query(gql`
- query {
- globalSettings {
- serverConfig {
- customFieldConfig {
- Customer {
- ... on RelationCustomFieldConfig {
- name
- entity
- scalarFields
- }
- }
- }
- }
- }
- }
- `);
- const single = globalSettings.serverConfig.customFieldConfig.Customer[1];
- expect(single.entity).toBe('Asset');
- expect(single.scalarFields).toEqual([
- 'id',
- 'createdAt',
- 'updatedAt',
- 'name',
- 'type',
- 'fileSize',
- 'mimeType',
- 'width',
- 'height',
- 'source',
- 'preview',
- ]);
- });
- describe('special data resolution', () => {
- let productId: string;
- const productCustomFieldRelationsSelection = `
- id
- customFields {
- cfCollection {
- languageCode
- name
- }
- cfCountry {
- languageCode
- name
- }
- cfFacetValue {
- languageCode
- name
- }
- cfFacet {
- languageCode
- name
- }
- cfProductOptionGroup {
- languageCode
- name
- }
- cfProductOption {
- languageCode
- name
- }
- cfProductVariant {
- languageCode
- name
- }
- cfProduct {
- languageCode
- name
- }
- cfShippingMethod {
- languageCode
- name
- }
- }`;
- function assertTranslatableCustomFieldValues(product: { customFields: any }) {
- expect(product.customFields.cfCollection).toEqual({
- languageCode: 'en',
- name: '__root_collection__',
- });
- expect(product.customFields.cfCountry).toEqual({ languageCode: 'en', name: 'Australia' });
- expect(product.customFields.cfFacetValue).toEqual({
- languageCode: 'en',
- name: 'electronics',
- });
- expect(product.customFields.cfFacet).toEqual({ languageCode: 'en', name: 'category' });
- expect(product.customFields.cfProductOptionGroup).toEqual({
- languageCode: 'en',
- name: 'screen size',
- });
- expect(product.customFields.cfProductOption).toEqual({
- languageCode: 'en',
- name: '13 inch',
- });
- expect(product.customFields.cfProductVariant).toEqual({
- languageCode: 'en',
- name: 'Laptop 13 inch 8GB',
- });
- expect(product.customFields.cfProduct).toEqual({ languageCode: 'en', name: 'Laptop' });
- expect(product.customFields.cfShippingMethod).toEqual({
- languageCode: 'en',
- name: 'Standard Shipping',
- });
- }
- it('translatable entities get translated', async () => {
- const { createProduct } = await adminClient.query(gql`
- mutation {
- createProduct(
- input: {
- translations: [
- {
- languageCode: en
- name: "Test product"
- description: ""
- slug: "test-product"
- }
- ]
- customFields: {
- cfCollectionId: "T_1"
- cfCountryId: "T_1"
- cfFacetValueId: "T_1"
- cfFacetId: "T_1"
- cfProductOptionGroupId: "T_1"
- cfProductOptionId: "T_1"
- cfProductVariantId: "T_1"
- cfProductId: "T_1"
- cfShippingMethodId: "T_1"
- }
- }
- ) { ${productCustomFieldRelationsSelection} }
- }
- `);
- productId = createProduct.id;
- assertTranslatableCustomFieldValues(createProduct);
- });
- it('translatable entities get translated on findOneInChannel', async () => {
- const { product } = await adminClient.query(gql`
- query {
- product(id: "${productId}") { ${productCustomFieldRelationsSelection} }
- }
- `);
- assertTranslatableCustomFieldValues(product);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2453
- it('translatable eager-loaded relation works (issue 2453)', async () => {
- const { collections } = await adminClient.query(gql`
- query {
- collections(options: { sort: { name: DESC } }) {
- totalItems
- items {
- id
- name
- customFields {
- campaign {
- name
- languageCode
- }
- }
- }
- }
- }
- `);
- expect(collections.totalItems).toBe(3);
- expect(collections.items.find((c: any) => c.id === 'T_3')).toEqual({
- customFields: {
- campaign: {
- languageCode: 'en',
- name: 'Clearance Up to 70% Off frames',
- },
- },
- id: 'T_3',
- name: 'children collection',
- });
- });
- it('ProductVariant prices get resolved', async () => {
- const { product } = await adminClient.query(gql`
- query {
- product(id: "${productId}") {
- id
- customFields {
- cfProductVariant {
- price
- currencyCode
- priceWithTax
- }
- }
- }
- }`);
- expect(product.customFields.cfProductVariant).toEqual({
- price: 129900,
- currencyCode: 'USD',
- priceWithTax: 155880,
- });
- });
- });
- it('ProductVariant without a specified property value returns null', async () => {
- const { createProduct } = await adminClient.query(gql`
- mutation {
- createProduct(
- input: {
- translations: [
- {
- languageCode: en
- name: "Product with empty custom fields"
- description: ""
- slug: "product-with-empty-custom-fields"
- }
- ]
- }
- ) {
- id
- }
- }
- `);
- const { product } = await adminClient.query(gql`
- query {
- product(id: "${createProduct.id}") {
- id
- customFields {
- cfProductVariant{
- price
- currencyCode
- priceWithTax
- }
- }
- }
- }`);
- expect(product.customFields.cfProductVariant).toEqual(null);
- });
- describe('entity-specific implementation', () => {
- function assertCustomFieldIds(customFields: any, single: string, multi: string[]) {
- expect(customFields.single).toEqual({ id: single });
- expect(customFields.multi.sort(sortById)).toEqual(multi.map(id => ({ id })));
- }
- const customFieldsSelection = `
- customFields {
- primitive
- single {
- id
- }
- multi {
- id
- }
- }`;
- describe('Address entity', () => {
- it('admin createCustomerAddress', async () => {
- const { createCustomerAddress } = await adminClient.query(gql`
- mutation {
- createCustomerAddress(
- customerId: "T_1"
- input: {
- countryCode: "GB"
- streetLine1: "Test Street"
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createCustomerAddress.customFields, 'T_1', ['T_1', 'T_2']);
- });
- it('shop createCustomerAddress', async () => {
- await shopClient.asUserWithCredentials('hayden.zieme12@hotmail.com', 'test');
- const { createCustomerAddress } = await shopClient.query(gql`
- mutation {
- createCustomerAddress(
- input: {
- countryCode: "GB"
- streetLine1: "Test Street"
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createCustomerAddress.customFields, 'T_1', ['T_1', 'T_2']);
- });
- it('admin updateCustomerAddress', async () => {
- const { updateCustomerAddress } = await adminClient.query(gql`
- mutation {
- updateCustomerAddress(
- input: { id: "T_1", customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] } }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateCustomerAddress.customFields, 'T_2', ['T_3', 'T_4']);
- });
- it('shop updateCustomerAddress', async () => {
- const { updateCustomerAddress } = await shopClient.query(gql`
- mutation {
- updateCustomerAddress(
- input: { id: "T_1", customFields: { singleId: "T_3", multiIds: ["T_4", "T_2"] } }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateCustomerAddress.customFields, 'T_3', ['T_2', 'T_4']);
- });
- });
- describe('Collection entity', () => {
- let collectionId: string;
- it('admin createCollection', async () => {
- const { createCollection } = await adminClient.query(gql`
- mutation {
- createCollection(
- input: {
- translations: [
- { languageCode: en, name: "Test", description: "test", slug: "test" }
- ]
- filters: []
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createCollection.customFields, 'T_1', ['T_1', 'T_2']);
- collectionId = createCollection.id;
- });
- it('admin updateCollection', async () => {
- const { updateCollection } = await adminClient.query(gql`
- mutation {
- updateCollection(
- input: {
- id: "${collectionId}"
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateCollection.customFields, 'T_2', ['T_3', 'T_4']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2840
- it('updating custom field relation on Collection does not delete primitive values', async () => {
- const { updateCollection } = await adminClient.query(gql`
- mutation {
- updateCollection(
- input: {
- id: "${collectionId}"
- customFields: { singleId: "T_3" }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(updateCollection.customFields.single).toEqual({ id: 'T_3' });
- expect(updateCollection.customFields.primitive).toBe('test');
- });
- });
- describe('Customer entity', () => {
- let customerId: string;
- it('admin createCustomer', async () => {
- const { createCustomer } = await adminClient.query(gql`
- mutation {
- createCustomer(
- input: {
- emailAddress: "test@test.com"
- firstName: "Test"
- lastName: "Person"
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- ... on Customer {
- id
- ${customFieldsSelection}
- }
- }
- }
- `);
- assertCustomFieldIds(createCustomer.customFields, 'T_1', ['T_1', 'T_2']);
- customerId = createCustomer.id;
- });
- it('admin updateCustomer', async () => {
- const { updateCustomer } = await adminClient.query(gql`
- mutation {
- updateCustomer(
- input: {
- id: "${customerId}"
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- ...on Customer {
- id
- ${customFieldsSelection}
- }
- }
- }
- `);
- assertCustomFieldIds(updateCustomer.customFields, 'T_2', ['T_3', 'T_4']);
- });
- it('shop updateCustomer', async () => {
- const { updateCustomer } = await shopClient.query(gql`
- mutation {
- updateCustomer(input: { customFields: { singleId: "T_4", multiIds: ["T_2", "T_4"] } }) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateCustomer.customFields, 'T_4', ['T_2', 'T_4']);
- });
- });
- describe('Facet entity', () => {
- let facetId: string;
- it('admin createFacet', async () => {
- const { createFacet } = await adminClient.query(gql`
- mutation {
- createFacet(
- input: {
- code: "test"
- isPrivate: false
- translations: [{ languageCode: en, name: "test" }]
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createFacet.customFields, 'T_1', ['T_1', 'T_2']);
- facetId = createFacet.id;
- });
- it('admin updateFacet', async () => {
- const { updateFacet } = await adminClient.query(gql`
- mutation {
- updateFacet(
- input: {
- id: "${facetId}"
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateFacet.customFields, 'T_2', ['T_3', 'T_4']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2840
- it('updating custom field relation on Facet does not delete primitive values', async () => {
- const { updateFacet } = await adminClient.query(gql`
- mutation {
- updateFacet(
- input: {
- id: "${facetId}"
- customFields: { singleId: "T_3" }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(updateFacet.customFields.single).toEqual({ id: 'T_3' });
- expect(updateFacet.customFields.primitive).toBe('test');
- });
- });
- describe('FacetValue entity', () => {
- let facetValueId: string;
- it('admin createFacetValues', async () => {
- const { createFacetValues } = await adminClient.query(gql`
- mutation {
- createFacetValues(
- input: {
- code: "test"
- facetId: "T_1"
- translations: [{ languageCode: en, name: "test" }]
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createFacetValues[0].customFields, 'T_1', ['T_1', 'T_2']);
- facetValueId = createFacetValues[0].id;
- });
- it('admin updateFacetValues', async () => {
- const { updateFacetValues } = await adminClient.query(gql`
- mutation {
- updateFacetValues(
- input: {
- id: "${facetValueId}"
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateFacetValues[0].customFields, 'T_2', ['T_3', 'T_4']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2840
- it('updating custom field relation on FacetValue does not delete primitive values', async () => {
- const { updateFacetValues } = await adminClient.query(gql`
- mutation {
- updateFacetValues(
- input: {
- id: "${facetValueId}"
- customFields: { singleId: "T_3" }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(updateFacetValues[0].customFields.single).toEqual({ id: 'T_3' });
- expect(updateFacetValues[0].customFields.primitive).toBe('test');
- });
- });
- describe('GlobalSettings entity', () => {
- it('admin updateGlobalSettings', async () => {
- const { updateGlobalSettings } = await adminClient.query(gql`
- mutation {
- updateGlobalSettings(
- input: {
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- ... on GlobalSettings {
- id
- ${customFieldsSelection}
- }
- }
- }
- `);
- assertCustomFieldIds(updateGlobalSettings.customFields, 'T_2', ['T_3', 'T_4']);
- });
- });
- describe('Order entity', () => {
- let orderId: string;
- beforeAll(async () => {
- const { addItemToOrder } = await shopClient.query(addItemToOrderDocument, {
- productVariantId: 'T_1',
- quantity: 1,
- });
- orderId = (addItemToOrder as any).id;
- });
- it('shop setOrderCustomFields', async () => {
- const { setOrderCustomFields } = await shopClient.query(gql`
- mutation {
- setOrderCustomFields(
- input: {
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- ... on Order {
- id
- ${customFieldsSelection}
- }
- }
- }
- `);
- assertCustomFieldIds(setOrderCustomFields.customFields, 'T_2', ['T_3', 'T_4']);
- });
- it('admin setOrderCustomFields', async () => {
- const { setOrderCustomFields } = await adminClient.query(gql`
- mutation {
- setOrderCustomFields(
- input: {
- id: "${orderId}"
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- ... on Order {
- id
- ${customFieldsSelection}
- }
- }
- }
- `);
- assertCustomFieldIds(setOrderCustomFields.customFields, 'T_1', ['T_1', 'T_2']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1664#issuecomment-1320872627
- it('admin order query with eager-loaded custom field relation', async () => {
- const { order } = await adminClient.query(gql`
- query {
- order(id: 1) {
- id
- customFields {
- productOwner {
- id
- }
- }
- }
- }
- `);
- // we're just making sure it does not throw here.
- expect(order).toEqual({
- customFields: {
- productOwner: null,
- },
- id: 'T_1',
- });
- });
- });
- describe('OrderLine entity', () => {
- it('shop addItemToOrder', async () => {
- const { addItemToOrder } = await shopClient.query(
- gql`mutation {
- addItemToOrder(productVariantId: "T_1", quantity: 1, customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }) {
- ... on Order {
- id
- ${customFieldsSelection}
- }
- }
- }`,
- );
- assertCustomFieldIds(addItemToOrder.customFields, 'T_1', ['T_1', 'T_2']);
- });
- });
- describe('Product, ProductVariant entity', () => {
- let productId: string;
- it('admin createProduct', async () => {
- const { createProduct } = await adminClient.query(gql`
- mutation {
- createProduct(
- input: {
- translations: [{ languageCode: en, name: "test" slug: "test" description: "test" }]
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createProduct.customFields, 'T_1', ['T_1', 'T_2']);
- productId = createProduct.id;
- });
- it('admin updateProduct', async () => {
- const { updateProduct } = await adminClient.query(gql`
- mutation {
- updateProduct(
- input: {
- id: "${productId}"
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateProduct.customFields, 'T_2', ['T_3', 'T_4']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2840
- it('updating custom field relation on Product does not delete primitive values', async () => {
- const { updateProduct } = await adminClient.query(gql`
- mutation {
- updateProduct(
- input: {
- id: "${productId}"
- customFields: { singleId: "T_3" }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(updateProduct.customFields.single).toEqual({ id: 'T_3' });
- expect(updateProduct.customFields.primitive).toBe('test');
- });
- let productVariantId: string;
- it('admin createProductVariant', async () => {
- const { createProductVariants } = await adminClient.query(gql`
- mutation {
- createProductVariants(
- input: [{
- sku: "TEST01"
- productId: "${productId}"
- translations: [{ languageCode: en, name: "test" }]
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }]
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createProductVariants[0].customFields, 'T_1', ['T_1', 'T_2']);
- productVariantId = createProductVariants[0].id;
- });
- it('admin updateProductVariant', async () => {
- const { updateProductVariants } = await adminClient.query(gql`
- mutation {
- updateProductVariants(
- input: [{
- id: "${productVariantId}"
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }]
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateProductVariants[0].customFields, 'T_2', ['T_3', 'T_4']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2840
- it('updating custom field relation on ProductVariant does not delete primitive values', async () => {
- const { updateProductVariants } = await adminClient.query(gql`
- mutation {
- updateProductVariants(
- input: [{
- id: "${productVariantId}"
- customFields: { singleId: "T_3" }
- }]
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(updateProductVariants[0].customFields.single).toEqual({ id: 'T_3' });
- expect(updateProductVariants[0].customFields.primitive).toBe('test');
- });
- describe('issue 1664', () => {
- // https://github.com/vendure-ecommerce/vendure/issues/1664
- it('successfully gets product by id with eager-loading custom field relation', async () => {
- const { product } = await shopClient.query(gql`
- query {
- product(id: "T_1") {
- id
- customFields {
- cfVendor {
- featuredProduct {
- id
- }
- }
- }
- }
- }
- `);
- expect(product).toBeDefined();
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1664
- it('successfully gets product by id with nested eager-loading custom field relation', async () => {
- const { customer } = await adminClient.query(gql`
- query {
- customer(id: "T_1") {
- id
- firstName
- lastName
- emailAddress
- phoneNumber
- user {
- customFields {
- cfVendor {
- id
- }
- }
- }
- }
- }
- `);
- expect(customer).toBeDefined();
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1664
- it('successfully gets product.variants with nested custom field relation', async () => {
- await adminClient.query(gql`
- mutation {
- updateProductVariants(
- input: [{ id: "T_1", customFields: { cfRelatedProductsIds: ["T_2"] } }]
- ) {
- id
- }
- }
- `);
- const { product } = await adminClient.query(gql`
- query {
- product(id: "T_1") {
- variants {
- id
- customFields {
- cfRelatedProducts {
- featuredAsset {
- id
- }
- }
- }
- }
- }
- }
- `);
- expect(product).toBeDefined();
- expect(product.variants[0].customFields.cfRelatedProducts).toEqual([
- {
- featuredAsset: { id: 'T_2' },
- },
- ]);
- });
- it('successfully gets product by slug with eager-loading custom field relation', async () => {
- const { product } = await shopClient.query(gql`
- query {
- product(slug: "laptop") {
- id
- customFields {
- cfVendor {
- featuredProduct {
- id
- }
- }
- }
- }
- }
- `);
- expect(product).toBeDefined();
- });
- it('does not error on custom field relation with eager custom field relation', async () => {
- const { product } = await adminClient.query(gql`
- query {
- product(slug: "laptop") {
- name
- customFields {
- owner {
- id
- code
- customFields {
- profile {
- id
- name
- }
- }
- }
- }
- }
- }
- `);
- expect(product).toBeDefined();
- });
- });
- });
- describe('ProductOptionGroup, ProductOption entity', () => {
- let productOptionGroupId: string;
- it('admin createProductOptionGroup', async () => {
- const { createProductOptionGroup } = await adminClient.query(gql`
- mutation {
- createProductOptionGroup(
- input: {
- code: "test"
- options: []
- translations: [{ languageCode: en, name: "test" }]
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createProductOptionGroup.customFields, 'T_1', ['T_1', 'T_2']);
- productOptionGroupId = createProductOptionGroup.id;
- });
- it('admin updateProductOptionGroup', async () => {
- const { updateProductOptionGroup } = await adminClient.query(gql`
- mutation {
- updateProductOptionGroup(
- input: {
- id: "${productOptionGroupId}"
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateProductOptionGroup.customFields, 'T_2', ['T_3', 'T_4']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2840
- it('updating custom field relation on ProductOptionGroup does not delete primitive values', async () => {
- const { updateProductOptionGroup } = await adminClient.query(gql`
- mutation {
- updateProductOptionGroup(
- input: {
- id: "${productOptionGroupId}"
- customFields: { singleId: "T_3" }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(updateProductOptionGroup.customFields.single).toEqual({ id: 'T_3' });
- expect(updateProductOptionGroup.customFields.primitive).toBe('test');
- });
- let productOptionId: string;
- it('admin createProductOption', async () => {
- const { createProductOption } = await adminClient.query(gql`
- mutation {
- createProductOption(
- input: {
- productOptionGroupId: "${productOptionGroupId}"
- code: "test-option"
- translations: [{ languageCode: en, name: "test-option" }]
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createProductOption.customFields, 'T_1', ['T_1', 'T_2']);
- productOptionId = createProductOption.id;
- });
- it('admin updateProductOption', async () => {
- const { updateProductOption } = await adminClient.query(gql`
- mutation {
- updateProductOption(
- input: {
- id: "${productOptionId}"
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateProductOption.customFields, 'T_2', ['T_3', 'T_4']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2840
- it('updating custom field relation on ProductOption does not delete primitive values', async () => {
- const { updateProductOption } = await adminClient.query(gql`
- mutation {
- updateProductOption(
- input: {
- id: "${productOptionId}"
- customFields: { singleId: "T_3" }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(updateProductOption.customFields.single).toEqual({ id: 'T_3' });
- expect(updateProductOption.customFields.primitive).toBe('test');
- });
- });
- describe('ShippingMethod entity', () => {
- let shippingMethodId: string;
- it('admin createShippingMethod', async () => {
- const { createShippingMethod } = await adminClient.query(gql`
- mutation {
- createShippingMethod(
- input: {
- code: "test"
- calculator: {
- code: "${defaultShippingCalculator.code}"
- arguments: [
- { name: "rate" value: "10"},
- { name: "includesTax" value: "true"},
- { name: "taxRate" value: "10"},
- ]
- }
- checker: {
- code: "${defaultShippingEligibilityChecker.code}"
- arguments: [
- { name: "orderMinimum" value: "0"},
- ]
- }
- fulfillmentHandler: "${manualFulfillmentHandler.code}"
- translations: [{ languageCode: en, name: "test" description: "test" }]
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createShippingMethod.customFields, 'T_1', ['T_1', 'T_2']);
- shippingMethodId = createShippingMethod.id;
- });
- it('admin updateShippingMethod', async () => {
- const { updateShippingMethod } = await adminClient.query(gql`
- mutation {
- updateShippingMethod(
- input: {
- id: "${shippingMethodId}"
- translations: []
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateShippingMethod.customFields, 'T_2', ['T_3', 'T_4']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2840
- it('updating custom field relation on ShippingMethod does not delete primitive values', async () => {
- const { updateShippingMethod } = await adminClient.query(gql`
- mutation {
- updateShippingMethod(
- input: {
- id: "${shippingMethodId}"
- translations: []
- customFields: { singleId: "T_3" }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(updateShippingMethod.customFields.single).toEqual({ id: 'T_3' });
- expect(updateShippingMethod.customFields.primitive).toBe('test');
- });
- it('shop eligibleShippingMethods (ShippingMethodQuote)', async () => {
- const { eligibleShippingMethods } = await shopClient.query(gql`
- query {
- eligibleShippingMethods {
- id
- name
- code
- description
- ${customFieldsSelection}
- }
- }
- `);
- const testShippingMethodQuote = eligibleShippingMethods.find(
- (quote: any) => quote.code === 'test',
- );
- assertCustomFieldIds(testShippingMethodQuote.customFields, 'T_3', ['T_3', 'T_4']);
- });
- });
- describe('PaymentMethod entity', () => {
- let paymentMethodId: string;
- it('admin createShippingMethod', async () => {
- const { createPaymentMethod } = await adminClient.query(gql`
- mutation {
- createPaymentMethod(
- input: {
- code: "test"
- enabled: true
- handler: {
- code: "${testSuccessfulPaymentMethod.code}"
- arguments: []
- }
- customFields: { singleId: "T_1", multiIds: ["T_1", "T_2"] },
- translations: [{ languageCode: en, name: "test" }]
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(createPaymentMethod.customFields, 'T_1', ['T_1', 'T_2']);
- paymentMethodId = createPaymentMethod.id;
- });
- it('admin updatePaymentMethod', async () => {
- const { updatePaymentMethod } = await adminClient.query(gql`
- mutation {
- updatePaymentMethod(
- input: {
- id: "${paymentMethodId}"
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] },
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updatePaymentMethod.customFields, 'T_2', ['T_3', 'T_4']);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/2840
- it('updating custom field relation on PaymentMethod does not delete primitive values', async () => {
- const { updatePaymentMethod } = await adminClient.query(gql`
- mutation {
- updatePaymentMethod(
- input: {
- id: "${paymentMethodId}"
- customFields: { singleId: "T_3" }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(updatePaymentMethod.customFields.single).toEqual({ id: 'T_3' });
- expect(updatePaymentMethod.customFields.primitive).toBe('test');
- });
- it('shop eligiblePaymentMethods (PaymentMethodQuote)', async () => {
- const { eligiblePaymentMethods } = await shopClient.query(gql`
- query {
- eligiblePaymentMethods {
- id
- name
- description
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(eligiblePaymentMethods[0].customFields, 'T_3', ['T_3', 'T_4']);
- });
- });
- describe('Asset entity', () => {
- it('set custom field relations on Asset', async () => {
- const { updateAsset } = await adminClient.query(gql`
- mutation {
- updateAsset(
- input: {
- id: "T_1",
- customFields: { singleId: "T_2", multiIds: ["T_3", "T_4"] }
- }
- ) {
- id
- ${customFieldsSelection}
- }
- }
- `);
- assertCustomFieldIds(updateAsset.customFields, 'T_2', ['T_3', 'T_4']);
- });
- it('findOne on Asset', async () => {
- const { asset } = await adminClient.query(gql`
- query {
- asset(id: "T_1") {
- id
- ${customFieldsSelection}
- }
- }
- `);
- expect(asset.customFields.single.id).toBe('T_2');
- expect(asset.customFields.multi.length).toEqual(2);
- });
- // https://github.com/vendure-ecommerce/vendure/issues/1636
- it('calling TransactionalConnection.findOneInChannel() returns custom field relations', async () => {
- TestPlugin1636_1664.testResolverSpy.mockReset();
- await shopClient.query(gql`
- query {
- getAssetTest(id: "T_1")
- }
- `);
- const args = TestPlugin1636_1664.testResolverSpy.mock.calls[0];
- expect(args[0].customFields.single.id).toEqual(2);
- expect(args[0].customFields.multi.length).toEqual(2);
- });
- });
- });
- it('null values', async () => {
- const { updateCustomerAddress } = await adminClient.query(gql`
- mutation {
- updateCustomerAddress(
- input: { id: "T_1", customFields: { singleId: null, multiIds: ["T_1", "null"] } }
- ) {
- id
- customFields {
- single {
- id
- }
- multi {
- id
- }
- }
- }
- }
- `);
- expect(updateCustomerAddress.customFields.single).toEqual(null);
- expect(updateCustomerAddress.customFields.multi).toEqual([{ id: 'T_1' }]);
- });
- describe('bi-direction relations', () => {
- let customEntityRepository: Repository<TestCustomEntity>;
- let customEntity: TestCustomEntity;
- let collectionIdInternal: number;
- beforeAll(async () => {
- customEntityRepository = server.app
- .get(TransactionalConnection)
- .getRepository(RequestContext.empty(), TestCustomEntity);
- const customEntityId = (await customEntityRepository.save({})).id;
- const { createCollection } = await adminClient.query(gql`
- mutation {
- createCollection(
- input: {
- translations: [
- { languageCode: en, name: "Test", description: "test", slug: "test" }
- ]
- filters: []
- customFields: { customEntityListIds: [${customEntityId}] customEntityId: ${customEntityId} }
- }
- ) {
- id
- }
- }
- `);
- collectionIdInternal = parseInt(createCollection.id.replace('T_', ''), 10);
- customEntity = await assertFound(
- customEntityRepository.findOne({
- where: { id: customEntityId },
- relations: {
- customEntityInverse: true,
- customEntityListInverse: true,
- },
- }),
- );
- });
- it('can create inverse relation for list=false', () => {
- expect(customEntity.customEntityInverse).toEqual([
- expect.objectContaining({ id: collectionIdInternal }),
- ]);
- });
- it('can create inverse relation for list=true', () => {
- expect(customEntity.customEntityListInverse).toEqual([
- expect.objectContaining({ id: collectionIdInternal }),
- ]);
- });
- });
- });
|