Skip to content

Commit 8decf94

Browse files
committed
fixup: move to function, add unit test
1 parent d703348 commit 8decf94

File tree

2 files changed

+52
-15
lines changed

2 files changed

+52
-15
lines changed

packages/compass-data-modeling/src/store/diagram.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
applyEdit,
55
getCurrentDiagramFromState,
66
getCurrentModel,
7+
getTypeNameForTelemetry,
78
openDiagram,
89
redoEdit,
910
undoEdit,
@@ -574,3 +575,30 @@ describe('Data Modeling store', function () {
574575
});
575576
});
576577
});
578+
579+
describe('getTypeNameForTelemetry', () => {
580+
it('should return undefined when bsonType is undefined', () => {
581+
const result = getTypeNameForTelemetry(undefined);
582+
expect(result).to.be.undefined;
583+
});
584+
585+
it('should return undefined when bsonType is an empty array', () => {
586+
const result = getTypeNameForTelemetry([]);
587+
expect(result).to.be.undefined;
588+
});
589+
590+
it('should return the string when bsonType is a string', () => {
591+
const result = getTypeNameForTelemetry('string');
592+
expect(result).to.equal('string');
593+
});
594+
595+
it('should return the single element when bsonType is an array with one element', () => {
596+
const result = getTypeNameForTelemetry(['string']);
597+
expect(result).to.equal('string');
598+
});
599+
600+
it('should return "mixed" when bsonType is an array with multiple elements', () => {
601+
const result = getTypeNameForTelemetry(['string', 'number']);
602+
expect(result).to.equal('mixed');
603+
});
604+
});

packages/compass-data-modeling/src/store/diagram.ts

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,28 @@ export function renameField(
740740
};
741741
}
742742

743+
/**
744+
* @internal Exported for testing purposes only.
745+
* If the field had a single type, we return that, otherwise 'mixed'.
746+
*/
747+
export function getTypeNameForTelemetry(
748+
bsonType: string | string[] | undefined
749+
): string | undefined {
750+
if (!bsonType) {
751+
return;
752+
}
753+
if (Array.isArray(bsonType)) {
754+
if (bsonType.length === 0) {
755+
return undefined;
756+
}
757+
if (bsonType.length === 1) {
758+
return bsonType[0];
759+
}
760+
return 'mixed';
761+
}
762+
return bsonType;
763+
}
764+
743765
export function changeFieldType(
744766
ns: string,
745767
fieldPath: FieldPath,
@@ -759,21 +781,8 @@ export function changeFieldType(
759781

760782
track('Data Modeling Field Type Changed', {
761783
source: 'side_panel',
762-
// If the field had a single type, we report that, otherwise 'mixed'.
763-
from: field.jsonSchema.bsonType
764-
? Array.isArray(field.jsonSchema.bsonType)
765-
? field.jsonSchema.bsonType.length === 1
766-
? field.jsonSchema.bsonType[0]
767-
: 'mixed'
768-
: field.jsonSchema.bsonType
769-
: undefined,
770-
to: to.bsonType
771-
? Array.isArray(to.bsonType)
772-
? to.bsonType.length === 1
773-
? to.bsonType[0]
774-
: 'mixed'
775-
: to.bsonType
776-
: undefined,
784+
from: getTypeNameForTelemetry(field.jsonSchema.bsonType),
785+
to: getTypeNameForTelemetry(to.bsonType),
777786
});
778787

779788
dispatch(

0 commit comments

Comments
 (0)