Skip to content

Commit

Permalink
chore: conditionally choose default component for AWSTimestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Hein Jeong authored and hein-j committed Feb 4, 2023
1 parent 820094c commit 1bfcb65
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
limitations under the License.
*/

import { mapModelFieldsConfigs, getFieldTypeMapKey } from '../../../generate-form-definition/helpers';
import { FormDefinition, GenericDataSchema } from '../../../types';
import {
mapModelFieldsConfigs,
getFieldTypeMapKey,
getFieldConfigFromModelField,
} from '../../../generate-form-definition/helpers';
import { FormDefinition, GenericDataField, GenericDataSchema } from '../../../types';
import { getBasicFormDefinition } from '../../__utils__/basic-form-definition';

describe('mapModelFieldsConfigs', () => {
Expand Down Expand Up @@ -791,3 +795,43 @@ describe('getFieldTypeMapKey', () => {
).toBe('NonModel');
});
});

describe('getFieldConfigFromModelField', () => {
it('should map the componentType of AWSTimestamp fields based on feature flag', () => {
const fieldName = 'birthTime';
const field: GenericDataField = { dataType: 'AWSTimestamp', readOnly: false, required: false, isArray: false };
const dataSchema: GenericDataSchema = {
dataSourceType: 'DataStore',
enums: {},
nonModels: {},
models: {
Dog: {
primaryKeys: ['id'],
fields: {
[fieldName]: field,
},
},
},
};

const fieldConfigWithoutFeatureFlags = getFieldConfigFromModelField({
dataTypeName: 'Dog',
fieldName,
field,
dataSchema,
featureFlags: undefined,
});

expect(fieldConfigWithoutFeatureFlags.inputType?.type).toBe('DateTimeField');

const fieldConfigWithRelationshipEnabled = getFieldConfigFromModelField({
dataTypeName: 'Dog',
fieldName,
field,
dataSchema,
featureFlags: { isRelationshipSupported: true },
});

expect(fieldConfigWithRelationshipEnabled.inputType?.type).toBe('NumberField');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,23 @@ export function getFieldConfigFromModelField({
field,
dataSchema,
setReadOnly,
featureFlags,
}: {
dataTypeName: string;
fieldName: string;
field: GenericDataField;
dataSchema: GenericDataSchema;
setReadOnly?: boolean;
featureFlags: FormFeatureFlags | undefined;
}): ExtendedStudioGenericFieldConfig {
const fieldTypeMapKey = getFieldTypeMapKey(field);

const { defaultComponent } = FIELD_TYPE_MAP[fieldTypeMapKey];
let { defaultComponent } = FIELD_TYPE_MAP[fieldTypeMapKey];

// we are rolling out the switch to NumberField with relationship support
if (!featureFlags?.isRelationshipSupported && fieldTypeMapKey === 'AWSTimestamp') {
defaultComponent = 'DateTimeField';
}

// When the relationship is many to many, set data type to the actual related model instead of the join table
// if (field.relationship && field.relationship.type === 'HAS_MANY' && field.relationship.relatedJoinTableName) {
Expand Down Expand Up @@ -308,6 +315,7 @@ export function mapModelFieldsConfigs({
field,
dataSchema,
setReadOnly: isPrimaryKey && formActionType === 'update',
featureFlags,
});
});

Expand Down

0 comments on commit 1bfcb65

Please sign in to comment.