| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448 |
- import { mergeConfig } from '@vendure/core';
- import { createTestEnvironment } from '@vendure/testing';
- import gql from 'graphql-tag';
- import path from 'path';
- import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
- import { initialData } from '../../../e2e-common/e2e-initial-data';
- import { TEST_SETUP_TIMEOUT_MS, testConfig } from '../../../e2e-common/test-config';
- import { assertThrowsWithMessage } from './utils/assert-throws-with-message';
- import { fixPostgresTimezone } from './utils/fix-pg-timezone';
- fixPostgresTimezone();
- const validateInjectorSpy = vi.fn();
- const customConfig = mergeConfig(testConfig(), {
- dbConnectionOptions: {
- timezone: 'Z',
- },
- customFields: {
- Product: [
- {
- name: 'attributes',
- type: 'struct',
- fields: [
- { name: 'color', type: 'string' },
- { name: 'size', type: 'string' },
- { name: 'material', type: 'string' },
- { name: 'weight', type: 'int' },
- { name: 'isDownloadable', type: 'boolean' },
- { name: 'releaseDate', type: 'datetime' },
- ],
- },
- ],
- Customer: [
- {
- name: 'coupons',
- type: 'struct',
- list: true,
- fields: [
- { name: 'code', type: 'string' },
- { name: 'discount', type: 'int' },
- { name: 'used', type: 'boolean' },
- ],
- },
- {
- name: 'company',
- type: 'struct',
- fields: [{ name: 'phoneNumbers', type: 'string', list: true }],
- },
- {
- name: 'withValidation',
- type: 'struct',
- fields: [
- { name: 'stringWithPattern', type: 'string', pattern: '^[0-9][a-z]+$' },
- { name: 'numberWithRange', type: 'int', min: 1, max: 10 },
- {
- name: 'stringWithValidationFn',
- type: 'string',
- validate: value => {
- if (value !== 'valid') {
- return `The value ['${value as string}'] is not valid`;
- }
- },
- },
- ],
- },
- ],
- },
- });
- describe('Custom field struct type', () => {
- const { server, adminClient, shopClient } = createTestEnvironment(customConfig);
- beforeAll(async () => {
- await server.init({
- initialData,
- productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'),
- customerCount: 1,
- });
- await adminClient.asSuperAdmin();
- }, TEST_SETUP_TIMEOUT_MS);
- afterAll(async () => {
- await server.destroy();
- });
- it('globalSettings.serverConfig.customFieldConfig resolves struct fields', async () => {
- const { globalSettings } = await adminClient.query(gql`
- query {
- globalSettings {
- serverConfig {
- customFieldConfig {
- Product {
- ... on CustomField {
- name
- type
- list
- }
- ... on StructCustomFieldConfig {
- fields {
- ... on StructField {
- name
- type
- list
- }
- }
- }
- }
- }
- }
- }
- }
- `);
- expect(globalSettings.serverConfig.customFieldConfig.Product).toEqual([
- {
- name: 'attributes',
- type: 'struct',
- list: false,
- fields: [
- { name: 'color', type: 'string', list: false },
- { name: 'size', type: 'string', list: false },
- { name: 'material', type: 'string', list: false },
- { name: 'weight', type: 'int', list: false },
- { name: 'isDownloadable', type: 'boolean', list: false },
- { name: 'releaseDate', type: 'datetime', list: false },
- ],
- },
- ]);
- });
- it('globalSettings.serverConfig.entityCustomFields resolves struct fields', async () => {
- const { globalSettings } = await adminClient.query(gql`
- query {
- globalSettings {
- serverConfig {
- entityCustomFields {
- entityName
- customFields {
- ... on CustomField {
- name
- type
- list
- }
- ... on StructCustomFieldConfig {
- fields {
- ... on StructField {
- name
- type
- list
- }
- }
- }
- }
- }
- }
- }
- }
- `);
- const productEntry = globalSettings.serverConfig.entityCustomFields.find(
- (e: any) => e.entityName === 'Product',
- );
- expect(productEntry).toEqual({
- entityName: 'Product',
- customFields: [
- {
- name: 'attributes',
- type: 'struct',
- list: false,
- fields: [
- { name: 'color', type: 'string', list: false },
- { name: 'size', type: 'string', list: false },
- { name: 'material', type: 'string', list: false },
- { name: 'weight', type: 'int', list: false },
- { name: 'isDownloadable', type: 'boolean', list: false },
- { name: 'releaseDate', type: 'datetime', list: false },
- ],
- },
- ],
- });
- });
- it('struct fields initially null', async () => {
- const result = await adminClient.query(gql`
- query {
- product(id: "T_1") {
- id
- customFields {
- attributes {
- color
- size
- material
- weight
- isDownloadable
- releaseDate
- }
- }
- }
- }
- `);
- expect(result.product.customFields.attributes).toEqual({
- color: null,
- size: null,
- material: null,
- weight: null,
- isDownloadable: null,
- releaseDate: null,
- });
- });
- it('update all fields in struct', async () => {
- const result = await adminClient.query(gql`
- mutation {
- updateProduct(
- input: {
- id: "T_1"
- customFields: {
- attributes: {
- color: "red"
- size: "L"
- material: "cotton"
- weight: 123
- isDownloadable: true
- releaseDate: "2021-01-01T12:00:00.000Z"
- }
- }
- }
- ) {
- id
- customFields {
- attributes {
- color
- size
- material
- weight
- isDownloadable
- releaseDate
- }
- }
- }
- }
- `);
- expect(result.updateProduct.customFields.attributes).toEqual({
- color: 'red',
- size: 'L',
- material: 'cotton',
- weight: 123,
- isDownloadable: true,
- releaseDate: '2021-01-01T12:00:00.000Z',
- });
- });
- it('partial update of struct fields nulls missing fields', async () => {
- const result = await adminClient.query(gql`
- mutation {
- updateProduct(
- input: {
- id: "T_1"
- customFields: { attributes: { color: "red", size: "L", material: "cotton" } }
- }
- ) {
- id
- customFields {
- attributes {
- color
- size
- material
- weight
- isDownloadable
- releaseDate
- }
- }
- }
- }
- `);
- expect(result.updateProduct.customFields.attributes).toEqual({
- color: 'red',
- size: 'L',
- material: 'cotton',
- weight: null,
- isDownloadable: null,
- releaseDate: null,
- });
- });
- describe('struct list', () => {
- it('is initially an empty array', async () => {
- const result = await adminClient.query(gql`
- query {
- customer(id: "T_1") {
- customFields {
- coupons {
- code
- discount
- used
- }
- }
- }
- }
- `);
- expect(result.customer.customFields.coupons).toEqual([]);
- });
- it('sets list values', async () => {
- const result = await adminClient.query(gql`
- mutation {
- updateCustomer(
- input: {
- id: "T_1"
- customFields: {
- coupons: [
- { code: "ABC", discount: 10, used: false }
- { code: "DEF", discount: 20, used: true }
- ]
- }
- }
- ) {
- ... on Customer {
- id
- customFields {
- coupons {
- code
- discount
- used
- }
- }
- }
- }
- }
- `);
- expect(result.updateCustomer.customFields).toEqual({
- coupons: [
- { code: 'ABC', discount: 10, used: false },
- { code: 'DEF', discount: 20, used: true },
- ],
- });
- });
- });
- describe('struct field list', () => {
- it('is initially an empty array', async () => {
- const result = await adminClient.query(gql`
- query {
- customer(id: "T_1") {
- id
- customFields {
- company {
- phoneNumbers
- }
- }
- }
- }
- `);
- expect(result.customer.customFields.company).toEqual({
- phoneNumbers: [],
- });
- });
- it('set list field values', async () => {
- const result = await adminClient.query(gql`
- mutation {
- updateCustomer(
- input: { id: "T_1", customFields: { company: { phoneNumbers: ["123", "456"] } } }
- ) {
- ... on Customer {
- id
- customFields {
- company {
- phoneNumbers
- }
- }
- }
- }
- }
- `);
- expect(result.updateCustomer.customFields.company).toEqual({
- phoneNumbers: ['123', '456'],
- });
- });
- });
- describe('struct field validation', () => {
- it(
- 'string pattern',
- assertThrowsWithMessage(async () => {
- await adminClient.query(gql`
- mutation {
- updateCustomer(
- input: {
- id: "T_1"
- customFields: { withValidation: { stringWithPattern: "abc" } }
- }
- ) {
- ... on Customer {
- id
- }
- }
- }
- `);
- }, `The custom field "stringWithPattern" value ["abc"] does not match the pattern [^[0-9][a-z]+$]`),
- );
- it(
- 'number range',
- assertThrowsWithMessage(async () => {
- await adminClient.query(gql`
- mutation {
- updateCustomer(
- input: { id: "T_1", customFields: { withValidation: { numberWithRange: 15 } } }
- ) {
- ... on Customer {
- id
- }
- }
- }
- `);
- }, `The custom field "numberWithRange" value [15] is greater than the maximum [10]`),
- );
- it(
- 'validate function',
- assertThrowsWithMessage(async () => {
- await adminClient.query(gql`
- mutation {
- updateCustomer(
- input: {
- id: "T_1"
- customFields: { withValidation: { stringWithValidationFn: "bad" } }
- }
- ) {
- ... on Customer {
- id
- }
- }
- }
- `);
- }, `The value ['bad'] is not valid`),
- );
- });
- });
|