Skip to content

Commit 84ee0cf

Browse files
committed
add/fix faker call preview test
1 parent 06258a8 commit 84ee0cf

File tree

2 files changed

+74
-6
lines changed

2 files changed

+74
-6
lines changed

packages/compass-collection/src/components/mock-data-generator-modal/faker-mapping-selector.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,12 @@ const FakerMappingSelector = ({
9797
</Banner>
9898
) : (
9999
<>
100-
<Label htmlFor="preview-faker-function-call">
100+
<Label htmlFor="faker-function-call-preview">
101101
Preview Faker Function Call
102102
</Label>
103103
<Code
104-
id="preview-faker-function-call"
104+
id="faker-function-call-preview"
105+
data-testid="faker-function-call-preview"
105106
language="javascript"
106107
copyable={false}
107108
>

packages/compass-collection/src/components/mock-data-generator-modal/mock-data-generator-modal.spec.tsx

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,8 +538,8 @@ describe('MockDataGeneratorModal', () => {
538538
);
539539
});
540540

541-
it('does not show faker args that are too large', async () => {
542-
const largeLengthArgs = Array.from({ length: 11 }, () => 'test');
541+
it('displays preview of the faker call without args when the args are invalid', async () => {
542+
const largeLengthArgs = Array.from({ length: 11 }, () => 'testArg');
543543
const mockServices = createMockServices();
544544
mockServices.atlasAiService.getMockDataSchema = () =>
545545
Promise.resolve({
@@ -548,7 +548,45 @@ describe('MockDataGeneratorModal', () => {
548548
fieldPath: 'name',
549549
mongoType: 'String',
550550
fakerMethod: 'person.firstName',
551-
fakerArgs: [JSON.stringify(largeLengthArgs)],
551+
fakerArgs: largeLengthArgs,
552+
isArray: false,
553+
probability: 1.0,
554+
},
555+
{
556+
fieldPath: 'age',
557+
mongoType: 'Int32',
558+
fakerMethod: 'number.int',
559+
fakerArgs: [
560+
{
561+
json: JSON.stringify({
562+
a: largeLengthArgs,
563+
}),
564+
},
565+
],
566+
isArray: false,
567+
probability: 1.0,
568+
},
569+
{
570+
fieldPath: 'username',
571+
mongoType: 'String',
572+
fakerMethod: 'string.alpha',
573+
// large string
574+
fakerArgs: ['a'.repeat(1001)],
575+
isArray: false,
576+
probability: 1.0,
577+
},
578+
{
579+
fieldPath: 'avatar',
580+
mongoType: 'String',
581+
fakerMethod: 'image.url',
582+
fakerArgs: [
583+
{
584+
json: JSON.stringify({
585+
width: 100_000,
586+
height: 100_000,
587+
}),
588+
},
589+
],
552590
isArray: false,
553591
probability: 1.0,
554592
},
@@ -564,8 +602,19 @@ describe('MockDataGeneratorModal', () => {
564602
type: 'String',
565603
probability: 1.0,
566604
},
605+
age: {
606+
type: 'Int32',
607+
probability: 1.0,
608+
},
609+
username: {
610+
type: 'String',
611+
probability: 1.0,
612+
},
613+
avatar: {
614+
type: 'String',
615+
probability: 1.0,
616+
},
567617
},
568-
sampleDocument: { name: 'Peaches' },
569618
},
570619
});
571620

@@ -574,6 +623,24 @@ describe('MockDataGeneratorModal', () => {
574623
await waitFor(() => {
575624
expect(screen.getByTestId('faker-schema-editor')).to.exist;
576625
});
626+
627+
userEvent.click(screen.getByText('name'));
628+
expect(screen.getByTestId('faker-function-call-preview')).to.exist;
629+
expect(screen.queryByText(/testArg/)).to.not.exist;
630+
631+
userEvent.click(screen.getByText('age'));
632+
expect(screen.getByTestId('faker-function-call-preview')).to.exist;
633+
expect(screen.queryByText(/testArg/)).to.not.exist;
634+
635+
userEvent.click(screen.getByText('username'));
636+
expect(screen.queryByText(/aaaaaaa/)).to.not.exist;
637+
expect(screen.getByTestId('faker-function-call-preview')).to.exist;
638+
639+
userEvent.click(screen.getByText('avatar'));
640+
expect(screen.getByTestId('faker-function-call-preview')).to.exist;
641+
expect(screen.queryByText(/width/)).to.not.exist;
642+
expect(screen.queryByText(/height/)).to.not.exist;
643+
expect(screen.queryByText(/100000/)).to.not.exist;
577644
});
578645

579646
it('disables the Next button when the faker schema mapping is not confirmed', async () => {

0 commit comments

Comments
 (0)